Note that there are some explanatory texts on larger screens.

plurals
  1. POObjectOutputStream over a TCP Socket
    text
    copied!<p>I wrote a peer-to-peer networked game in Java using the java.net package and for some reason the Socket between the client and the server closes or gets corrupted. I am calling this "peer-to-peer" because one of the clients also runs a server class, which is just a custom class I wrote that accepts Socket connections - one from the other computer and one from the client computer. The connection is being used to move objects back and forth using ObjectOutputStream and ObjectInputStream.</p> <p>The Socket error happens irregularly. The connection is usually open for 5-10 minutes before there is a problem. Sometimes it closes when a new game is started, other times it closes while a game is in progress.</p> <ol> <li>Should I be using keepAlive to keep the connection open?</li> <li>Does anyone have a suggestion on how I should go about troubleshooting this problem?</li> </ol> <p>Here is the code generating the error:</p> <pre><code>/** * Continually checks for messages to be read in. Then it * sends the messages to processMessage if a message is available. */ public void listenForMessages() { int availableBytes; Object obj = null; try { availableBytes = socketStream.available(); if (availableBytes &gt;5) { obj = in.readObject(); message = (ClientMessage) (obj); processMessage(); } } catch(java.io.StreamCorruptedException utoh) { System.out.println("Read failed: " + utoh); try { System.out.println("Number of Bytes available: " + socketStream.available()); utoh.printStackTrace(); System.out.println("Attempting to close socked."); socketStream.close(); } catch(IOException ioe) { System.out.println("Unable to close: " + ioe.toString()); } } catch(IOException e) { System.out.println("Read failed" + e); } </code></pre> <p>Here is the error message and stack trace:</p> <pre><code>Read failed: java.io.StreamCorruptedException: invalid type code: 00 Number of Bytes available: 100 </code></pre> <p>There are two sockets (two streams) active, one for each client. When the other socket throws the same error, I am seeing invalid type code: FF</p> <p><strong>Stack Trace</strong></p> <pre><code>java.io.StreamCorruptedException: invalid type code: 00 at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1355) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) at thornworks.quiz.PlayerInterface.listenForMessages(PlayerInterface.java:227) at thornworks.quiz.PlayerInterface.actionPerformed(PlayerInterface.java:214) at javax.swing.Timer.fireActionPerformed(Timer.java:291) at javax.swing.Timer$DoPostEvent.run(Timer.java:221) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:602) at java.awt.EventQueue$1.run(EventQueue.java:600) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:611) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload