Setting source port on a Java Socket?

后端 未结 5 1387
自闭症患者
自闭症患者 2021-02-20 09:03

I\'m very new to socket programming:

Is it possible to explicitly set the the source port on a Java Socket?

I am working on a client/server application in which

5条回答
  •  遥遥无期
    2021-02-20 10:00

    It usually goes like this:

    First, the Server opens a ServerSocket on a well known port and waits for input.

    Meanwhile the Client opens a (client) Socket with the servers hostname and this well known port address. It sends a request message to the server to initialize a communication session.

    The server receives the message, spawns a worker thread, which opens another ServerSocket on a different port and the server sends a response, where it tells the client this port number.

    Now the client closes the actual connection and creates a new Socket, now with the port number he has just been told.

    This way, the server can handle more then one client at a time, because each client gets his individual 'connection' (port).

提交回复
热议问题