Article ID: 129065 - Last Review: July 11, 2005 - Revision: 1.1

PRB: Getsockname() Returns IP Address 0.0.0.0 for UDP

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q129065

On This Page

Expand all | Collapse all

SYMPTOMS

By following the steps listed below, you might think you should get back the interface address over which the connection was made. However, it actually returns the address 0.0.0.0.
  1. Open a UDP socket.
  2. Bind it to INADDR_ANY.
  3. Call connect() to make a UDP connection.
  4. Call getsockname() on your socket.
However, if it was a TCP socket, you would get back the IP address of the interface.

CAUSE

UDP

This is the behaviour expected from some flavors of UNIX, notably those derived from BSD. When an application calls connect() on a UDP socket that is bound to INADDR_ANY, the operating system associates the remote address with the local socket. This saves the programmer from having to specify the remote IP address in each sendto() or recvfrom(). Instead they may use send() and recv(). Note that this is just a convenience provided by the operating system; there is no network traffic associated with this call. At this point, the underlying IP software determines the interface over which packets will be sent. As described earlier, under BSD UNIX, calling getsockname() will return the IP address of the interface to the application.

This however, is not expected behaviour under Windows NT, Windows 95, or Microsoft TCP IP/32 for Windows for Workgroups version 3.11. Calling getsockname() will return the IP address 0.0.0.0 (INADDR_ANY). Applications should not assume that they can get the IP address of the interface.

TCP

The behaviour is different if it was a TCP socket. In this case, calling getsockname() on a connected socket that was bound to INADDR_ANY will return the IP address of the interface over which the connection was made. The state of the connection can also be observed by typing 'netstat' at a command prompt.

NOTE: To enumerate all the IP addresses on an IP host, the application should call gethostname(), call gethostbyname(), and then iterate through the h_addr_list[] member of the hostent struct returned by gethostbyname() as in this example:
   char     Hostname[100];
   HOSTENT *pHostEnt;
   int      nAdapter = 0;

   gethostname( Hostname, sizeof( Hostname ));
   pHostEnt = gethostbyname( Hostname );

   while ( pHostEnt->h_addr_list[nAdapter] )
   {
      // pHostEnt->h_addr_list[nAdapter] -the current address in host order
      nAdapter++;
   }
				

STATUS

This behavior is by design.

APPLIES TO
  • Microsoft Windows Software Development Kit 3.11
  • Microsoft Windows Software Development Kit 3.11
  • Microsoft Platform Software Development Kit-January 2000 Edition
  • Microsoft Win32 Software Development Kit (SDK) for Windows NT 3.5
Keywords: 
kbwinsock kbnetwork kbip kbprb KB129065