mgaor

October 31, 2010
collection of programming in java Socket communication, the weekdays will be closed on the following exceptions:
first one exception is java.net.BindException: Addressalreadyinuse : JVM_Bin d.
The exception generated in the side newServerSocket (port) (port is an integer value 0,65536) control time. 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 resolve the title of.
second exception is java.net.ConnectException: Connectionrefused: connec t.
The exception occurs on the client side newSocket (ip, port) control, 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. Appear in the title, check the beginning of the client ip and port is not wrong, it is assumed accurate ping from the client side of the monitor about the testimony of the specified port model is not activated, the determination to solve this title.
The third exception is java.net.SocketException: Socketisclosed.
the exception on the client and are likely to arise. Notice is the unusual side closed the connection active (call the close method of the Socket) and then connect to the collection of read and write control.
fourth exception is java.net.SocketException: (Connectionreset or Connectresetbypeer: Socketwriteerror).
The exception in both the client and the client may have led to notice the exception of two, the first is to assume that the Socket is closed at one end (or take the initiative to shut down or quit because of abnormalities caused by close ), the other end is still sending data, the first packet sent to stimulate the exception (Connectresetbypeer). Another is the end of the exit, but did not close the connection when exiting the other end of the connection is assumed to read data from the Thrown (Connectionreset). Simply means that the connection is lost after the read and write control triggered.
fifth exception is java.net.SocketException: Brokenpipe.
the exception on the client and are likely to generate. In the first four exceptions in the first case (that is thrown SocketExcepton: Connectresetbypeer: Socketwriteerror later), assuming continued to write data is re-thrown. The first two exceptions to ensure the beginning of the solution is to close all testimonies example out of the collection of connections, followed by testing each other close connection to control, the other side closes the connection after the invention itself should close the connection.
two. Written testimony of role models need to carefully collect the title:
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, preparing data -> Connect -> send data -> close the 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 solid example of the necessary communication with the testimonies. Detect each other active disconnection is simple, even an important one-off initiative, the other party assumes control during reading, then the return value is -1 only once to detect each other off with, you should take the initiative off the side 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”, assuming that last a few cycles are not closed heartbeat to the other side, you can sever the other down, or collect abnormal exit barrier, this time also need to take the initiative to close the client connection, the client can assume that one accurate time delay after the connection from the new proposal. Although there is a Socket connection keepalive option to protect, it is assumed that this option, the invention generally have two hours to each other down, and collecting unusual exit barrier.
3, dealing with the effectiveness of the title. Whether the client still is assumed to be a testimony of a long connection example at least have two threads, one for receiving data, one for sending heartbeat, do not have to write a special thread, although other classes also have a thread (commonly known as Worker thread) for message processing, that is only responsible for receiving thread to receive data, and then distributed to Worker for data processing. Connection is assumed to be short, no need to send the heartbeat thread, assuming that is also required is 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


Categories: Uncategorized

Comments: No comments


Think E-Cigarette, Think Kimree!