Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disconnecting from a socket in java

Tags:

java

sockets

Hi i am building a little p2p program, implementing both the server-side and the client-side. when I lunch the client-side program, first think it does is to connect to each server in its list, send data (about the client-side) and disconnect. The next time the client-side connects to one of these servers it will be recognized.

My problem - when i tell the client-side to disconnect, i get this exception

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at oop.ex3.nameserver.NameServerThread.run(NameServerThread.java:24)

to disconnect i just wrote:

 finally {
        out.close();
        in.close();
        socket.close();
    }

so, how do i avoid this exception? thanks!

like image 859
yotamoo Avatar asked Apr 26 '11 16:04

yotamoo


1 Answers

The JavaDoc for Socket.close() states clearly:

Closing this socket will also close the socket's InputStream and OutputStream.

which will throw the exception since you've already closed them!

like image 127
Liv Avatar answered Sep 19 '22 23:09

Liv