java. net. Socketexception four exception solutions – turn

java. net. How can socketexception be better used? This requires us to understand the relevant problems about the language first. I hope you can help. Then let's take a look at the relevant net. Relevant knowledge of socketexception.

The first exception is Java net. BindException:Address already in use: JVM_ Bind。

This exception occurs when the server performs a new ServerSocket (port) (port is an integer value of 065536). The reason for the exception is that a port like port has been started and listens. At this time, use the netstat – an command to see a port in listing status. You only need to find a port that is not occupied to solve this problem.

The second exception is Java net. socketException: Connection refused: connect。

This exception occurs when the client performs a new socket (IP, port) operation, The reason for this exception is that either the machine with the IP address cannot be found (that is, the specified IP route does not exist from the current machine), or the IP exists but the specified port cannot be found for listening. If this problem occurs, first check whether the IP and port of the client are written incorrectly. If it is correct, Ping the server from the client to see if it can be pinged. If it can be pinged (another method is needed to disable Ping on the service server). It depends on whether the program listening to the specified port on the server is started. This can certainly solve this problem.

The third exception is Java net. socketException: Socket is closed,

This exception can occur on both the client and server. The reason for the exception is that the user actively closes the connection (calls the close method of the socket) and then reads and writes the network connection.

The fourth exception is Java net. Socketexception: (connection reset or connect reset by peer: socket write error).

This exception may occur on both the client and the server. There are two reasons for this exception. The first is that if the socket at one end is closed (or actively closed or closed due to abnormal exit), the other end still sends data, and the first packet sent raises this exception (connect reset by peer). The other is that one end exits without closing the connection. If the other end is reading data from the connection, this exception will be thrown (connection reset). In short, it is caused by read and write operations after the connection is disconnected.

The fifth exception is Java net. socketException: Broken pipe。

This exception can occur on both the client and server. In the first case of the fourth exception (that is, after socketexcepton: connect reset by peer: socket write error is thrown), if you continue to write data, this exception will be thrown. The solution to the first two exceptions is to first ensure that all network connections are closed before the program exits. Secondly, check the other party's closing connection operation. If you find that the other party closes the connection, you should also close the connection yourself.

Problems needing attention when writing network program

The first problem is to correctly distinguish between long and short connections. The so-called long connection is permanent once established. Short connection is to prepare data - > establish connection - > send data - > close connection in the following scenarios. Many programmers have written network programs for many years, but they don't know what is a long connection and what is a short connection.

The second problem is the maintenance of long connections. The so-called maintenance includes two aspects, The first is to detect the active disconnection of the other party (call the close method of the socket), and then detect the other party's downtime, abnormal exit and network failure. This is a must for a robust communication program. It is very simple to detect the other party's active disconnection. One party is actively disconnected, and if the other party is reading, the return value at this time is only - 1. Once the other party's disconnection is detected, it should actively close its own connection Connect (call the close method of socket).

The common method to detect the other party's downtime, abnormal exit and network failure is to use "heartbeat", that is, both parties periodically send data to the other party and receive "heartbeat" from the other party. If the other party's heartbeat is not received for several consecutive cycles, it can be judged that the other party is down or abnormally launched or the network is not connected. At this time, it is also necessary to actively close its own connection, If it is a client, it can re initiate the connection after a certain delay. Although the socket has a keep alive option to maintain the connection, it usually takes two hours to find the other party's downtime, abnormal exit and network impassability.

The third problem is dealing with efficiency. Whether it is a client or a server, if it is a long connection, a program needs at least two threads, one for receiving data and one for sending heartbeat. No special thread is required for writing data, Of course, another type of thread is required (commonly known as worker thread) is used to process messages, that is, the receiving thread is only responsible for receiving data and then distributing it to workers for data processing. If it is a short connection, it does not need to send a heartbeat thread. If it is a server, it also needs a special line to listen to connection requests. These are the overall requirements of a communication program. What are the specific requirements So designing your program depends on your own design level.

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
分享
二维码
< <上一篇
下一篇>>