Archived

This topic is now archived and is closed to further replies.

Local machine IP address with Winsock

This topic is 5590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As the subject suggests, I''m trying to figure out the IP of the local machine. I''ve figured out how to get the host name, but I can''t seem to resolve it to an IP without crashing Any way to get just the IP? Thanks. --------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites
try this:


  
#include <iostream>

#include <windows.h>

using namespace std;
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA wsData;
::WSAStartup(MAKEWORD(2,2),&wsData);
char name[512];
::gethostname(name,512);
hostent* phost = ::gethostbyname(name);
sockaddr_in addr;
::memset(&addr,0,sizeof(addr));
::memcpy(&(addr.sin_addr.s_addr),phost->h_addr,4);
char* ip = ::inet_ntoa(addr.sin_addr);
cout << name << ", ip: " << ip << endl;
::WSACleanup();
}

Share this post


Link to post
Share on other sites
Thanks for the quick reply. Ok, I tried that and it crashes It is very similar to the code I myself wrote. Did I mention I''m writing this app for Windows Pocket PC 2000/2002? I''m not sure if that''ll make a difference... any other ideas?

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites