Java Socket Network Programming common exceptions (RPM)

Source: Internet
Author: User
Tags connection reset keep alive

Ext.: https://www.cnblogs.com/qq78292959/p/5085559.html

1.java.net.sockettimeoutexception

This exception is more common and the socket times out. There are generally 2 places that will throw this: a. When a is connect, this timeout parameter is determined by the latter in connect (socketaddress endpoint, int timeout), B. and Setsotimeo. UT (int timeout), which is the time-out for setting the read. They are set to 0 which means infinity.
-----------------------------------------------------------------------------------------------------
2.java.net.bindexception:address already in Use:jvm_bind
This exception occurs when a new ServerSocket (port) or Socket.bind (socketaddress bindpoint) operation is performed on the server side.

Cause: A port that is the same as port has been enabled and is listening. At this point, with the Netstat-an command, you can see a port with a listening status. Simply finding a port that is not occupied will solve the problem.
------------------------------------------------------------------------------------------------------ 3.java.net.connectexception:connection Refused:connectThis exception occurs when the client is doing a new Socket (IP, port) or socket.connect (address, timeout) operation. Cause: The machine that specifies the IP address cannot be found (that is, from the current machine does not exist to the specified IP route), or the IP exists, but the specified port is not found for listening. Should first check the client's IP and port is wrong, if you can ping (the server side of the ping will need a different method), the server-side listening to the specified port program is started.
--------------------------------------------------------------------------------------------------------
4.java.net.socketexception:socket is closedThis exception can occur on both the client and the server side. The reason for the exception is that you have actively closed the connection (called the Close method of the socket) and read and write the network connection. ------------------------------------------------------------------------------ 5.java.net.socketexception:connection reset or connect reset by Peer:socket write error
This exception can occur on both the client and server side, causing the exception to be two, a. The first is that if one end of the socket is closed (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). B. The other is one end exiting, but the connection is not closed when exiting, and the other end throws the exception if it reads data from the connection (Connection reset). The simple thing is that the read and write operations are caused by the disconnection of the connection.   For the server, the general reason can be considered: a) the number of concurrent connections to the server exceeds its capacity, the server will be some of the connections actively down. b) in the process of data transmission, the browser or the receiving client is closed, and the server is also sending data to the client. ---------------------------------------------------------------------------- 6.java.net.socketexception:broken Pipe
This exception can occur on both the client and the server. After throwing 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. For exceptions in both cases 4 and 5, special attention needs to be paid to the maintenance of the connection.   In the case of a short connection, if it is a long connection condition, it is very easy to get an exception for improper maintenance of the connection state. Basically the long connection needs to do is: a) detect the other side of the active disconnection (the other party called the Close method of the socket). The return value at this time is-1 if the other party is doing a read operation because the other side is actively disconnected.    So once you detect a disconnection, you are actively shutting down your own connection (call the Close method of the socket). b) Detect the other side of the outage, abnormal exit and network does not pass, the general practice is heartbeat detection. The two sides periodically send data to each other, but also from the other side to receive the "Heartbeat data", if a few consecutive cycles have not received the other heartbeat, you can judge the other side or down or abnormal exit or network is not connected, at this time also need to actively close their own connection; 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. ---------------------------------------------------------------------------------------------- 7.java.net.socketexception:too many open filesCause: The maximum number of handles to open files in the operating system is limited and often occurs when many concurrent users access the server.  Because many files are loaded for each user's application server (a file handle is required for the new socket), this results in a lack of open file handles. Solution: A) Try to make the class into a jar package, because a jar package consumes only one file handle, and if not packaged, a class consumes a file handle. b) The Java GC cannot close a file handle opened by a network connection, and if no close () is executed, the file handle will persist and cannot be closed. You can also consider setting the maximum number of open sockets to control this problem. Increase the maximum number of file handles by setting the operating system as appropriate. Ulimit-a can see the system current resource limits, Ulimit-n 10240 can be modified, this modification is only valid for the current window.

Java Socket Network Programming common exceptions (RPM)

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.