Java ObjectInputStream 挂起

发布于 2024-12-07 17:36:24 字数 584 浏览 5 评论 0原文

我现在感觉真的很愚蠢,伙计们……基本上我是通过本地计算机上的 TCP 连接的……当我尝试在客户端创建输入/输出流时,它不会通过创建对象输入流。什么给?这在打印 2 后停止...没有异常或任何东西...这不是我第一次使用这个类,这部分是我感到困惑的原因。

try {
            System.out.println("1");
            mySocket = new Socket("localhost", 11311);
            System.out.println("12");
            oos = new ObjectOutputStream(mySocket.getOutputStream());
            System.out.println("2");
            ois = new ObjectInputStream(mySocket.getInputStream());
            System.out.println("13");

        } catch (Exception e) {
            e.printStackTrace();
        }

I am feeling really stupid right now guys.... basically I am connecting over TCP on a local machine... and when I try to make the In/out streams at the client it wont get passed creating the object input stream. What gives? This stops after printing 2... no exceptions or anything... This isn't the first time I've used this class which is partialy why I am puzzled.

try {
            System.out.println("1");
            mySocket = new Socket("localhost", 11311);
            System.out.println("12");
            oos = new ObjectOutputStream(mySocket.getOutputStream());
            System.out.println("2");
            ois = new ObjectInputStream(mySocket.getInputStream());
            System.out.println("13");

        } catch (Exception e) {
            e.printStackTrace();
        }

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

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

发布评论

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

评论(2

紫轩蝶泪 2024-12-14 17:36:24

来自ObjectInputStream的规范

这个构造函数会阻塞,直到对应的ObjectOutputStream
已写入并刷新标头。

From the specification of ObjectInputStream:

This constructor will block until the corresponding ObjectOutputStream
has written and flushed the header.

时光清浅 2024-12-14 17:36:24

(对于未来的读者:)我遇到了同样的问题,因为我在服务器程序中做了一个愚蠢的更改,并且很长一段时间没有测试它,然后我对为什么程序被锁定感到困惑。

ServerSocket 接受连接 (responderSocket = serverSock.accept();),然后突然出现一个不适当的 if (我提到的愚蠢的更改!)程序跳出线程,并且因为我没有添加 finally 块来关闭流和套接字,所以套接字被遗弃,无法发送或接收任何内容(甚至是流标头)。所以在客户端程序中没有流头(当我调试代码时,我看到锁定之前执行的最后一个函数是:

public ObjectInputStream(InputStream in) throws IOException {
    verifySubclass();
    bin = new BlockDataInputStream(in);
    handles = new HandleTable(10);
    vlist = new ValidationList();
    enableOverride = false;
    readStreamHeader();                  //// <== This function
    bin.setBlockDataMode(true);
}

readStreamHeader();

所以要小心服务器端发生的事情,也许问题不在你所期望的地方!

(For future readers:) I had the same problem because i made a silly change in server program and didn't test it for a long time then i was confused about why program is locked.

ServerSocket accepts the connection (responderSocket = serverSock.accept();) then suddenly for a inapropriate if (The silly change i mentioned!) program jumps out of the thread and because i didn't add a finally block to close streams and sockets the socket was left abandoned w/o sending or recieving anything (even stream headers). So in client side program there was no stream header (When i debbugged The code i saw that the last function executed before lock was:

public ObjectInputStream(InputStream in) throws IOException {
    verifySubclass();
    bin = new BlockDataInputStream(in);
    handles = new HandleTable(10);
    vlist = new ValidationList();
    enableOverride = false;
    readStreamHeader();                  //// <== This function
    bin.setBlockDataMode(true);
}

readStreamHeader();)

So be careful about what happens in server side, maybe problem isn't where you expecting it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文