带有套接字的 ObjectInputStreams 的进一步问题 [英] Further problems with ObjectInputStreams with Sockets

查看:70
本文介绍了带有套接字的 ObjectInputStreams 的进一步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个线程中,我寻求有关套接字和 ObjectInputStreams 问题的帮助.

In another thread I asked for help with a problem of sockets and ObjectInputStreams.

swapnil7 回答并给了我帮助:我的代码说:

swapnil7 answered and gave me help: My Code said:

        while(!line.equalsIgnoreCase("exit")){
            while(in.available() <= 0){}
                line = in.readObject().toString();
                System.out.println(">>> recieved: " + line);
        }

我想,in.available() 请求,准备接收多少字节.所以我做了一个循环,什么也没做,直到 in.available() 说有更多的字节要接收.然后swapnil7说,我必须这样做:

I thought, that in.available() requests, how much bytes are ready to recieve. So I did a loop, that did nothing, until in.available() said, that there are more bytes to recieve. Then swapnil7 said, that I have to do it like this:

        while(!line.equalsIgnoreCase("exit")){
            while(in.available() <= 0){
                line = in.readObject().toString();
                System.out.println(">>> recieved: " + line);
            }
        }

我工作过.但这只是测试应用程序.我的实际应用程序不仅将 ObjectStreams 用于文本,它看起来像这样:

I worked. But that was only the test application. My real application, that uses the ObjectStreams not only for text, looks like this:

    while(true){
        try{
            while(in.available() <= 0) {
                Object obj = in.readObject();
                if(obj instanceof ArrayList){
                    Main.info("recieved package: " + obj.getClass());
                    app.sendClientUpdate(this, obj);
                }else{
                    Main.info("recieved unexpected package: " + obj.toString());
                }
            }

        }catch(Exception ex){
            Main.error("Error while reading: " + ex.getMessage());
        }
    }

这应该是完全一样的,但它抛出了一个异常:

This should be the exact same thing, but it throws an exception:

读取时出错:无效类型代码:42

Error while reading: invalid type code: 42

在代码中抛出:

Main.error("读取时出错:" + ex.getMessage());

Main.error("Error while reading: " + ex.getMessage());

这是我所期望的,当我想到 in.available() 返回可接收字节的数量时.我想,这个异常意味着 inputStream 阻塞了读取,因为缓冲区是空的.现在,我很困惑,因为一方面,我必须像 swapnil7 所说的那样做,另一方面,我的大应用程序抛出异常.谁能向我解释这两种不同的行为?

This is what I expected, when I thought, that in.available() returns the amount of recievable bytes. I thought, that this exception means, that the inputStream blocks the reading, because the buffer is empty. Now, I am confused, because on one hand, I had to do it like swapnil7 said, on the other hand, my big application throws an exception. Can anyone explain these two different behaviors to me?

附言英语仍然不是我的母语 :) 所以我对错误感到抱歉.

P.S. English is still not my mother language :) So I'm sorry about mistakes.

所以,这里是写作部分.它每秒写入套接字超过一次(我认为是 6 次左右):

So, here is the writing part. It writes to the sockets more than one time per second (I think 6 times, or so):

public void sendPackets() throws IOException{
    out.writeObject(tanks);
    out.flush();
    out.writeObject(enemies);
    out.flush();
    out.writeObject(bullets);
    out.flush();
}

此代码完美运行,不抛出任何内容且不会中止(一切都经过测试).写入的对象是可序列化对象的ArrayList,包含一些原始数据.

This code works perfectly, throws nothing and do not abort (Everything tested). The written objects are ArrayLists of serializble Objects, containing some primitive data.

推荐答案

不要使用 in.available().

只需调用 in.readObject().

如果还没有什么可读取的,readObject() 调用将阻塞,直到一些数据从套接字的另一端到达.如果套接字关闭,那么您的代码将获得某种类型的 IOException 而不是返回一个对象.

If there is nothing there to read yet, the readObject() call will block until some data arrives from the other end of the socket. If the socket closes, then your code will get an IOException of some kind instead of returning an object.

如果你想解释为什么你的代码在去掉 available() 调用后失败了,你需要确切地告诉我们你在做什么.理想情况下,为客户端和服务器端创建一个 SSCCE >>we<<可以自己跑去看看问题.

If you want an explanation of why your code is failing when you get rid of the available() calls, you will need to show us exactly what you are doing. Ideally create an SSCCE for the client and server sides that >>we<< can run to see the problem for ourselves.

值得一提的是,无效类型代码:42"表示流损坏,或者写入和读取操作序列之间不匹配.有很多可能的解释,但如果没有 SSCCE,就不可能进一步缩小范围.

For what it is worth, the "invalid type code: 42" indicates either a stream corruption, or that there is a mismatch between the sequences of write and read operations. There are lots of possible explanations, but without an SSCCE, it is impossible narrow it down further.

这篇关于带有套接字的 ObjectInputStreams 的进一步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆