How the clients (client sockets) are identified?

前端 未结 5 1701
别那么骄傲
别那么骄傲 2020-12-25 08:34

To my understanding by serverSocket = new ServerSocket(portNumber) we create an object which potentially can \"listen\" to the indicated port. By clientSo

5条回答
  •  臣服心动
    2020-12-25 08:52

    Every TCP connection has as identifier the quadruple (src port, src address, dest port, dest address).

    Whenever your server accepts a new client, a new Socket is created and it's indipendent from every other socket created so far. The identification of clients is not implictly handled somehow..

    You don't have to think sockets as associated to "clients", they are associated with an ip and a port, but there is not direct correlation between these two.

    If the same client tries to open another socket by creating a new one you'll have two unrelated sockets (because ports will be different for sure). This because the client cannot use the same port to open the new connection so the quadruple will be different, same client ip, same server ip, same server port but different client port.

    EDIT for your questions:

    1. clients don't specify a port because it's randomly choosen from the free ones (> 1024 if I'm not wrong) from the underlying operating system
    2. a connection cannot be opened from a client using the same port, the operating system won't let you do that (actually you don't specify any port at all) and in any case it would tell you that port is already bound to a socket so this issue cannot happen.
    3. whenever the server receives a new connection request it's is considered new, because also if ip is the same port will be different for sure (in case of old packet resend or similar caveats I think that the request will be discarded)

    By the way all these situations are clearly explained in TCP RFC here.

提交回复
热议问题