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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get IP address

Status
Not open for further replies.

exodus300

Programmer
Mar 22, 2002
262
AU
I think i saw something about this a while ago, but I never saw a response.

How can you get the IP address of the computer? [pc3]
 
Try thread101-18803. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that they think I am now
qualified to do anything with nothing.
 
#include <iostream.h>
#include <winsock.h>

struct hostent *phe;
//----------------------------------------------------------
String GetLocalIP(int ipnum)
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
return &quot;255&quot;;
}

//int retval = doit(argc, argv);


char ac[80];

if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
{
//return WSAGetLastError();
}

//return ac;

/* attempts to find host */

phe = gethostbyname(ac);

if (phe == 0)
{
return &quot;error&quot;;
}

/* Return specified local IP address */
for (int i = 0; phe->h_addr_list != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list, sizeof(struct in_addr));
//cout << &quot;Address &quot; << i << &quot;: &quot; << inet_ntoa(addr) << endl;

/* returns specified IP */
if (i == ipnum)
return inet_ntoa(addr);
}


WSACleanup();

return &quot;Done&quot;;
}
//---------------------------------------------------------- Cyprus
 
I tried this, passing 0 and 1 as the paramater, but it returns &quot;80.13.127.0&quot;. Any number other than 0 or 1 returns &quot;Done&quot;.

My local address is 192.168.0.1
My internet address is dynamic, but usually 144.13x.xxx.xxx

What exactly is the ipnum paramater for?

By the way 2ffat, Thread101-18803 is no good. [pc3]
 
[blush] I left off the one: thread101-188301. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that they think I am now
qualified to do anything with nothing.
 
[upsidedown]
2ffat,
This time you swapped the 3 and the 1
The correct thread should be Thread101-188031 [pc3]
 
basically, it will return all of thhe IP addresses on your machine, hence the &quot;ipnum&quot;, the number of the IP address that you want. To get your IP address, make ipnum '0'. If there is no IP, then it will return &quot;Done&quot;. Cyprus
 
[colorface] This just isn't my thread. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that they think I am now
qualified to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top