Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing Network Communication between client Socket and Server ServerSocket in Series

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "the method of realizing network communication between client Socket and server ServerSocket in series". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "the method of realizing network communication between client Socket and server ServerSocket in series" can help you solve your doubts.

1. Class attributes

The main function of ServerSocket, as a socket on the server side, is to accept the information passed by the client socket and send the response back to the client. Its attribute is very simple, as follows:

Private boolean created = false;// has been created private boolean bound = false;// binding private boolean closed = false;// has been turned off / / all underlying functions rely on SocketImpl to implement private SocketImpl impl

ServerSocket, like Socket, relies on the capabilities of SocketImpl at the bottom, while the implementation of the underlying capabilities of SocketImpl is basically implemented by native methods.

2. Initialize

Initialization can be roughly divided into two categories: non-parametric constructors and parametric constructors.

The no-parameter constructor is relatively simple to do, specifying only SocketImpl as the SocksSocketImpl class; there are several initialization forms of the parametric constructor, so let's take a look at the source code of the constructor with the most parameters.

Public ServerSocket (int port, int backlog, InetAddress bindAddr) throws IOException {/ / defaults to SocksSocketImpl implementation setImpl (); / / the port must be greater than 0 and less than 65535 if (port)

< 0 || port >

0xFFFF) throw new IllegalArgumentException ("Port value out of range:" + port); / / if the maximum number of connections is less than 1, then the default 50 if (backlog < 1) backlog = 50; try {/ / underlying navtive method bind (new InetSocketAddress (bindAddr, port), backlog);} catch (SecurityException e) {close (); throw e } catch (IOException e) {close (); throw e;}}

The input parameter port refers to the local port that the ServerSocket needs to bind.

The input parameter backlog refers to the maximum length of the client connection queue accepted by the server. It should be noted here that the number of client connections is not limited. We have done an experiment under the JDK8 version. We set the server's backlog to 1 and slow down the server's processing speed. When the server's concurrent request comes, it is not the second request to refuse the connection. We are in actual work. It is also best not to use backlog to limit the number of client connections.

It is also important to note that when backlog is less than 1, backlog is set to the default of 50.

The input parameter InetAddress indicates the ip address.

3 、 bind

The main purpose of the bind method is to bind the ServerSocket to the local port. This method is only used when we initialize the ServerSocket with a no-parameter constructor. If we use a parameter constructor, it is already bound to the local port at initialization.

With no-parameter constructor, we usually use this:

/ / initialize ServerSocket serverSocket = new ServerSocket (); / / bind serverSocket.bind (new InetSocketAddress ("localhost", 7007)); 4. Accept

The accept method is mainly used for ServerSocket to accept sockets from the client. If there is no request from the client, this method will always block. If the timeout is set through the setSoTimeout method, then accept will only block within the timeout, and an exception will be thrown after the timeout.

The underlying bind and accept methods are both implemented by the native method, so we won't look at the source code.

5.1.What is your understanding of Socket and ServerSocket?

A: both of them can be called sockets. The underlying layer is based on the TCP/UDP protocol. Sockets encapsulate the underlying protocol to make it more convenient for us to use. Socket is often used on the client side to request data and receive responses from the server. ServerSocket is often used on the server side to accept and process requests from the client. Both of them rely on the native method of the subclass of SocketImpl.

5.2.What is your understanding of SO_TIMEOUT in SocketOptions?

Answer: the SocketOptions class has many property settings, such as SO_TIMEOUT, SO_LINGER and so on. Just talk about your own understanding of these questions.

5.3.Can I choose TCP or UDP when constructing Socket? How should I choose?

A: yes, Socket has a constructor with three parameters, and the third parameter indicates whether you want to use TCP or UDP.

Does TCP have a mechanism to automatically detect whether the server is alive? Is there a better way?

A: yes, we can activate this feature through the setKeepAlive method. If there is no communication between the socket of the client and the server within two hours, TCP will automatically send keepalive probe to the server. It is predicted that there are three situations on the server:

The server uses the expected ACK reply to indicate that everything is fine.

The server replies RST, indicating that the server is in a state of panic or restart and terminates the connection.

There is no response from the server (it will be tried several times), indicating that the socket has been closed.

However, we do not recommend this way. We can set up a scheduled task and regularly access the special interface of the server. If the data returned by the server is as expected, it means that the server is alive.

After reading this, the article "the method of realizing network communication between client-side Socket and server-side ServerSocket in series" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 220

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report