Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In socket programming, why a client is not bind to an address?

In socket programming, I see that the server is bind to an address- comprising of IP and port number. Then the steps are listen, and accept ( blocking call). I am wondering why the client doesn't need to bind with its ip address and port number. In fact, it binds with the server socket address. Afterwards, the client calls the connect() call to establish a connection. Later, client and server both can speak to each other. How a server can speak to the client? The client has not bind with its IP address and port number. How is it possible for a server to reach client?

like image 891
dexterous Avatar asked Feb 08 '14 12:02

dexterous


People also ask

Why do we need not bind the address to a socket for a client program?

Because there is an internal bind() as part of connect(), if the socket isn't already bound, and because the server doesn't care what the client's port number is: it doesn't need to be fixed like the server's port number.

Why does socket bind fail?

If you're seeing a "TCP/UDP: Socket bind failed on local address" error message in the OpenVPN log, it means your VPN connection is configured to bind to a fixed local address and/or port number, and that this address/port number is unavailable.

Why do we need to call bind () in server code and not in the client code?

For incoming connections, you have to bind to a known port so clients know where to contact you. Once they do so, they've given the server their local address/port so that communication can then flow in both directions. listen() will only work after a bind() call.


2 Answers

Choosing the client port number is usually done by the kernel. This makes sense as only the kernel knows which ports are currently in use. However, you are free to specify the client port on your own, but I think it is hard for client program to make sure the port is free. It could just "try an error"

Choosing the clients IP address is also usually done by the kernel, by consulting the IP routing tables. You can show and modify them using the route command.


You've asked how the server can know the clients IP address. You need to understand TCP/IP in order to answer your question. Read the wiki article. It should be a good starting point.

like image 96
hek2mgl Avatar answered Sep 27 '22 23:09

hek2mgl


I am wondering why the client doesn't need to bind with its ip address and port number.

Because there is an internal bind() as part of connect(), if the socket isn't already bound, and because the server doesn't care what the client's port number is: it doesn't need to be fixed like the server's port number.

In fact, it binds with the server socket address.

No it doesn't: it connects to the server's address.

How a server can speak to the client?

Over the connection that has been established.

The client has not bind with its IP address and port number.

Yes it has, see above.

How is it possible for a server to reach client?

Same way the client reaches the server. By sending a packet with a target address and port.

like image 30
user207421 Avatar answered Sep 27 '22 23:09

user207421