开发者

Fundamentals of iostream and read/writeObject calls

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)

  1. TestServer project, run it as a Java Application
  2. 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)

  1. TestServer project, run it as a Java Application
  2. TestClient project, run it as a Android Junit Test

Console Outp开发者_C百科ut

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)


Not sure if this causes this specific problem, but you should always use this order when creating object streams:

  1. Create ObjectOutputStream
  2. Flush it
  3. Create 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.


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.


I think this is because, your client exits after executing

objectOutputStream.writeObject(message) ;

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜