What is the difference between Socket and ServerSocket?

前端 未结 9 1355
遇见更好的自我
遇见更好的自我 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:25

    java.net.ServerSocket

    This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.

    java.net.Socket

    This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.

    0 讨论(0)
  • 2021-02-01 02:29

    (I post this answer because I always feel it's important to make the logic right.)

    I suggest you take a look at the following sample.

    http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

    Admittedly, when carrying out TCP/IP communication, all the necessary information can be provided by the Socket class alone for the sole purpose of communication. No matter it is on server side or client side.

    As you can see from the above link, server side use the following code to acquire its own Socket instance. That is, another socket is created on the same server local port and the client port pair.

    enter image description here

    Then, server use this Socket instance to talk to the client.

    And to make the picture complete, below code snippet shows client's Socket instance.

    enter image description here

    So if Socket can do it all already, why do we still need the ServerSocket?

    This is because of the working paradigm of communication over TCP/IP protocol.

    When 2 programs talk over TCP/IP, usually one will passively listen/wait on a <IP:port> and the other one will actively connect to it.

    So you can see, at this very starting phase of the communication, the 2 sides have very different behaviors. So 2 different classes are used to reflect this difference.

    • Socket class encapsulates the behavior of the active side. (a.k.a. the client)
    • ServerSocket class encapsulates the behavior of the passive side (a.k.a. the server)

    Once the ServerSocket accomplished its listening task and detected an incoming connection, it will accept() it and create a new Socket instance to facilitate the communication.

    Similarily, in java.nio package, you will find ServerSocketChannel and SocketChannel classes. And still, they behave like this:

    ServerSocketChannel -------------> SocketChannel
                          accept()
    

    So, to some extent, I agree with @JohnK as he pointed out in the comment, it's more or less just a 6-letter difference.

    0 讨论(0)
  • 2021-02-01 02:29

    Take a look at http://java.sun.com/docs/books/tutorial/networking/sockets/

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 02:38

    Socket is for the client side and ServerSocket is for the server side.

    0 讨论(0)
  • 2021-02-01 02:41

    ServerSocket is again a Socket with additional features of server endpoint. The server features includes listening to the port and accepting an incoming connection etc...

    0 讨论(0)
提交回复
热议问题