Results 1 to 2 of 2
Thread: StreamCorruptedException
- 04-10-2011, 08:46 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
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 addedto 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:oos.reset();
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.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] }
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(); } } } }
- 04-10-2011, 09:56 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
java.io.StreamCorruptedException
By goto in forum NetworkingReplies: 6Last Post: 01-04-2011, 04:53 PM -
I got a problem with StreamCorruptedException, any ideas?
By Goodwine in forum New To JavaReplies: 8Last Post: 11-05-2010, 10:26 PM -
StreamCorruptedException
By wikisb in forum New To JavaReplies: 3Last Post: 10-28-2010, 07:16 AM -
StreamCorruptedException
By cristo_haris in forum Advanced JavaReplies: 11Last Post: 04-20-2009, 03:44 PM -
java.io.StreamCorruptedException
By elizabeth in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks