Java socket common exception handling and network programming issues to be aware of

Source: Internet
Author: User
Tags connection reset keep alive

In the Java Network programming socket communication, you will typically encounter the following exception conditions:  The 1th exception is java.net.BindException:Address already in Use:jvm_bind. This exception occurs when the server side operates on a new ServerSocket (port is an integer value of 0,65536). The exception is due to the fact that a port is already being started and is listening. At this point, with the Netstat-an command, you can see a port with a listending status. Just looking for a port that is not occupied will solve the problem.    2nd exception is java.net.ConnectException:Connection Refused:connect. This exception occurs when the client makes a new Socket (IP, Port) operation because the exception occurs because either the machine with the IP address cannot be found (that is, the current machine does not exist to the specified IP route), or the IP exists, but the specified port is not found for listening. This problem occurs, first check the client's IP and port is wrong, if it is correct to ping the server from the client to see if it can ping, if you can ping (the server side of the ping will need a different method), see on the server side of the program to listen to the specified port is started, This will certainly solve the problem.    3rd exception is the Java.net.SocketException:Socket is closed. This exception can occur on both the client and the server. The reason for the exception is that the local side actively shuts down the connection (called the Close method of the socket) and then reads and writes the network connection.    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 the server side, causing the exception to be two, the first of which is if the socket on one end is turned off (or closed by 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 one end exiting, but the connection is not closed when exiting, and the other end throws the exception if the data is read from the connection (Connection reset). In short, it is caused by read and write operations that are disconnected from the connection.    5th exception is Java.net.SocketException:Broken pipe. The exception is in the customerBoth end and server can occur. In the first case of the 4th exception (that is, after you throw socketexcepton:connect reset by Peer:socket write error), the exception is thrown if you continue to write the data. The solution to the first two exceptions is to first ensure that all network connections are closed before the program exits, followed by detection of the other's closed connection operation, and that the other person closes the connection after closing the connection.     two. Issues to be aware of when writing network programs:1, is to correctly distinguish between long and short connections. The so-called long-term connection refers to permanent retention once established. In the case of a short connection, prepare the data--make connection, send data, and close the connection.   Many programmers write for years of Web programs, incredibly do not know what is a long connection, what is a short connection. 2, ismaintenance of long connections。 The so-called maintenance includes two aspects, the first is to detect the other's active disconnection (that is, call the Close method of the socket), followed by the detection of the other side of the outage, abnormal exit and network is not connected. This is a robust communication program that must be available. Detection of the other side of the active disconnection is simple, the main side of the active disconnection, the other side if the read operation, the return value at this time only-1, once the other side of the detection of the disconnection, you should actively close the connection (call the socket's Close method). and detect the other side of the outage, abnormal exit and network, the common method is to use the "heartbeat", that is, the two sides periodically send data to each other, but also from the other side to receive the "Heartbeat", if a few consecutive cycles have not received the other heartbeat, you can judge the other side down, abnormal exit or network, You also need to actively shut down the connection at this time, if the client can restart the connection after a certain amount of delay.Although the socket has a keep alive option to maintain the connection, if this option is used, it typically takes two hours to discover the other party's outage, abnormal exit, and network connectivity .。 3, is the processing efficiency problem. 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 Heartbeat, writing data does not require a dedicated thread, and of course a class of threads (commonly known as worker threads) is used for message processing, which means that the receiving thread is only responsible for receiving the data and then distributing it to the worker for processing the data.if it is a short connection, you do not need to send the heartbeat of the thread, if the server also requires a dedicated thread responsible for the connection request listening。 These are the overall requirements of a communication program, specifically in your program, to see how you optimize the program. Common exception handling for Java sockets

Java socket common exception handling and network programming issues to be aware of

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.