JavaSocket common exception handling
October 28, 2010
Socket Communications, the daily encounter the following exceptions:
first one exception is java.net.BindException: Addressalreadyinuse: JVM_Bin d.
The exception occurs in the offices of device side newServerSocket (port) (port is an integer value 0,65536) operation. Notice is that with the exception of the port as a port has been activated, and eavesdropping. Netstat-an then with the Royal Decree, you can see the status of a Listending port. Only to find a port that is not occupied can solve the problem.
second exception is java.net.ConnectException: Connectionrefused: connec t.
The exception occurs on the client side newSocket (ip, port) operation, the exception is generated notices or ip address of the machine with less than her inability to find (that is, from the current machine does not exist to specify the ip route) or the ip there, but could not find the specified port for monitoring. The problem occurs, check the beginning of the client ip and port is not wrong, if accurate work from the client ping the device is not able to ping to see if can ping (ping work to ban drop-side is not required of the method), then look in the offices of the specified port-side of the monitor is not the testimony of the model, then this can be determined to solve this problem.
The third exception is java.net.SocketException: Socketisclosed.
the exception on the client device and the work may generate. Notice is the unusual initiative to end the connection is closed (called Socket close method) and then connect to the collection of read and write operations.
fourth exception is java.net.SocketException: (Connectionreset or Connectresetbypeer: Socketwriteerror).
the exception on the client and server-side things are possible, cause notice of the exception of two, the first is if the Socket is closed at one end (or active or closed because of abnormal exit caused closed), the other end is still sending data, the first packet sent to stimulate the exception (Connectresetbypeer). Another is the end of the exit, but exit when the connection is not closed, the other end if the connection to read data from the Thrown (Connectionreset). Simply means that the connection is lost after the read and write operations triggered.
fifth exception is java.net.SocketException: Brokenpipe.
the exception on the client, and work devices are likely to generate. In the first four exceptions in the first case (that is thrown SocketExcepton: Connectresetbypeer: Socketwriteerror later), and then continued to write the data if the exception is thrown. The first two exceptions beginning of the solution is to ensure that all the testimonies example out of the collection before the connection closed, followed by the other side closed connection to testing operation, the invention itself should also be connection to the other side closed the connection closed.
two. Example, when writing to collect testimonies have attention problems:
1, is to accurately identify the long and short connection. Connection is the so-called long been established on a permanent hold. Connection is short, preparation of data -> Connect -> send data -> closed connection. Many members of the testimony of the model for many years wrote a collection of testimonies example, does not even know what a long connection, what is the short connection.
2, is a long connection protection. The so-called protection consists of two aspects, beginning of the initiative is to test each other disconnection (ie, calling Socket close method), followed by testing each other down, exception exit and collect unreasonable. This is a strong testimony of the communication model must have. Detect each other active disconnection is simple, even an important one-off initiative, if the other party during the read operation, then the return value of -1 only once to detect each other off with, you should take the initiative to close the end of the connection (called Socket The close method). And testing each other down, and collecting unusual exit barrier, often applied method is to use a “heartbeat”, which is on both sides periodically send data to each other, but also from each other to receive a “heartbeat”, if the last few cycles have not received to each other heartbeat, you can conclude that the other side down, exception exit or collect unreasonable at this time also need to take the initiative to close local connection, if the client can be one accurate time delay after the connection from the new proposal. Socket has a keepalive option of course to protect the connection, if using this option, the invention generally have two hours to each other down, and collecting unusual exit barrier.
3, dealing with effectiveness. Whether the client devices still work, if the model is a long testimony of at least a need to connect two threads, one for receiving data, one for sending heartbeat, do not have to write a special thread, of course, also have a class of other threads ( commonly known as the Worker thread) for message processing, that is only responsible for receiving thread to receive data, and then distributed to Worker for data processing. If it is a short connection, no need to send the heartbeat thread, if the device is also required to act in a dedicated thread responsible for listening for connection requests. These are the testimonies of a communication model of the overall request, the specific example of your testimonies, the testimonies to see you Ruo model for optimization of.
Posted: January 7th, 2012
at 7:46am by admin
Tagged with brokenpipe
Categories: Uncategorized
Comments: No comments













