java.net.SocketException: socket closed TCP Client Server Communication [duplicate]
Asked Answered
S

1

6

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?

Standin answered 2/4, 2013 at 16:14 Comment(2)
You should really add some code and the stacktrace...Knotting
Make sure that you are closing the connection at one of the end , if this is not the case than i am sure you closing the Input or Output Stream , do not close them until you are sure that you data communication is ended and you flush() method whenever you want to send data to socketOla
H
19

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.

Highclass answered 3/4, 2013 at 2:57 Comment(3)
I only close the buffer that sents the file, which is the one that reads the outPutStream of the Socket. Thats the only one I am thinking that is closing the Socket. If I dont close the buffer though, the client does not receive the file.Standin
@Standin So you closed the socket. Then later you got a 'socket closed' exception, because you kept using the closed socket. You will have to open a new one.Highclass
Actually yes. That was the problem. I replaced the buffer reader with a DataOutputStream and after I read the file with a FileInputStream I sent it through the DataOutputStream. I only have to close the FileInputStream now to finish the "transfer". Thank you! :)Standin

© 2022 - 2024 — McMap. All rights reserved.