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