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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With