Deep understanding of java socket communication

sketch

In Java, sockets are divided into ordinary sockets and niosockets. Here we introduce sockets.

We can compare socket to the means of transportation between two cities. With it, we can shuttle back and forth between the two cities. There are many kinds of means of transportation, and each means of transportation also has corresponding traffic rules. Socket is the same. There are many kinds. In most cases, TCP / IP stream socket is used, which is a stable communication protocol. (comparison between TCP / IP and UDP)

Network communication in Java is realized through sockets. Sockets are divided into ServerSocket and socket. ServerSocket is used by the server to listen to requests through the accept method. After listening to requests, it returns to the socket. Socket is used to complete data transmission. The client directly uses socket to initiate requests and transmit data.

The use of ServerSocket can be divided into three steps:

1. Create ServerSocket. There are five construction methods of ServerSocket. Usually, ServerSocket (int port) is used, and only port number (port) is required.

2. Call the accept method of the created ServerSocket to listen. The accept method blocks the method, that is, after calling the accept method, the program will stop and wait for the connection request. The program will not go down until the request is received. After receiving the request, the accept method will return a socket.

3. Use the socket returned by the accept method to communicate with the client.

Chestnuts

Client:

Server:

details

Listening request:

When a new socket request arrives, a new socket data structure will be created for the connection. The information of the socket data contains the address and port of the formal request source address and port. The newly created data structure will be associated with an incomplete connection data structure list of the ServerSocket instance. Note that at this time, the corresponding socket instance of the server has not been created, but the socket instance of the server will not return until three handshakes with the client are completed, and the data structure corresponding to the socket instance will be moved from the unfinished list to the completed list.

Data transmission:

When the connection has been established successfully, both the server and the client will have a socket instance. Each socket instance has an InputStream and OutputStream, and exchange data through these two objects.

You should know that network I / O is transmitted in byte stream. When a socket object is created, the operating system will allocate a certain size of cache for InputStream and OutputStream respectively. Data writing and reading are completed through this cache.

The writer writes the data to the sendq queue corresponding to the OutputStream. When the queue is full, the data will be transferred to the recvq queue of the InputStream at the other end. If the recvq is full at this time, the write method of the OutputStream will be blocked until the recvq queue has enough space to accommodate the data sent by sendq. The process is shown in the figure below:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>