32

If a client listens on a socket, at http://socketplaceonnet.com for example, how does it know that there is new content? I assume the server cannot send data directly to the client, as the client could be behind a router, with no port forwarding so a direct connection is not possible. The client could be a mobile phone which changes it's IP address. I understand that for the client to be a listener, the server doesn't need to know the client's IP.

Thank you

2
  • 4
    A brief introduction (related to the TCP protocol).
    – Vidul
    Commented Jun 2, 2012 at 10:33
  • 3
    The fact that the connection is direct or not changes nothing. Bytes (in packets) come to your computer (it may be a buffer on your network card) and as a result a signal is sent to your software. Commented Jun 2, 2012 at 10:36

2 Answers 2

99

A client socket does not listen for incoming connections, it initiates an outgoing connection to the server. The server socket listens for incoming connections.

A server creates a socket, binds the socket to an IP address and port number (for TCP and UDP), and then listens for incoming connections. When a client connects to the server, a new socket is created for communication with the client (TCP only). A polling mechanism is used to determine if any activity has occurred on any of the open sockets.

A client creates a socket and connects to a remote IP address and port number (for TCP and UDP). A polling mechanism can be used (select(), poll(), epoll(), etc) to monitor the socket for information from the server without blocking the thread.

In the case that the client is behind a router which provides NAT (network address translation), the router re-writes the address of the client to match the router's public IP address. When the server responds, the router changes its public IP address back into the client's IP address. The router keeps a table of the active connections that it is translating so that it can map the server's responses to the correct client.

0
0

The TCP Iterative server accepts a client's connection, then processes it, completes all requests from the client, and disconnects. The TCP iteration server can only process one client's request at a time. Only when all the requests of the client are satisfied, the server can continue the subsequent requests. If one client occupies the server, other clients can't work, so TCP servers seldom use the iterated server model.

Not the answer you're looking for? Browse other questions tagged or ask your own question.