2

(This question is inspired by a response to this thread: How WebSocket server handles multiple incoming connection requests?)

My understanding is this way:

Assume client IP = 1.1.1.1, server IP = 9.9.9.9

  1. Browser choose a random local available port, say 5555, and initiate a connection to server's port 80. So on client, the socketfd_client should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:80, TCP).

  2. Server calls accept() on its port 80 and identified the connection request from client. Then server picks a random local available port, say 8888, to fulfill that connection request. So on server, the socketfd_server should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:8888, TCP).

My question is:

If my above understanding is correct, socektfd_client and socketfd_server have different server port. Client has 80 while server has 8888. How could the communication be carried out? I think client should change to use the server port 8888 as well, but when and how?

Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 2
    The TCP connection is between client port `5555` and server port `80`. There is no port `8888`, or any other port, involved. – Ron Maupin Feb 14 '16 at 03:15
  • @RonMaupin Thanks. That's also what I thought. My confusion came from this answer. http://stackoverflow.com/questions/28516962/how-websocket-server-handles-multiple-incoming-connection-requests/35323070#35323070 I don't think his observation 3 is correct. – smwikipedia Feb 14 '16 at 04:08
  • 1
    Read the accepted answer to the question. A connection is based on both client and server IP addresses and ports, and the server has no idea what the client IP address and port are until it is contacted by the client. The connection created will be with `1.1.1.1:5555` and `9.9.9.9:80`. – Ron Maupin Feb 14 '16 at 06:42
  • 1
    @smwikipedia The answer you cited is not correct. – user207421 Feb 14 '16 at 07:38

1 Answers1

5

Browser choose a random local available port, say 5555

No. The operating system does that: specifically, the TCP part of the network stack.

and initiate a connection to server's port 80. So on client, the socketfd_client should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:80, TCP).

Correct.

Server calls accept() on its port 80 and identified the connection request from client.

Correct.

Then server picks a random local available port, say 8888

No.

to fulfill that connection request.

No.

So on server, the socketfd_server should represent an IP connection like (1.1.1.1:5555, 9.9.9.9:8888, TCP).

No. The connection at both ends is represented by {1.1.1.1:5555, 9.9.9.9:80}. There is no new port at the server end.

My question is:

If my above understanding is correct

It isn't.

socektfd_client and socketfd_server have different server port.

No.

Client has 80 while server has 8888. How could the communication be carried out? I think client should change to use the server port 8888 as well, but when and how?

Never.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483