从java中的套接字断开连接

发布于 2024-11-04 02:11:04 字数 580 浏览 1 评论 0原文

你好 我正在构建一个小 p2p 程序,实现服务器端和客户端。 当我使用客户端程序时,首先想到它的作用是连接到其列表中的每个服务器,发送数据(关于客户端)并断开连接。下次客户端连接到这些服务器之一时,它将被识别。

我的问题 - 当我告诉客户端断开连接时,我收到此异常

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)

以断开连接我刚刚写道:

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

那么,我如何避免此异常?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最丧也最甜 2024-11-11 02:11:04

Socket.close() 的 JavaDoc 明确指出:

关闭此套接字也将关闭
套接字的输入流和
输出流。

这将引发异常,因为您已经关闭了它们!

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!

不寐倦长更 2024-11-11 02:11:04

当您关闭客户端时,您期望服务器端发生什么?

为了避免这种异常,您需要实现自己的 readUnsignedShort() 方法,例如。

public int readUnsignedShort() {
   int ch1 = in.read();
   int ch2 = in.read();
   if ((ch1 | ch2) < 0)
//     don't throw new EOFException(); 
       return -1; // EOF marker.

   return (ch1 << 8) + (ch2 << 0);
}

When you close the client side, what would you expect to happen on the server side?

To avoid this exception, you need to implement you own readUnsignedShort() method like.

public int readUnsignedShort() {
   int ch1 = in.read();
   int ch2 = in.read();
   if ((ch1 | ch2) < 0)
//     don't throw new EOFException(); 
       return -1; // EOF marker.

   return (ch1 << 8) + (ch2 << 0);
}
帝王念 2024-11-11 02:11:04

在关闭输出流之前进行刷新是否正确?:

finally {
    //this is the DataOutputStream
    if(dout != null){
        dout.flush();
        dout.close();
    }
    //and this the OutputStream
    if(out != null){
        out.flush();
        out.close();
    }
    if (din != null){
        din.close();
    }
    if (in != null){
        in.close();
    }
    if (socket != null){
        socket.close();
    }
}

Would it be right doing a flush before closing the output streams?:

finally {
    //this is the DataOutputStream
    if(dout != null){
        dout.flush();
        dout.close();
    }
    //and this the OutputStream
    if(out != null){
        out.flush();
        out.close();
    }
    if (din != null){
        din.close();
    }
    if (in != null){
        in.close();
    }
    if (socket != null){
        socket.close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文