Official Reasons For "Software Caused Connection Abort: Socket Write Error"

Official reasons for Software caused connection abort: socket write error

This error can occur when the local network system aborts a
connection, such as when WinSock closes an established connection
after data retransmission fails (receiver never acknowledges data sent
on a datastream socket).

See this MSDN article. See also Some information about 'Software caused connection abort'.

java.net.SocketException: Software caused connection abort: socket write error when resubmitting the request

You are closing the client connection before the client is done. Try this in your Server class:

@Override
public void run()
{
try (BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); BufferedWriter output = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream())))
{
while (clientSocket.isConnected())
{
String req = getRequest(reader);
setResponse(output, req);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
clientSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Software caused connection abort: socket write error

I'm not going to try to read your code in detail (see comment above ...), but there is clearly something very strange / wrong about it.

Put simply, if you are going to talk to an HTTP-based server, you can't just open a socket and write stuff. Your client has to create well-formed HTTP requests, and process the HTTP responses that come back.

The exception on the client-side is happening because the server side ... in fact YOUR CODE ... has closed the connection at the other end.

java.net.SocketException: Software caused connection abort: socket write error

Please refer to the answers to Official reasons for "Software caused connection abort: socket write error"

EDIT

I don't think there is much more that can be said in general, and there doesn't appear to be anything unusual about your code that would cause connections to abort. I would however note that:

  • Casting the bytes to integers for the write call is unnecessary. It will be promoted automatically.
  • It would be better (simpler, potentially more efficient in terms of network traffic) to use write(byte[]) instead of write(int).
  • The receiving side is assuming that each byte represents a complete character. This may be incorrect depending on how the sending side formed the bytes to be transmitted, and
  • It would be a good idea to start by sending a byte count so that the receiving end can tell if something has gone wrong before the sender sent the whole byte array.

Software caused connection abort: socket write error on clearing the app from recent list even (it running in a different thread from a service)

It works now. Just needed to make the service work in Foreground. Making it Foreground didn't work before because of a silly mistake, I was doing it in another thread but forgot to execute it (didn't call .start()).



Related Topics



Leave a reply



Submit