java.net.SocketException how best to use it? This requires us to first understand the relevant issues related to the language. Hope that helps. Then we take a look at the java.net.SocketException of relevant knowledge.
The first one exception is java.net.BindException: Address already in use: JVM_Bind.
The exception occurred on the server for new ServerSocket (port) (port is an integer value 0,65536) operation. The reason is that with the exception of the port as a port has been started, and eavesdropping. At this point with the netstat-an command, you can see a Listending state port. Just looking for a port can not be occupied to solve this problem.
- Java.net package using the Java JDK in the UDP protocol control
- Crawl through Java.net.Socket class content
- Crawl through the java.net.Socket class content
- Establish two-way communication through the Java.net package
- HTTP server to access the reproduction of functional classes java.net
2nd exception is java.net.SocketException: Connection refused: connect.
The exception occurs on the client for new Socket (ip, port) operation, because the exception occurs or the machine with ip address can not be found (ie from the current machine does not exist to specify the ip route), or the ip exists, but can not find the specified port for listening. The problem occurs, first check the client ip and port there is a mistake, and if correct click the server from the client to see if ping ping pass, pass if they can ping (service server-side to cut out the need to ping the other way), then look at the server side of the monitor whether the procedures specified port start, this would certainly solve the problem.
Article 3 exception is java.net.SocketException: Socket is closed,
The exception on the client and the server can occur. Anomaly is due to one's own initiative to close the connection (called the Socket of the close approach) and then to read and write operations on the network connection.
4th exception is java.net.SocketException: (Connection reset or Connect reset by peer: Socket write error).
The exception in both client and server side may occur, causing the exception for two reasons, first is that if one end of the Socket is closed (or take the initiative to shut down or quit because of abnormalities caused by the closure), the other side is still sending data , sent the first data packet triggered the exception (Connect reset by peer). Another is the end of the exit, but did not close the connection when out of the other side if the data is read from the connection throws the exception (Connection reset). Simply put, that is disconnected after the read and write operation caused.
Article 5 exception is java.net.SocketException: Broken pipe.
The exception on the client and server are possible. 4 abnormalities in the first case (that is, throw SocketExcepton: Connect reset by peer: Socket write error later), if we continue to write data is thrown the exception. The first two anomalies solution is to first make sure the program exits to close all network connections, followed by testing each other's close connection to action, find each other close the connection, we also have to close the connection.
Write network programs require attention to issues
1 problem is to correctly distinguish the long and the short connection. The so-called long connection is maintained through the establishment of the permanent. Short connection is in the following scenarios in preparation for data -> connection -> send data -> close the connection. Many of the programmers to write web applications for many years, actually do not know what is a long connection, what is a short connection.
No. 2 problem is the maintenance of the long connection. The so-called maintenance consists of two areas, first, detection of active fault with each other (not only call the close method of Socket), followed by testing each other's downtime, and network abnormal exit barrier. This is a robust communications program must have. Testing each other's active disconnection is very simple with one initiative to break the other if the time during operation, then the return value is only -1, once detected each other off with, you should take the initiative to close the side of the connection (call to Socket the close method).
The detection of the other side of the downtime, and network abnormal exit barrier common method is to use "heart", that is, the two sides periodically send data to each other, but also from the other party to receive "heartbeat", if not received in successive cycles other heartbeat, you can judge each other or abnormal release or network downtime or unreasonable, at this time also need to take the initiative to close one's own connection, if the client can be re-initiated after a certain time delay connections. Although there is a Socket keep alive option to maintain the connection, if using this option, it normally takes two hours to find each other's downtime, and network abnormal exit barrier.
The first three issues to deal with efficiency. Whether client or server, if it is a long process to connect at least two threads, one for receiving data, one for sending heartbeat, do not need a special thread to write data, of course, also need a class of threads (commonly known as Worker thread ) used for message processing, that is only responsible for receiving the data reception thread, and then distributed to Worker for data processing. If it is a short connection, you do not need to send the heartbeat of the thread, if the server also need a special thread for listening for connection requests. These are the overall requirements of a communication process, specifically how to design your program, it depends on the level of your own design.