MFC proactively acquires network address functions----broadcast address, gateway, subnet mask

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.