Stream Corrupted while connection

V

vidhi

We have a java swing game that relies on connection with the server and
that is established through object stream. Our problem is that in
between game play "Stream Corrupted Exception" occurred and
connection breaks.
Is there a way we can avoid this, so that connection will carry on till
the game finish?

here is code at server for sending data

public void sendMessage(CMessage cMessage)
{
if(bIsClosed==false)
try
{
ObjectOutputStream objOutputStream=new
ObjectOutputStream(socket.getOutputStream());
objOutputStream.writeObject(cMessage);
objOutputStream.flush();
}catch(Exception e)
{
bIsClosed=true;
System.out.println("Error Sending Message
"+cMessage.get_title()+e.toString());
server.removeConnection( socket );
}
}


at receiving at client

Object objMessageRecived;
CMessage cMessageRecived;
while(true)
{
try
{
objMessageRecived=(new
ObjectInputStream(socket.getInputStream())).readObject();
cMessageRecived=(CMessage)objMessageRecived;
m_cWindow.handle_message(cMessageRecived);
}
catch(IOException ex){
System.out.println("Error in connection "+ex.toString());
break;
}
catch(ClassNotFoundException ce){System.out.println("Error in
connection !!!!!! "+ce.toString());}
}

Thanks for all your suggestions,
-Vidhi.
 
J

John C. Bollinger

vidhi said:
We have a java swing game that relies on connection with the server and
that is established through object stream. Our problem is that in
between game play "Stream Corrupted Exception" occurred and
connection breaks.
Is there a way we can avoid this, so that connection will carry on till
the game finish?

Probably, if the issue is in your code rather than somewhere such as an
unreliable transmission medium.
here is code at server for sending data
[...]

ObjectOutputStream objOutputStream=new
ObjectOutputStream(socket.getOutputStream());
objOutputStream.writeObject(cMessage);
objOutputStream.flush();
[...]

at receiving at client

Object objMessageRecived;
CMessage cMessageRecived;
[...]

objMessageRecived=(new
ObjectInputStream(socket.getInputStream())).readObject();
cMessageRecived=(CMessage)objMessageRecived;
m_cWindow.handle_message(cMessageRecived);

[...]

The strangest thing I see there is that you are creating new Object
streams for each message send and reception. It is unnecessary and
probably inefficient to do so; more importantly, it could be the source
of your problem. I recommend that you create one Object stream on each
end and keep using it as long as the game is active. You may still find
that you need an error recovery strategy, but it should be a fallback,
not something that gets exercised routinely.

If you continue having the same problem then we may need to see more
code to help you. In that case, try also to figure out whether there is
anything systematic about the messages you are sending when the error
occurs.
 
V

vidhi

Thanks for your attention
I will try to code as you suggested.
There is one more important thing I like to say is after happening this
exception connection is not beaked actually instead we are unable to
receive any message for server but can send as many as we can.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,183
Latest member
OrderGlycoEase

Latest Threads

Top