What is the difference between Socket and ServerSocket?

前端 未结 9 1625
遇见更好的自我
遇见更好的自我 2021-02-01 02:10

If Socket represents client side and ServerSocket represents server side, why Socket.read reads the data from server side? I\'m really con

9条回答
  •  忘掉有多难
    2021-02-01 02:36

    First of all, let's clarify what IS Socket look like: in a common case, Socket is a concatenation of IP and port via :, for example: 127.0.0.1:8080.

    So, you decided to make client-server application using Socket. There's nothing too much complicated. Here's short explanation about making connection between client and server:

    1. First of all, let's clarify that fact, that our client have his own Socket and knows server IP address and port. For server there are provided only ServerSocket and port. In both cases port are the same number between 0 and 65535.
    2. So, we decided to connect our client to our server:

      • client creates his Socket clientSocket object with known IP and port of our server.

      • server got incoming connection request with his ServerSocket.accept() method, which generates new Socket newClientSocket object (still on a server side (!) ).

      • Further data exchanging goes via clientSocket and newClientSocket objects (not between clientSocket and ServerSocket).

    Here is almost perfect picture to understand the basic connection process (keep in mind, that Socket object on Client at this picture - same objects).

    After you've made this simple structure, you need to open two streams on both Client.clientSocket and Server.newClientSocket sides for reading and writing information.

提交回复
热议问题