aspose file tools
The moose likes I/O and Streams and the fly likes Socket - StreamCorruptedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Socket - StreamCorruptedException" Watch "Socket - StreamCorruptedException" New topic

Socket - StreamCorruptedException

Mobeen Zafar

Joined: Nov 05, 2003
Posts: 2
I am working on a Chat Server(Client-Applet). Communication between client and server is over sockets. Serialized object has been wriiten and read from the socket ObjectOutputStream and ObjectInputStream.
>>getting server socket
inetAddress = InetAddress.getAllByName(IP);
s = new ServerSocket(Integer.parseInt(serverPort),Integer.parseInt(localPort),inetAddress[0]);
>>server socket accepting client request to connect
Socket C_Soc = s.accept();
InputStream in = C_Soc.getInputStream();
i = new ObjectInputStream(in);
OutputStream out =C_Soc.getOutputStream();
o = new ObjectOutputStream(out);
>>code to write on a socket
>>code to read object
Object obj = (Object) in.readObject();
I got StreamCorruptedException on server side if the ObjectOutputStream of a socket is flooded with messages or some times IOException. This causes My Client Thread which is waiitng to read an Object throws an Exception which results in disconnection of a client from the chat server.
Can somebody reply me why I got these exceptions and any solution to this problem. What about setting the setting the sender and reaciever buffer size of a socket. please check the code above.
Ed Tidwell

Joined: Oct 22, 2002
Posts: 18
At a quick glance I dont' think you can set the TCP buffers to the values you have given.
That is PART OF THE OS platform that the JVM sits on top of.
So for example I'm willing to bet you can not set the TCP buffer of a wireless card to the same size as the TCP buffer of a 10BaseT connection.
You can ask the JVM how big you want to make your TCP buffers but I'm pretty sure it can ignore this request.
Look at getsendBufferSize() in the Java Docs and notice the key words "buffer size USED BY THE PLATFORM".
You can create you own buffer to pass around and TCP will break it up for streaming over the net behind the scenes. You should look at NIO and Socket Channels.
I hope this was helpful.
It is sorta covered in the JavaRanch Style Guide.
subject: Socket - StreamCorruptedException