Java the difference of Socket and ServerSocket in using port

后端 未结 3 1583
独厮守ぢ
独厮守ぢ 2021-01-06 01:04

At the server side, we use

Socket server = serverSocket.accept();

to create a socket. After the socket is created, we can create a new thr

3条回答
  •  一向
    一向 (楼主)
    2021-01-06 01:13

    So from my understanding, can I conclude that, at server side, we can create multiple sockets under one port? (similar to what web server does)

    You're confusing yourself with your terminology. ServerSocket.accept() accepts a connection, and wraps the endpoint in a Socket. The endpoint has the same local port number as the ServerSocket, by definition as per RFC 793, and therefore so does the wrapping Socket.

    Actually my question is, at client side, when we are creating a socket, we can specify the local port that we want to use.

    We can, but we rarely if ever do so.

    After we have successful created a client socket at that local port, can we reuse that port for other client socket?

    No.

    Does that port bind to the socket permenantly until the socket is closed (or port close)?

    Yes, or rather the other way round: the socket is bound to the port.

    Since there is no "Listening" concept at client side, are we able to do the same thing as ServerSocket does (refer to ServerSocket can create multiple socket under one port)?

    No.

提交回复
热议问题