iostream 和 read/writeObject 调用的基础知识
我正在设计一个java服务器来响应多个客户端请求。因此,设计基本上有一个服务器套接字,接受客户端套接字,从客户端输入/输出流创建一个 inputObjectStream 和一个outputObjectStream。
然后我在客户端上使用 writeobject 发出请求,在服务器上使用 readObject 接收请求。处理它,将对象写回同一流作为响应,并在客户端读取对象来处理响应。
现在,如果我在 Android 模拟器/设备上运行代码就可以正常工作。如果我在“android junit java 测试用例”上运行同一段代码,我会在处理我的所有请求后收到异常。例外情况发生在服务器端的 readObject 调用上。
java.io.EOFException at
java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2570)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1314)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)`
问题 在 iostream 上读/写对象是正确的设计吗?
已编辑
我已将示例项目上传到 4shared.com (http://www.4shared.com/archive/98gET_pV/Issue15426tar.html) 或者 (http://www.sendspace.com/file/v04zjp)
测试 1 (PASS)
- TestServer 项目,将其作为 Java 应用程序运行
- TestClient 项目,将其作为 Android 应用程序运行
控制台输出
服务器套接字已打开 /127.0.0.1
客户端套接字已接受
输入流已创建已创建
输出流已创建
读取对象已创建
测试 2(失败)
- TestServer 项目,将其作为 Java 应用程序
- TestClient 运行项目,将其作为 Android Junit Test
Console Output
Server Socket Opened /127.0.0.1
Client Socket Accepted运行
Error : Unable to open server socket. Server wont load.
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2297)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2766)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:797)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:297)
at com.test.server.myThread.run(Main.java:52)
at com.test.server.Main.main(Main.java:32)
I am designing a java server to respond to multiple client requests. So the design basically has a server socket, accepts a client socket, creates a inputObjectStream and a outputObjectStream from the client input/outputStream.
I then use writeobject on the client to make a request, readObject on the server to receive the request. Process it, write the object back on the same stream as a response, and on the client side readobject to process the response.
Now, if I run the code on a android emulator/device works fine. The same piece of code if I run on a "android junit java test case", i get a exception after it processes all my requests. The exception is on the server side on readObject call.
java.io.EOFException at
java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2570)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1314)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)`
Question
Is it a correct design to read/writeObjects on a iostream ?
Edited
I have the sample project uploaded on 4shared.com (http://www.4shared.com/archive/98gET_pV/Issue15426tar.html)
OR
(http://www.sendspace.com/file/v04zjp)
Test 1 (PASS)
- TestServer project, run it as a Java Application
- TestClient project, run it as a Android Application
Console Output
Server Socket Opened /127.0.0.1
Client Socket Accepted
Input Stream created
Output Stream created
Read Object created
Test 2 (FAIL)
- TestServer project, run it as a Java Application
- TestClient project, run it as a Android Junit Test
Console Output
Server Socket Opened /127.0.0.1
Client Socket Accepted
Error : Unable to open server socket. Server wont load.
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2297)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2766)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:797)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:297)
at com.test.server.myThread.run(Main.java:52)
at com.test.server.Main.main(Main.java:32)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这是否会导致此特定问题,但在创建对象流时应始终使用此顺序:
ObjectOutputStream
ObjectInputStream
我至少在服务器端看到这一点您首先创建了
ObjectInputStream
并且没有刷新可能会导致问题的流。此处提供有关此命令背后的主题和原因的更多信息。
Not sure if this causes this specific problem, but you should always use this order when creating object streams:
ObjectOutputStream
ObjectInputStream
I see that at least on the server side you created
ObjectInputStream
first and didn't flush the stream which might cause your problems.Here is more info about the topic and reasons behind this order.
您应该选择 1024 以上的服务器端口,因为以下所有端口都是为已知服务保留的。只需选择 1024 到 65535 之间的端口,看看它是否可以工作。
另外我建议您将 ObjectInputStream 包装到 BufferedInputStream 中;一旦你的连接不稳定,它可能会失败。
You should choose a server port above 1024, as all the ports below are reserved for known services. Just choose a port between 1024 and 65535 and see if it works then.
Also I would advise you to wrap your ObjectInputStream into a BufferedInputStream; It may be that once your connection isn't that steady, it will fail.
我认为这是因为,你的客户端在执行后退出
所以尝试在此行之后引入人为延迟并查看。
在所有情况下,当客户端/服务器关闭连接/终止程序时,如果另一端正在等待任何输入(例如,inputObjectStream.readObject()),则另一端将抛出此 EOF 异常,
因此请确保由在客户端终止之前,服务器可以正确读取客户端。所以在客户端引入一些延迟看看。
I think this is because, your client exits after executing
So try to introduce artificial delay after this line and see.
In all the cases, when client/server closes the connections/terminates the program, then the other end will throw this EOF exception if it is waiting for any input(eg, inputObjectStream.readObject())
so make sure that whatever is written by client is properly read by the server before the client terminates. So introduce some delay at the client and see.
请在客户端刷新流。我希望这应该能解决问题。
Please flush the stream in the client side. I hope that should solve the problem.