Java the difference of Socket and ServerSocket in using port

后端 未结 3 1587
独厮守ぢ
独厮守ぢ 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:12

    You should imagine a socket as a two-pair array of information:

    • {Self Port, Self Addr}
    • {Dest Port, Dest Addr}

    therefore a single Server may have many connections connected to it that differ by their {Dest Port, Dest Addr}

    example: Server port 10000 addr 10.0.0.1
    
    Socket 1:
    
     - {10000,10.0.0.1}
     - {10001,10.0.0.2}
    
    Socket 2:
    
     - {10000,10.0.0.1}
     - {10002,10.0.0.1} - address may seem the same but as a whole its a
       different destination
    

    hope this helps.

提交回复
热议问题