Use of MFC IP controls to get local IP display to the control

Source: Internet
Author: User
Tags get ip

When writing LAN programming, it is often necessary to use IP controls.

Excuse me, I'm still using the VC6.0.

drag it to the dialog box to use it!

。。。。。。。。。。。。。。。。。。。。 Split line ........ ........

Get local IP Display IP control

(1) Get the local computer name

int Getlocalhostname (CString &shostname)//Get local computer name
{
Char szhostname[256];
int nRetCode;
Nretcode=gethostname (szhostname,sizeof (szhostname));
if (nretcode!=0)
{
Error generated
shostname=_t ("not obtained");
return GetLastError ();
}
Shostname=szhostname;
return 0;
}

sHostName is the computer name.

(2) with the local computer name, obtain the IP

int getipaddress (const CString &shostname, BYTE &f0,byte &f1,byte &f2,byte &f3)//Get local IP
{
struct hostent FAR * lphostent=gethostbyname (sHostName);
if (lphostent==null)
{
Error generated
f0=f1=f2=f3=0;
return GetLastError ();
}
Get IP
LPSTR lpaddr=lphostent->h_addr_list[0];
if (LPADDR)
{
struct IN_ADDR inaddr;
Memmove (&inaddr,lpaddr,4);
F0=inaddr.s_un. S_UN_B.S_B1;
F1=inaddr.s_un. S_UN_B.S_B2;
F2=inaddr.s_un. S_UN_B.S_B3;
F3=inaddr.s_un. S_UN_B.S_B4;
}
return 0;
}

IP address divided into four parts

(3) Display to IP control

((CIPAddressCtrl *) (GetDlgItem (idc_ipaddress))->setaddress (F0,F1,F2,F3);


Idc_ipaddress for IP control ID




Use of MFC IP controls to get local IP display to the control

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.