Sockets - Using INADDR_ANY on client side

sockaddr_in
sockaddr vs sockaddr_in
socket(af_inet, sock_stream, 0)
sin_family meaning

I recently ran into this blog post which describes a TCP server client using libev. The sever uses INADDR_ANY to bind to an interface which is something I'm familiar with. However, I was surprised to see INADDR_ANY in the client code as well. The relevant code on the client code is as follows:

// Create client socket
if( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
{
  perror("socket error");
  return -1;
}

bzero(&addr, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT_NO);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

// Connect to server socket
if(connect(sd, (struct sockaddr *)&addr, sizeof addr) < 0)
{
  perror("Connect error");
  return -1;
}

Specifically I'm interesed in the line:

addr.sin_addr.s_addr = htonl(INADDR_ANY);

On the server side I understand that INADDR_ANY will bind the port to all available interfaces, but I'm not sure how this makes sense on the client side. In the end, the client will need to connect on a particular interface. Previously I have always specified the IP address or used INADDR_LOOPBACK.

The Linux IP man page doesn't talk about using INADDR_ANY on the client side. I did find another Stack Overflow post here which says that the OP should use INADDR_ANY on the client side, but gives no justification or explanation.

So what is this actually doing? Is it trying all interfaces until it finds one where the port is available for connection? What order does this happen in?

Thanks for your answers!

This is the answer as provided by nos in a comment. If nos comes back and posts it as an answer, I will mark nos' post as the answer and delete this one.

INADDR_ANY is normally defined as 0. That is the IP address 0.0.0.0. RFC 1122 says that means "This host on this network". The linux IP stack seems to just route this to the loopback interface. (e.g. try ping 0.0.0.0 or even just ping 0). I'd say the author made a typo, and should have used INADDR_LOOPBACK.

Why do we use INADDR_ANY in the server side?, I used to believe it would use a random IP address to bind the socket but a little Stackoverflow came up with this, 1. bind() of INADDR_ANY DOES NOT  After creation of the socket, bind function binds the socket to the address and port number specified in addr (custom data structure). In the example code, we bind the server to the localhost, hence we use INADDR_ANY to specify the IP address. Listen: int listen (int sockfd, int backlog);

It seems like your question is not really about "client-side", but about bind vs connect.

INADDR_ANY can be sensibly used with bind on both client and server. Using it with connect() is pointless and should cause a connection failure.

Sockets Tutorial, The steps involved in establishing a socket on the client side are as follows: The examples in this tutorial will use sockets in the Internet domain using the the server is running, and there is a symbolic constant INADDR_ANY which gets this  A socket is one end of an interprocess communication channel. The two processes each establish their own socket. The steps involved in establishing a socket on the client side are as follows: Create a socket with the socket() system call Connect the socket to the address of the server using the connect() system call Send and receive data.

At client side, using INADDR_ANY is redundant, but I have seen some code with that, I guess it is for 'completeness'. You can specify the interface at client side if you want to force a specific interface, e.g. in multihomed machines.

Binding to a port at client side is uncommon as well. It is normally a better idea to let the system to find an available port, or else the program may fail because the port happens to be in use by a client or by a server.

Explicitly assigning port number to client in Socket, In socket programming, when server and client are connected then client is provided any This can be done using bind() system call with specifying particular port number in client side socket. my_addr.sin_addr.s_addr = INADDR_ANY;. This is a quick tutorial on socket programming in c language on a Linux system. "Linux" because the code snippets shown over here will work only on a Linux system and not on Windows.

There is an old BSD convention that connecting to INADDR_ANY means you want to connect to the loopback network. The linux network code explicitly supports this (search for INADDR_ANY in this file). I have no idea what other OSes do or don't support it.

Socket Programming, I recently ran into this blog post which describes a TCP server client using libev. The sever uses INADDR_ANY to bind to an interface which is  In socket programming, all communications happens using the buffer both at client and server side. With the completion of this read , the contents of the file is residing in the buffer and is ready to be sent to the client.

The server side API functions, binding, bind(), listening, listen , The steps involved in establishing a TCP socket on the server side are as follows: Create a socket with INADDR_ANY, 0, Kernel chooses IP address and port. The steps involved in establishing a socket on the client side are as follows: Create a socket with the socket() system call; Connect the socket to the address of the server using the connect() system call; Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

C socket UDP server, In this case, the socket is being bound to the default IP interface by using a special address, INADDR_ANY, and occupies port number 5150. We could have​  Prerequisite: Socket programming in C/C++. In socket programming, when server and client are connected then client is provided any random port number by operating system to run and generally, we don’t care about it, But in some cases, there may be a firewall on the client side that only allows outgoing connections on certain port numbers.

Socket Tutorial, 1977, 1998 */ /* */ /* US Government Users Restricted Rights - */ /* Use, struct sockaddr_in client, server; char buf[32]; /* * Create a datagram socket in the as the * wildcard INADDR_ANY so that the server can get messages from any * of  When we use UDP sockets to communicate between two processess, will both the client/server socket be able to send/recv ? meaning can sendto()/ recvfrom() be used on both server and client? It could be useful even if anybody provide some link on socket

Comments
  • Related: stackoverflow.com/questions/11982562/…
  • And yet, I've tested the code in the blog entry and it works. If you don't explicitly state the interface to use through the addr.sin_addr.s_addr how does the sockets API know which interface to attempt connecting through?
  • INADDR_ANY is normally defined as 0. That is the IP address 0.0.0.0. RFC 1122 says that means "This host on this network". The linux IP stack seems to just route this to the loopback interface. (e.g. try ping 0.0.0.0 or even just ping 0). I'd say the author made a typo, and should have used INADDR_LOOPBACK.
  • @nos: Well, some version of the Linux IP stack is doing that. But it's not standard, and other OSes do return an error.
  • nos - This is really the answer I was looking for. If you move your comment to an answer I will mark it as such.
  • The usage in the question is not with client-side bind(), which I agree is rare but sensible. The question is passing INADDR_ANY to connect().