Java.net. SocketException: Software caused connection abort: recv failed exception Analysis

Source: Internet
Author: User
Tags connection reset

Keyword: recv failed

Java.net. SocketException: Software caused connection abort: recv failed
At java.net. SocketInputStream. socketRead0 (Native Method)
At java.net. SocketInputStream. read (fig. java: 129)
At java.net. SocketInputStream. read (fig. java: 182)

Clients <--> servers communicate with each other through a persistent socket connection. The client has a receiving thread in the while (true ){..} the client continuously reads data from the InputStream stream. The client sends a heartbeat packet every several seconds. If the number of consecutive heartbeat packets has reached the specified number, the client considers the link to be abnormal, close the socket, the server will throw java.net. socketException: Connection reset by peer is abnormal, and then the thread allocated to this socket Connection exits, then the client is in the while (true ){..} loop. when reading the stream, the java.net will be sent. socketException:
Software caused connection abort: recv failed exception.

 

Try {
String host = getCodeBase (). getHost ();
Byte [] response = new byte [50500];

Host = "192.168.2.148 ";
ClientSocket = new Socket (host, 16555 );
// The input and output streams are required.
Outbound = new DataOutputStream (clientSocket. getOutputStream ());

Inbound = new DataInputStream (clientSocket. getInputStream ());

// Send a request to the server
Ps = new PrintStream (outbound );

Byte [] cmd = new byte [1, 1024];

Cmd [0] = 0x7e;
Cmd [1] = 0x00;

Ps. write (cmd,); // The first sending
Ps. flush ();

Inbound. Read (response, 0, 50500); // No exception is reported here

CMD [0] = 0x7e;
CMD [1] = 0x01;

PS. Write (CMD,); // The second sending
PS. Flush ();

If (inbound. Read (response, 0, 50500) =-1) // an exception is reported here
Return false;
Return true;
}
Catch (ioexception IOE ){
System. Out. println ("ioexception:" + IOE );
Return false;
}

Related Article

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.