What is a socket

Source: Internet
Author: User
Tags socket port number

Socket or socket is also an abstraction of the form of software that expresses the "terminal" of a connection between two machines. There is a "socket" on each machine for a particular connection, and you can imagine a virtual "cable" between them. Each end of the cable is inserted into a "socket" or "socket". Of course, the physical hardware and cable connections between machines are completely unknown. The basic tenet of abstraction is that we don't have to know the details as much as possible.
In Java, we create a socket that uses it to establish a connection to another machine. The result from the socket is a inputstream and OutputStream (reader and writer, respectively, if the appropriate converter is used), to treat the connection as an IO stream object. There are two socket classes based on data streams: ServerSocket, the server uses it to "listen" for incoming connections, and the socket where the client uses it to connect initially. Once a client (program) requests to establish a socket connection, ServerSocket returns (via the Accept () method) a corresponding server-side socket for direct communication. From then on, we get a real "socket-socket" connection, and you can treat both ends of the connection in the same way, because they are the same! getInputStream () and Getoutputstream () can be used to generate the corresponding InputStream and OutputStream objects from each socket at this time. These data streams must be encapsulated within the buffer. You can format a class in the way described in chapter 10th, as you would any other stream object.
For the naming mechanism of the Java library, the use of ServerSocket (server sockets) is undoubtedly another example of confusion. You may think that ServerSocket is best called "Serverconnector" (server Connector), or other name, just don't install a "Socket" in it. It may also be assumed that both ServerSocket and sockets should inherit from some generic base class. In fact, these two types do contain several common methods, but they are not qualified to assign them to a generic base class. Instead, ServerSocket's main task is to wait patiently for other machines to connect to it, and return to an actual socket. This is where the name "ServerSocket" is misplaced, because its goal is not to really be a socket, but to produce a socket object when someone else is connected to it.
However, ServerSocket does create a physical "server" or a listening socket on the host. This socket listens for incoming connections and then uses the Accept () method to return a "built" socket (both local and remote endpoints are defined). The easy to confuse is that these two sockets (listening and established) are associated with the same server socket. A listening socket can receive only new connection requests and cannot receive actual packets. So although ServerSocket does not have much meaning for programming, it is "physical".
When you create a serversocket, you only need to give it a port number. You don't have to assign an IP address to it because it's already on the machine that you're representing. However, when you create a socket, you must give both the IP address and the port number to which you want to connect (on the other hand, the socket returned from serversocket.accept () already contains all this information).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.