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?
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.
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.
It's because client always have to connect to the server. Client can be called as slave and server is always the master. But server can speak to the client only if there is the particular code given such Buffered Inputstream. other wise it can't send any input to client even if there is a connection. Port number is basically the address of the program stored in the server computer. so when the program in client side has to contact server side it needs port number so server side doesn't needs client's port number as the client program itself wants to access server program.