How can I use java.net. socketexception better? In this case, we need to first understand the relevant questions about this language. I hope you will be helpful. Let's take a look at the knowledge about java.net. socketexception.
The first exception is java.net. bindexception: address already in use: jvm_bind.
This exception occurs when the server performs the new serversocket (port) (port is an integer value of) operation. The cause of the exception is that the same port has been started and listened. Run the netstat-An command to view the port in the listending status. You only need to find an unused port to solve this problem.
Use the Java JDK java.net package to control the UDP protocol
Capture webpage content through java.net. Socket class
Capture webpage content through java.net. Socket class
Establish two-way communication through the java.net package
The function class used to access the HTTP server. It is similar to java.net.
The first exception is java.net. socketexception: Connection refused: connect.
This exception occurs when the client performs the new socket (IP, Port) operation, the cause of this exception is that the machine with the IP address cannot be found (that is, the current machine does not exist to the specified IP address route), or the IP address exists, but the specified port cannot be found for listening. In this case, first check whether the client's IP address and port are written incorrectly. If yes, ping the server from the client to check whether the server can be pinged, if the Ping is successful (the server can disable the ping operation, you need another method). Check whether the program on the specified port of the server listener is started. This will definitely solve this problem.
The first exception is java.net. socketexception: socket is closed,
This exception can occur on both the client and server. The exception occurs when you close the connection (call the socket close method) and perform read/write operations on the network connection.
The first exception is java.net. socketexception: (Connection Reset or connect reset by peer: Socket write error ).
This exception may occur on both the client and server. There are two reasons for this exception, the first one is that if the socket at one end is closed (or the socket is closed actively or caused by abnormal exit), the other end still sends data, the first packet sent causes this exception (connect reset by peer ). The other end exits, but does not close the connection when exiting. If the other end reads data from the connection, this exception (Connection Reset) is thrown ). In short, it is caused by read and write operations after the connection is disconnected.
The first exception is java.net. socketexception: broken pipe.
This exception may occur on both the client and server. In the first case of 4th exceptions (that is, after throwing socketaskton: connect reset by peer: Socket write error), if you continue writing data, this exception is thrown. The solution to the first two exceptions is to first ensure that all network connections are closed before the program exits, and secondly, to detect the closing operation of the other party and disable the connection after the other party closes the connection.
Notes for compiling network programs
The 1st problem is that long and short connections must be correctly distinguished. The so-called persistent connection is permanently maintained once it is established. Short connections are used to prepare data, establish a connection, send data, and close the connection in the following scenarios. Many programmers have been writing network programs for many years and do not know what persistent connections are or what short connections are.
The 2nd issue is the maintenance of persistent connections. The so-called maintenance involves two aspects: first, detecting the active disconnection of the other party (both calling the socket close method), and second, detecting the other party's downtime, abnormal exit, and network failure. This is required by a robust communication program. It is very easy to detect the active disconnection of the other party. If the other party is performing a read operation, only-1 is returned. Once the other party is detected to be disconnected, you should take the initiative to close your connection (call the close method of socket ).
A common method for detecting the downtime, abnormal exit, and network disconnection of a peer is to send data to the peer periodically and receive a heartbeat from the peer ", if the heartbeat of the other party is not received for several consecutive cycles, you can determine whether the other party is down or has an abnormal launch or the network is disconnected. In this case, you also need to take the initiative to close your connection, the client can initiate a new connection after a certain delay. Although the socket has a keep alive option to maintain the connection, if this option is used, it generally takes two hours to find the other party's downtime, abnormal exit, and network failure.
3rd problems are about handling efficiency. Whether it is a client or a server, if it is a persistent connection to a program, it requires at least two threads, one for receiving data, the other for sending heartbeat, and no special thread is required to write data, of course, another type of thread (commonly known as the worker thread) is also required for message processing. That is to say, the receiving thread is only responsible for receiving data and then distributing it to the worker for data processing. For short connections, no heartbeat thread is required. For servers, a dedicated thread is required to listen for connection requests. These are the overall requirements of a communication program. How to design your program depends on your own design level.