Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:17:01+00:00 2026-05-31T02:17:01+00:00

I wrote a peer-to-peer networked game in Java using the java.net package and for

  • 0

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.

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.

  1. Should I be using keepAlive to keep the connection open?
  2. Does anyone have a suggestion on how I should go about troubleshooting this problem?

Here is the code generating the error:

/**
 * 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 >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);
      }

Here is the error message and stack trace:

Read failed: java.io.StreamCorruptedException: invalid type code: 00
Number of Bytes available: 100

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

Stack Trace

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:17:02+00:00Added an answer on May 31, 2026 at 2:17 am

    When creating networking applications it is very important to introduce network I/O logging early in the development. It will help you troubleshoot simple bugs like connection breaks and incorrect data being sent or received.

    I assume that you have connection problems in your test environment: that is on local network?
    Can you describe your client? Is it a mobile device?

    If your application needs connection to be constantly open I would recommend implementing some reconnect logic. That is if connection is broken (you detect it when performing I/O operation) you try to re-establish it with the peer.

    UPDATE:

    if (availableBytes >5) {
        obj = in.readObject();
        message = (ClientMessage) (obj);
        processMessage();
    }
    

    What will happen if there are several objects serialized in the available bytes?
    What does in.readObject()? Does it read required amount of bytes for object serialization or everything that is available?

    IMO the possible reason of the behavior you see is that the code does not expect several objects can be received at once and stored in the receive buffer. Also receive buffer can contain parts of the objects (e.g. object1 + two bytes of the object2).

    TCP is a stream protocol and therefore you cannot determine message end easily. You should implement protocol that will determine the start and end of the message being transmitted.
    In the code from you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote an application using ASP.NET MVC, in this application I have an Index
I'm experiencing some issues with rewriting my blocking socket server to a non-blocking version.
I'm currently writing a server application on Linux x86_64 using <sys/socket.h> . After accepting
I'm trying to secure a connection from a Java Client/Server application that communicates over
I'm trying to build a file server using Java TCP sockets. I keep getting
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
I wrote a small internal web app using (a subset of) pylons . As
I wrote a program that forks some processes with fork(). I want to kill
I wrote my code using this article at msdn as a primary helper My
I wrote a trigger that updates local table and similar table on linked server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.