Results 1 to 1 of 1
Thread: Object streams messed up!
- 03-18-2011, 02:18 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
Object streams messed up!
Hello!
I have met problem that i cant climb over even if i've tried many different ways and asked for help. But i've never managed to make it work, its about object streams that corrupts / "invalid type code: AC". This is the way how i want it to work:
Client connects to the server, server makes new thread for client and listens to it and whenever the client writes object on stream that server listens for the server sends the object to every connected client.
Whenever i connect to the client i am able to send a object to it and receive it back but when i do that again i got error! "java.io.StreamCorruptedException: invalid type code: AC" How can i avoid this and what causes this? I've heared its about the headers of streams. I got no idea whats wrong. I know google is nice friend but this time it doesnt just help me! :eek:
Heres my code.
This is the server:
and this is the client that connects to the server:Java Code:import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class server { ServerSocket ss; List<Socket> users = new ArrayList<Socket>(); public static void main(String args[]){ new server(); } server(){ try { ss = new ServerSocket(1234); } catch (IOException e) { e.printStackTrace(); } while(true){ Socket s = null; try { s = ss.accept(); } catch (IOException e) { e.printStackTrace(); } users.add(s); new handler(s).start(); } } class handler extends Thread{ Socket s; Object o; ObjectInputStream ois; handler(Socket s){ this.s = s; } public void run(){ try { ois = new ObjectInputStream(s.getInputStream()); } catch (IOException e) { e.printStackTrace(); } while(s.isConnected()){ try { o = ois.readObject(); sendAll(o); } catch (IOException e) { e.printStackTrace(); stop(); } catch (ClassNotFoundException e) { e.printStackTrace(); stop(); } } } } void sendAll(Object obj){ for(int x = 0; x < users.size(); x++){ try { new ObjectOutputStream(users.get(x).getOutputStream()).writeObject(obj); } catch (IOException e) { e.printStackTrace(); } } } }
and the object i want to send over internet(i know theres no mind! but this is just an example, and i want to understand more about sockets / object streams)Java Code:import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class client { Socket s; ObjectOutputStream oos; public static void main(String args[]){ new client(); } client(){ try { s = new Socket("localhost", 1234); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } new reader(s).start(); Scanner in = new Scanner(System.in); try { oos = new ObjectOutputStream(s.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } while(true){ try { oos.writeObject(new human(in.nextLine())); } catch (IOException e) { e.printStackTrace(); } } } class reader extends Thread{ ObjectInputStream ois; Socket s; reader(Socket s){ this.s = s; } public void run(){ try { ois = new ObjectInputStream(s.getInputStream()); } catch (IOException e1) { e1.printStackTrace(); } while(true){ try { human i = (human) ois.readObject(); System.out.println(i); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } }
Please tell me what is wrong and why?Java Code:import java.io.Serializable; public class human implements Serializable { String name; human(String name){ this.name = name; } public String toString(){ return "Humans name is "+name; } }
Similar Threads
-
Cant make my object streams work correctly
By Ruuhkis in forum NetworkingReplies: 0Last Post: 03-14-2011, 06:54 PM -
[BUG]: jframe objects messed up
By batia in forum Advanced JavaReplies: 4Last Post: 03-09-2011, 07:27 AM -
Is object of my class in a thread or I messed up again?
By atch in forum Threads and SynchronizationReplies: 5Last Post: 02-09-2010, 08:51 AM -
[newbie] messed up code
By jon80 in forum New To JavaReplies: 2Last Post: 05-31-2009, 04:29 PM -
I must be seriously messed up...
By jpnym15 in forum New To JavaReplies: 2Last Post: 11-16-2008, 07:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks