sockets - java.io.StreamCorruptedException: invalid stream header: 70707070 -


so have seen lot of different questions no definitive help, @ least understanding or personal application. making socket "chat room" program allows user send images selected users through central server. can establish clients connect when sending image error occurs. here code:

client:

    thread thread = new thread() {         @override         public void run() {             try {                 s = new socket("localhost", 4000);                  while (s.isconnected()) {                     oos = new objectoutputstream(s.getoutputstream());                     if (!initialized) {                         oos.writeobject(identity);                         oos.flush();                         oos.reset();                         initialized = true;                     }                     baos = new bytearrayoutputstream(1000);                     // take screenshot                     bufferedimage img = new robot()                             .createscreencapture(new rectangle(toolkit.getdefaulttoolkit().getscreensize()));                      // write img baos                      imageio.write(img, "jpg", baos);                      // send image on socket                     oos.writeobject(baos.tobytearray());                     oos.flush();                     oos.reset();                 }             } catch (exception e) {                 e.printstacktrace();             }          }     };     thread.start(); 

central server home:

    public home() {     initcomponents();     textview = new terminal(terminal);      users = new copyonwritearraylist<>();      thread startserver = new thread(new serverstart());     startserver.start(); }  public class serverstart implements runnable {      @override     public void run() {          try {             serversock = new serversocket(4000);              terminal.append("server started...\n");              while (true) {                 // detect client connection                 socket clientsock = serversock.accept();                  thread thread = new thread(new clienthandler(clientsock));                 thread.start();             }         } catch (exception ex) {             ex.printstacktrace();         }     } }  public class clienthandler implements runnable {      socket socket;      public clienthandler(socket socket) {         this.socket = socket;     }      @override     public void run() {         try {                 user user = new user(socket);                 terminal.append(user.getname() + " connected " + user.gettype() + "...\n");                 if (user.gettype().equals(user.type_01)) {                     users.add(user);                 } else {                     user client = finduser(user);                     while(true){                         user.sendscreen(client.receivescreen());                     }                 }         } catch (exception e) {             e.printstacktrace();         }     }      private user finduser(user user) {         (user client : users) {             if (client.getcompany().equals(user.getcompany())) {                 if (client.getname().equals(user.getname())) {                     return client;                 }             }         }         return null;     } } 

central server user:

    public static final string type_00 = "viewer"; public static final string type_01 = "client";  private byte[] bytes; private objectinputstream in; private objectoutputstream out; private string company, name, type;  public user(socket socket) throws ioexception {     this.out = new objectoutputstream(socket.getoutputstream());     this.in = new objectinputstream(socket.getinputstream());     setusertype(); }  public void sendscreen(byte[] bytes) {     try {         out.writeobject(bytes);         out.flush();     } catch (exception ex) {         ex.printstacktrace();     } }  public byte[] receivescreen() {     byte[] bytes = null;     try {         bytes = (byte[]) in.readobject();     } catch (exception e) {         e.printstacktrace();     }     return bytes; }  public string getcompany() {     return company; }  public string getname() {     return name; }  public string gettype() {     return type; }  public void setusertype() {     string[] strings = null;     try{         strings = (string[])in.readobject();         type = strings[0];         company = strings[1];         name = strings[2];     } catch(exception e){         e.printstacktrace();     } } 

client viewer:

    thread thread = new thread() {         @override         public void run() {             try {                 socket s = new socket("localhost",4000);                   string[] strings = { type, "vision", "cadams" };                   while (s.isconnected()) {                      if(!initialized){                         system.out.println("initialized");                         oos = new objectoutputstream(s.getoutputstream());                         oos.writeobject(strings);                         oos.flush();                         oos.reset();                         initialized = true;                     }                     ois = new objectinputstream(s.getinputstream());                      byte[] bytes = (byte[]) ois.readobject();                     bufferedimage img = imageio.read(new bytearrayinputstream(bytes));                     imageicon ico = new imageicon(                             img.getscaledinstance(viewer.getwidth(), viewer.getheight(), image.scale_smooth));                     viewer.seticon(ico);                 }             } catch (exception e) {                 e.printstacktrace();             }         }     };     thread.start();  } 

i have done extensive research , know streams looking bowl of ramen noodles haven't seen sort of proposal on how fix in terms of source. thank can contribute , please let me know if there can further understanding.

don't keep creating new objectinput/outputstreams. use same ones life of socket.

nb while (s.isconnected() isn't valid. doesn't magically become false when peer disconnects. need handle end of stream , various socket termination exceptions.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -