Ava.net.SocketException How to use it better. This requires us to first understand the relevant questions about the language. I hope we can help. So let's take a look at the relevant knowledge about java.net.SocketException.
The 1th exception is java.net.BindException:Address already in Use:jvm_bind.
The exception occurs when the server side makes a new ServerSocket (port is a 0,65536 integer value) operation. The reason for the exception is that one of the ports like Port has been started and is listening. With the Netstat–an command, you can see a port with a listending state. You can solve this problem by looking for a port that is not occupied. Using the Java JDK Java.net Packet Control UDP protocol to crawl Web content through the Java.net.Socket class to crawl Web content through the Java.net.Socket class to establish bidirectional communication via java.net packet The Imitation java.net function class used to access HTTP servers
The 2nd exception is Java.net.SocketException:Connection Refused:connect.
This exception occurs when the client makes a new Socket (IP, port) operation because either the machine with an IP address cannot be found (that is, it does not exist from the current machine to the specified IP route), or the IP exists, but the specified port is not found for listening. This problem occurs, first check whether the client's IP and port is wrong, if correct, ping the server from the client to see if it can ping, if you can ping (service server to the ping ban will need another way), then look at the server to listen to the specified port program to start, This will certainly solve the problem.
The 3rd exception is the Java.net.SocketException:Socket is closed,
This exception can occur on both the client and the server. The exception is due to the active shutdown of the connection (called the Close method of the socket) and read and write to the network connection.
The 4th exception is java.net.SocketException: (Connection reset or connect reset by Peer:socket write error).
This exception can occur on both the client and server side. This exception is caused by two, the first is that if the socket at one end is closed (or if the active shutdown or shutdown due to an unexpected exit), the other end still sends the data, and the first packet that is sent throws the exception (Connect reset by Peer). The other is an exit, but the connection is not closed when it exits, and the other end throws the exception if the data is read from the connection (Connection reset). Simply speaking, it is caused by read and write operations after a disconnected connection.
The 5th exception is Java.net.SocketException:Broken pipe.
This exception can occur on both the client and the server. In the first case of the 4th exception (that is, after you throw the Socketexcepton:connect reset by Peer:socket write error), the exception is thrown if you continue to write the data. The first two exceptions are to make sure that all network connections are turned off before the program exits, and that the next step is to detect the other person's shutdown connection and to close the connection itself after it closes.
Issues to be aware of when writing network programs
The 1th problem is to correctly differentiate between long and short connections. The so-called long connection is permanently maintained once it is established. The short connection is in the following scenario, prepares the data-> establishes the connection-> sends the data-> closes the connection. Many programmers have been writing web programs for years, not knowing what a long connection is and what a short connection is.
The 2nd issue is the maintenance of long connections. The so-called maintenance includes two aspects, the first is to detect the other side of the active disconnected (both the call socket Close method), followed by the detection of each other's downtime, abnormal exit and network impassability. This is a robust communication program must have. Detection of each other's active disconnected is very simple, the main side of the active disconnected, the other side if the read operation, then the return value of only 1, once detected that the other disconnected, you should actively shut down the connection (call the socket Close method).
and detect each other's downtime, abnormal exit and network is commonly used by the "Heartbeat", that is, the two sides periodically send data to each other, but also received from the other "heartbeat", if the number of consecutive cycles did not receive the other heartbeat, you can judge the other or the outage or abnormal launch or the network is not through, At this point, you also need to actively close your own connection, if the client can be delayed after a certain time to initiate the connection. Although the socket has a keep alive option to maintain the connection, if you use this option, it typically takes two hours to discover each other's downtime, abnormal exit, and network impassability.
The 3rd issue is dealing with the issue of efficiency. Whether it's a client or a server, if it is a long connection a program requires at least two threads, one for receiving data, one for sending heartbeats, writing data without a dedicated thread, and, of course, a class of threads (commonly known as worker threads) for processing messages, which means that the receiving thread is only responsible for receiving data , and then distribute it to the worker for data processing. If it is a short connection, you do not need to send a heartbeat of the thread, if the server also requires a dedicated thread responsible for the connection request of the listener. These are the overall requirements of a communications program, specifically how to design your program, it depends on your own design level.