void Csetsignalboxdlg::onbnclickedbuttongetbroadcastaddr ()
{//Gaze section is also a way to get the IP, you can skip
Char name[255];//defines the variable used to hold the obtained host name
Char *ip;//defines an IP address variable
CString strip= "";
Phostent Hostinfo;
if (gethostname (name, sizeof (name)) = = 0)
//{
// Assume that the local host name is successfully stored in a buffer specified by the name parameter
// if ((Hostinfo = gethostbyname (Name)) = NULL)
// {
// This is the get host name. Assuming the hostname succeeds, a pointer is returned, pointing to the Hostinfo,hostinfo
// As a variable of type phostent, the following will be used in this structure
// IP = Inet_ntoa (* (struct in_addr *) *hostinfo->h_addr_list);
// Strip.format (IP);
// }
//}
CString StrIP;
CString Strmask;
CString Strbroadcast;
CString Strgateway;
IN_ADDR broadcast;
Pip_adapter_info Padapterinfo; IP_ADAPTER_INFO Structure Storage network card information, including network card name, network card description, network card MAC address, network card IP, etc.
Pip_adapter_info padapter = NULL;
DWORD dwretval = 0;
Padapterinfo = (Ip_adapter_info *) malloc (sizeof (ip_adapter_info));
ULONG Uloutbuflen;
Get structure size for GetAdaptersInfo parameters
Uloutbuflen = sizeof (Ip_adapter_info);
First call Getadapterinfo get Uloutbuflen size
Call the GetAdaptersInfo function to populate the Pipadapterinfo pointer variable, where the Uloutbuflen parameter is both an input and an output
if (GetAdaptersInfo (padapterinfo, &uloutbuflen) = = Error_buffer_overflow)
{
Free (padapterinfo);
Padapterinfo = (Ip_adapter_info *) malloc (Uloutbuflen);
}
if ((Dwretval = GetAdaptersInfo (Padapterinfo, &uloutbuflen)) = = No_error)
{
Padapter = Padapterinfo;
while (Padapter)//have multiple network cards
{
Strip.format (_t ("%s"), padapter->ipaddresslist.ipaddress.string);
Strmask.format (_t ("%s"), padapter->ipaddresslist.ipmask.string);
Strgateway.format (_t ("%s"), padapter->gatewaylist.ipaddress.string);
Padapter = padapter->next;
}
Broadcast. S_un. S_addr = (inet_addr (StrIP) & Inet_addr (strmask)) | (~inet_addr (Strmask));
Strbroadcast = Inet_ntoa (broadcast);
}
Setdlgitemtext (Idc_edit_serverip,strbroadcast);
Setdlgitemtext (Idc_edit_subnetmask,strmask);
Setdlgitemtext (Idc_edit_gateway,strgateway);
if (padapterinfo)
{
Free (padapterinfo);
}
}
MFC proactively acquires network address functions----broadcast address, gateway, subnet mask