jdl
jdl

Reputation: 6333

Get IP address of my computer on a local network by way of BSD sockets?

Since there is no function in BSD sockets to get the IP address, I did client/server program to establish the connection. One thread for each: server and client.

The IP address returned from "inet_ntoa" with localhost was 127.0.0.1.
But the network says my computer is this 10.0.0.7, and this address is what works.

How do I get the 10.0.0.7 address? Thx

Here is my code:

DWORD WINAPI CIpAddressDlg::Thread_TcpServer(LPVOID iValue)
{
      CIpAddressDlg *pp = (CIpAddressDlg*)iValue;
      CString c;
      char buffer[128]; 
      int sinlen;
      struct sockaddr_in sin; 
      int s, h; 

      sin.sin_family = AF_INET; 
      sin.sin_addr.s_addr = INADDR_ANY;
      sin.sin_port = htons(4000);   // Port 

      s = socket(AF_INET, SOCK_STREAM,0);
      bind(s,(struct sockaddr*)&sin,sizeof(sin));
      listen(s,1);
      sinlen = sizeof(sin);      
      h=accept(s,(struct sockaddr*)&sin,&sinlen );

      //get IP address
      int len = sizeof sin;
      if(::getsockname(h,(struct sockaddr*)&sin,&len) == -1)
            pp->MessageBox("Error local host ip");

      c.Format("%d\nlocal addr %s:%u\n  errno: %d", sin.sin_addr, inet_ntoa(sin.sin_addr),ntohs(sin.sin_port), errno);
      pp->MessageBox(c);

      //verification of send
      recv(h,buffer,sizeof(buffer),0);
      pp->MessageBox(buffer);
      send(h,buffer,strlen(buffer),0);

      ::closesocket(s);
    return 0;
}

DWORD WINAPI CIpAddressDlg::Thread_TcpClient(LPVOID iValue)
{
    CIpAddressDlg *pp = (CIpAddressDlg*)iValue;
    CString c;
      char buffer[128]= "Hello world"; 
      struct sockaddr_in sin; 
      struct hostent *host; 
      int s; 

      host = gethostbyname("localhost");  

      memcpy(&(sin.sin_addr), host->h_addr,host->h_length); 
      sin.sin_family = host->h_addrtype; 
      sin.sin_port = htons(4000);

      s = socket(AF_INET, SOCK_STREAM,0);                       
      connect(s,  (struct sockaddr*)&sin,sizeof(sin));     

      send(s,buffer,strlen(buffer)+1,0); 
      recv(s,buffer,sizeof(buffer),0);

      ::closesocket(s);     

    return 0;
}

Upvotes: 1

Views: 1289

Answers (2)

DigitalRoss
DigitalRoss

Reputation: 146221

Despite the intuitive appeal of the concept and, for that matter, widespread belief in said concept, computers do not have IP addresses.

Interfaces have IP addresses.

You can get a list of the interfaces and choose the first one. Unfortunately, getting a list of interfaces in most languages is system dependent.

The usual approach is to just use 0.0.0.0.

Upvotes: 2

jedwards
jedwards

Reputation: 30250

There are easier ways to get the "private" / "LAN" IP of the machine your program is running on. Your solution is very ingenuitive though.

I think the most straightforward is probably GetAdaptersAddresses. The example code on the MSDN page seems pretty thorough.

But if that requires a newer compiler version, consider GetAdaptersInfo. See "Method Three" here and some sample code here.

Also, it seems like you could also access the IP address via the WinSock API. See this example.

Lastly, just as a sidenote, there is a function in BSD sockets to do exactly this (getifaddrs()), its just not ported to Windows.

Upvotes: 0

Related Questions