Clarity in socket function ()

While I was reading how to make a TCP client / server connection in C, I had a doubt about the bind () function.

I read that you need this function to bind the socket that you created to the local IPEndPoint, because the client / server connection accepts a pair of sockets made using LocalIP: LocalPort, RemoteIP: RemotePort . So my questions are:

  • What happens and what does the kernel do when the client does not call bind (), but calls connect () right after creating the socket (this is a common thing, I do it in the client program too, but I don’t understand why I don’t need to bind)?

    ... and above all ...

  • Why does the server program call bind (), specifying INADDR_ANY as the LocalIP address? I read that this constant is useful for specifying a common IP address, that is, the server receives data from anywhere on the Internet. But ... is this a specification of the local IP address or indicates where clients can get to the server? I am very confused ...

+4
source share
2 answers

1) Usually you need to call Bind if you want to create a server socket. There are some cases when you need to install a client socket, but more often it is not necessary for client sockets. If you want to wait for incoming connections on a specific port, you need to bind it. If you want to connect to some IP and ports, there is no need to bind them. Server socket binding provides exclusive access to the TCP port. Nothing else can connect to the network and bind to this port until your application closes or the socket is closed by you.

2) You indicate which IP address on the local computer you want to associate. One computer can have many IP addresses. Your computer may have a wired and wireless connection. Each of them has its own IP address in the local network. You can bind to one of these IP addresses and not to the other. You can even have one application bound to port 473 (for example) on one IP address and a completely different application connected to port 473 on another IP address. If you specify INADDR_ANY, you bind to all valid IP addresses that the machine has. Therefore, it does not matter which IP address the client used for you, it will work.

+3
source

What happens and what does the kernel do when the client does not call bind (), but calls connect () right after creating the socket (this is a common thing, I do it in the client program too, but I don’t understand why I don’t need to bind)?

When you create an outbound connection without first binding the socket to an IP / port, the kernel automatically selects the source IP address and port based on the routing tables and available ports.

Why does the server program call bind (), specifying INADDR_ANY as the LocalIP address? I read that this constant is useful for specifying a common IP address, that is, the server receives data from anywhere on the Internet. But ... is this a specification of the local IP address or indicates where clients can get to the server? I am very confused ...

What you read is inaccurate - the IP address in sockaddr passed to bind() does not indicate where the server will accept connections from. It indicates which local IP addresses the socket is connecting to. INADDR_ANY indicates that you want to listen for connections on the specified port on all IP addresses attached to the machine. On servers with multiple IP addresses, it is often useful to specify one IP address on bind() so that other sockets can be bound to the same port on different IP addresses. It is also often useful to bind to a port only on the local host.

+4
source

Source: https://habr.com/ru/post/1412162/


All Articles