Results 1 to 2 of 2
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default StreamCorruptedException

    Not sure if I should really be posting in the "Advanced" section lol.

    The problem is really that I don't know how to reset or flush the ObjectOutputStream so that I can write that same Object over the stream more than once, for continuous updating, thus making the other characters "live".

    I'll show you guys some code.

    I took out all methods that don't have to do with direct communication between serverthread and client. Know that the only error is that I can't figure out how to write my arraylist object that holds all clients' character position data from the serverthread to the client.

    P.S. The error occurs when I added
    Java Code:
    oos.reset();
    to the program. I'm not sure how to correctly reset an ObjectOutputStream so that I can write an object to it more than one time and have the object go through, rather than just get an acknowledgement that the object was already written to the stream after the first write.

    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Random;
    
    public class ServerThread extends Thread
    {
    
      public Socket socket;
      public DataInputStream in;
      public DataOutputStream out;
      public ObjectOutputStream oos;
      public int x = 0;
      public int y = 0;
      public boolean disconnect;
      public boolean cont;
      public int num;
      public ArrayList<Integer> awesome;
      public boolean wait = false;
      public Random random;
    
      public ServerThread(Socket socket)
      {
        this.socket = socket;
        cont = true;
        disconnect = false;
        awesome = new ArrayList<Integer>();
        try
        {
          in = new DataInputStream(this.socket.getInputStream());
          out = new DataOutputStream(this.socket.getOutputStream());
          oos = new ObjectOutputStream(this.socket.getOutputStream());
          random = new Random();
        }
        catch(Exception e)
        {
          try
          {
            in.close();
            out.close();
    	oos.close();
          }
          catch(Exception e1)
          {
    	e1.printStackTrace();
          }
          e.printStackTrace();
        }
      }
    
      [B]public void run()
      {
        while(cont)
        {
          System.out.println(" ");
          for(int z = 0;z<awesome.size();z++)
          {
    	System.out.print(awesome.get(z)+" ");
          }
          System.out.println(" ");
          try
          {
    	int b = in.readByte();
            if(b == 2)
            {
    	  x = in.readInt();
    	  //System.out.println(x);
            }
            if(b == 3)
            {
    	  y = in.readInt();
    	  //System.out.println(y);
            }
    	if(b == 4)
    	{
    	  disconnect = true;
    	}
    	out.writeBoolean(true);
    	oos.writeObject(awesome);
    	while(!wait)
    	{
    	  wait = in.readBoolean();
    	}
    	oos.reset();
          }
          catch(IOException ie)
          {
    	ie.printStackTrace();
          }
        }
        try
        {
          in.close();
          out.close();
          oos.close();
          socket.close();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }[/B]
    }
    That was my Server's thread code, now for the client code (once again, the client gets the data from the game about where the character's position is correctly. The problem is strictly in the ObjectInputStream, because I keep getting a StreamCorruptedException):

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class Client extends TimerTask
    {
    
      public Socket socket;
      public DataInputStream in;
      public DataOutputStream out;
      public ObjectInputStream ois;
      public int x;
      public int y;
      public int stop;
      public boolean wait = false;
      public boolean cont = true;
      public int number;
      public ArrayList others;
    
      public Client()
      {
        others = new ArrayList();
        try
        {
          socket = new Socket("192.168.1.102",9001);
          in = new DataInputStream(socket.getInputStream());
          out = new DataOutputStream(socket.getOutputStream());
          ois = new ObjectInputStream(socket.getInputStream());
        }
        catch(Exception e)
        {
          e.printStackTrace();
          System.exit(0);
        }
        if(socket.isConnected())
        {
          System.out.println("Dude you connected!");
        }
      }
    
      [B]public void communicate() throws IOException, ClassNotFoundException
      {
        out.writeByte(2);
        out.writeInt(x);
        out.writeByte(3);
        out.writeInt(y);
        while(!wait)
        {
          wait = in.readBoolean();
        }
        wait = false;
        others = (ArrayList)ois.readObject();
        out.writeBoolean(true);
      }[/B]
    
      public void run()
      {
        if(cont)
        {
          try
          {
    	communicate();
          }
          catch(Exception e)
          {
    	e.printStackTrace();
          }
        }
      }
    }

  2. #2
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Nevermind! Problem solved, thanks to writeUnshared() in ObjectOutputStream! Thank goodness for random stumblings upon the methods I need.

Similar Threads

  1. java.io.StreamCorruptedException
    By goto in forum Networking
    Replies: 8
    Last Post: 11-14-2013, 02:44 PM
  2. Replies: 8
    Last Post: 11-05-2010, 11:26 PM
  3. StreamCorruptedException
    By wikisb in forum New To Java
    Replies: 3
    Last Post: 10-28-2010, 08:16 AM
  4. StreamCorruptedException
    By cristo_haris in forum Advanced Java
    Replies: 11
    Last Post: 04-20-2009, 04:44 PM
  5. java.io.StreamCorruptedException
    By elizabeth in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 07:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •