Get IP address of computer
-
Hi gurus, How do we get the ip address of computer using MFC? Any clues? Ideas? :rose:Thanks, Wax
See the following from which u can get the IP address of the local machine gethostname(szHostName, strlen( szHostName ) ); //returns machine name strFullName="\\\\"+szHostName; host = gethostbyname(strFullName); //returns hostent* if(host!=NULL) { ptr = (struct in_addr *) host->h_addr_list[0]; int a = ptr->S_un.S_un_b.s_b1; // Eg. 211.40.35.76 split up like this. int b = ptr->S_un.S_un_b.s_b2; // 40 int c = ptr->S_un.S_un_b.s_b3; // 35 int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%d.%d.%d.%d",a,b,c,d); AfxMessageBox("IP:="+strTemp); } Thanks and Regards Laxman :cool: FAILURE is the first step towards SUCCESS :cool: -- modified at 23:45 Friday 20th January, 2006
-
See the following from which u can get the IP address of the local machine gethostname(szHostName, strlen( szHostName ) ); //returns machine name strFullName="\\\\"+szHostName; host = gethostbyname(strFullName); //returns hostent* if(host!=NULL) { ptr = (struct in_addr *) host->h_addr_list[0]; int a = ptr->S_un.S_un_b.s_b1; // Eg. 211.40.35.76 split up like this. int b = ptr->S_un.S_un_b.s_b2; // 40 int c = ptr->S_un.S_un_b.s_b3; // 35 int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%d.%d.%d.%d",a,b,c,d); AfxMessageBox("IP:="+strTemp); } Thanks and Regards Laxman :cool: FAILURE is the first step towards SUCCESS :cool: -- modified at 23:45 Friday 20th January, 2006
Is this MFC?:omg:
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Is this MFC?:omg:
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
I was asking whether the code given by Laxman is MFC or not. It's SDK.:^)
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
I was asking whether the code given by Laxman is MFC or not. It's SDK.:^)
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
waxie wrote:
It's in .NET
It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
waxie wrote:
It's in .NET
It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
waxie wrote:
It's in .NET
It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
I'm getting these errors when linking: error LNK2001: unresolved external symbol __imp__gethostbyname@4 error LNK2001: unresolved external symbol __imp__gethostname@8 you have any idea what causes these errors? I guess the linker is looking for a lib file or something.. waxie
-
I'm getting these errors when linking: error LNK2001: unresolved external symbol __imp__gethostbyname@4 error LNK2001: unresolved external symbol __imp__gethostname@8 you have any idea what causes these errors? I guess the linker is looking for a lib file or something.. waxie
Have you added support for
WinSock
in your project. This happens if you don't have support forwinsock
in your project. You need to includeWinsock2.h
And which version of Visual Studio are you using.Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Have you added support for
WinSock
in your project. This happens if you don't have support forwinsock
in your project. You need to includeWinsock2.h
And which version of Visual Studio are you using.Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Yes I have included winsock.h in my project - else it would produce symbol not found errors. I'm using VC6.
Did you link to the Winsock library.
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Did you link to the Winsock library.
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Yes, I have. I don't have errors now but it really doesn't output anything. If it's alright with you, can you check if this code works fine in your pc and outputs your IP ad? Thanks so much. waxie
Try this one.... You may have to modify it a bit to suit your needs....
void CHostNameAndIpDlg::OnBnClickedFind()
{
WSADATA WSAData;
::ZeroMemory(&WSAData, sizeof(WSAData));
//init winsock
::WSAStartup(MAKEWORD(1,0), &WSAData);char szHostName\[MAX\_PATH\]; ::gethostname(szHostName, MAX\_PATH); //SetDlgItemText(IDC\_HOSTNAME, szHostName); AfxMessageBox(szHostName); char szIps\[128\]; struct sockaddr\_in sckAddr; struct hostent \*pHost; pHost = ::gethostbyname(szHostName); for(int index=0; pHost->h\_addr\_list\[index\] != NULL;index++) { memcpy(&sckAddr.sin\_addr, pHost->h\_addr\_list\[index\], pHost->h\_length); strcpy(szIps, inet\_ntoa(sckAddr.sin\_addr)); strcat(szIps, ", "); //SetDlgItemText(IDC\_HOSTIP, szIps); AfxMessageBox(szIps); szIps\[0\] = 0; } //close winsock ::WSACleanup();
}
AfxMessageBox
denotes output. You can replace it with your own output procedure. I have commented my output procedure. First it will display a message box containing host name, second it will display message boxes for each IP found(If there are more that one).Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Try this one.... You may have to modify it a bit to suit your needs....
void CHostNameAndIpDlg::OnBnClickedFind()
{
WSADATA WSAData;
::ZeroMemory(&WSAData, sizeof(WSAData));
//init winsock
::WSAStartup(MAKEWORD(1,0), &WSAData);char szHostName\[MAX\_PATH\]; ::gethostname(szHostName, MAX\_PATH); //SetDlgItemText(IDC\_HOSTNAME, szHostName); AfxMessageBox(szHostName); char szIps\[128\]; struct sockaddr\_in sckAddr; struct hostent \*pHost; pHost = ::gethostbyname(szHostName); for(int index=0; pHost->h\_addr\_list\[index\] != NULL;index++) { memcpy(&sckAddr.sin\_addr, pHost->h\_addr\_list\[index\], pHost->h\_length); strcpy(szIps, inet\_ntoa(sckAddr.sin\_addr)); strcat(szIps, ", "); //SetDlgItemText(IDC\_HOSTIP, szIps); AfxMessageBox(szIps); szIps\[0\] = 0; } //close winsock ::WSACleanup();
}
AfxMessageBox
denotes output. You can replace it with your own output procedure. I have commented my output procedure. First it will display a message box containing host name, second it will display message boxes for each IP found(If there are more that one).Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Remove this from the loop...
szIps[0] = 0;
or else you won't get all the ip addresses. And put the messagebox in the loop, outside the loop. This will display all the ip addresses together.Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
Remove this from the loop...
szIps[0] = 0;
or else you won't get all the ip addresses. And put the messagebox in the loop, outside the loop. This will display all the ip addresses together.Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose: