Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the IP Address

Status
Not open for further replies.

navya

Technical User
Nov 1, 2006
1
IN
hi all
how do i get the IP adress of the system which a user is working.
is there any function that returns the ip address.

thanks in advance

 
Yes there are.
API functions gethostbyname si gethostbyaddr are all you need. Take care with the includes, these are functions from WSOCK.DLL.

Hope this help,s-)
Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
This will print the IP address of all the NICs in a PC.


struct in_addr addr;
struct hostent *phe;
char ac[80];
int i;
char *local;

// get the name of the local host
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
printf("Error getting local host name: %d\n",WSAGetLastError());
return 1;
}
printf("Host name is %s\n",ac);
phe = gethostbyname(ac);
if (phe == 0) {
cerr << &quot;Error: Bad host lookup.&quot; << endl;
return 1;
}
for (i=0; phe->h_addr_list !=0; ++i) {
memcpy(&addr, phe->h_addr_list,sizeof(struct in_addr));
printf(&quot;Card %i) %s\n&quot;,i,inet_ntoa(addr));
local=inet_ntoa(addr);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top