java.net.SocketException: socket closed TCP Client Server Communication

lemanou picture lemanou · Apr 2, 2013 · Viewed 51.7k times · Source

I am creating a java client/server application over TCP where we have two sockets:

  • One is for exchanging messages.
  • Two is for file transfer.

I have created in the Server two ServerSockets in order to create Socket One and Two
by accepting the ServerSockets.

At first the client sends some bytes through the first Socket
so that it can tell the Server which file it needs.

Then the Server through the second socket sends the file to the client.
After the client receives the file tries to send back to the server a Transfer Done Message.

There I get the Exception for the closed socket.
However I never close the socket up until now.
I only close the buffer which sents the file.
Even if I try to open again the socketInputStream after I sent the file the error is still the same.
Also if I do not close the buffer which sents the file, the client does not get the file.

Server Exception

Error in Return Message - java.net.SocketException: socket closed

Client Exception

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

What am I doing wrong?

Answer

user207421 picture user207421 · Apr 3, 2013

A 'Socket closed' exception means that the application that caught the exception closed the socket and then kept trying to use it. You may be unaware that closing the input or output stream of a socket closes the other stream and the socket as well. For 'software caused connection abort' see the duplicate link.