IP Address of own PC
-
nope...doesnt work!!!:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
Try initializing winsock first:
#include "Winsock2.h"
#pragma comment(lib,"Ws2_32.lib")int main(int argc, char* argv[])
{WORD wVersionRequested;
WSADATA wsaData;
int err;wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return -1;
}char host\[80\]; gethostname(host,80); LPHOSTENT lpHost; lpHost = gethostbyname(host); UINT ip = ((LPIN\_ADDR)lpHost->h\_addr)->s\_addr; char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr); return 0;
}
John
-
Try initializing winsock first:
#include "Winsock2.h"
#pragma comment(lib,"Ws2_32.lib")int main(int argc, char* argv[])
{WORD wVersionRequested;
WSADATA wsaData;
int err;wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return -1;
}char host\[80\]; gethostname(host,80); LPHOSTENT lpHost; lpHost = gethostbyname(host); UINT ip = ((LPIN\_ADDR)lpHost->h\_addr)->s\_addr; char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr); return 0;
}
John
Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Autoconfiguration IP Address. . . : 169.254.159.128 Subnet Mask . . . . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . . . . : PPP adapter PacNet: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 210.24.247.6 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 210.24.247.6 the codes u gave me are fetching Autoconfiguration IP Address i.e 169.254.159.128 what i need is IP Address i.e 210.24.247.6 can someone please help???:(( With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
-
Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Autoconfiguration IP Address. . . : 169.254.159.128 Subnet Mask . . . . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . . . . : PPP adapter PacNet: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 210.24.247.6 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 210.24.247.6 the codes u gave me are fetching Autoconfiguration IP Address i.e 169.254.159.128 what i need is IP Address i.e 210.24.247.6 can someone please help???:(( With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
Joseph_R_Thomas wrote: can someone please help??? The problem is you have more than one ip address. The code above will return the first. replace h_addr in the previous examples with h_addr_list[1] to get the second. John
-
Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Autoconfiguration IP Address. . . : 169.254.159.128 Subnet Mask . . . . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . . . . : PPP adapter PacNet: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 210.24.247.6 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 210.24.247.6 the codes u gave me are fetching Autoconfiguration IP Address i.e 169.254.159.128 what i need is IP Address i.e 210.24.247.6 can someone please help???:(( With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
See the docs for hostent[^] and gethostbyname[^] for a better understanding of what is going on. John
-
See the docs for hostent[^] and gethostbyname[^] for a better understanding of what is going on. John
:confused::confused::~ but what if the program keeps on shifting from one machine to another where some machine have more than one IP addresses and soem have only one????:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
-
:confused::confused::~ but what if the program keeps on shifting from one machine to another where some machine have more than one IP addresses and soem have only one????:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
It gets very complicated. Any network adapter may have multiple IP addresses. A computer may have more than one network adapters. I am not sure how you can figure out which ip address is the one that is connected to the internet and actually more than one can be connected to the internet. You can use this code to go through the list of ip addresses:
#include "Winsock2.h"
#pragma comment(lib,"Ws2_32.lib")int main(int argc, char* argv[])
{WORD wVersionRequested;
WSADATA wsaData;
int err;wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return -1;
}char host\[80\]; gethostname(host,80); LPHOSTENT lpHost; lpHost = gethostbyname(host); for(int i=0;(LPIN\_ADDR)lpHost->h\_addr\_list\[i\] != 0;i++) { UINT ip = ((LPIN\_ADDR)lpHost->h\_addr\_list\[i\])->s\_addr; char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr\_list\[i\]); } return 0;
}
John
-
It gets very complicated. Any network adapter may have multiple IP addresses. A computer may have more than one network adapters. I am not sure how you can figure out which ip address is the one that is connected to the internet and actually more than one can be connected to the internet. You can use this code to go through the list of ip addresses:
#include "Winsock2.h"
#pragma comment(lib,"Ws2_32.lib")int main(int argc, char* argv[])
{WORD wVersionRequested;
WSADATA wsaData;
int err;wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return -1;
}char host\[80\]; gethostname(host,80); LPHOSTENT lpHost; lpHost = gethostbyname(host); for(int i=0;(LPIN\_ADDR)lpHost->h\_addr\_list\[i\] != 0;i++) { UINT ip = ((LPIN\_ADDR)lpHost->h\_addr\_list\[i\])->s\_addr; char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr\_list\[i\]); } return 0;
}
John
but then how to select the correct one????:~ :sigh::confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
-
but then how to select the correct one????:~ :sigh::confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
Joseph_R_Thomas wrote: but then how to select the correct one???? They are all correct. I will explain. Think about the case where there is a lan connected to one network adapter and a second network adapter is conected to the internet using a dsl modem. One ip adddress will work to talk to the other pcs on the lan. The other ip address will work for the internet. I assume you mean the one connected to the internet?? I am not sure. John
-
Joseph_R_Thomas wrote: but then how to select the correct one???? They are all correct. I will explain. Think about the case where there is a lan connected to one network adapter and a second network adapter is conected to the internet using a dsl modem. One ip adddress will work to talk to the other pcs on the lan. The other ip address will work for the internet. I assume you mean the one connected to the internet?? I am not sure. John
u see..in my school its al connected to lan so there is only one ip.. at my house however as i showed you in the first or second post..there are more than one IP Address....in this case i need the IP Address that connects to the internet...the one that is called IP Address not AutoConfiguration IP Address..... so in school the codes work fine..but at home...i got prob since i need to select the IP Address not the Auto config IP Address....:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
-
u see..in my school its al connected to lan so there is only one ip.. at my house however as i showed you in the first or second post..there are more than one IP Address....in this case i need the IP Address that connects to the internet...the one that is called IP Address not AutoConfiguration IP Address..... so in school the codes work fine..but at home...i got prob since i need to select the IP Address not the Auto config IP Address....:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7
I might be on the wrong track here but when you call
bind
on a socket you can specify ADDR_ANY for the ip address. Then, windows will sort out which actual IP address to use based on routing information - this saves you having to manually determine which IP address you need to select. Phil -
I might be on the wrong track here but when you call
bind
on a socket you can specify ADDR_ANY for the ip address. Then, windows will sort out which actual IP address to use based on routing information - this saves you having to manually determine which IP address you need to select. PhilI was thinking the same. It depends why he wants the address. John