serialization - java.io.StreamCorruptedException: invalid type code: AC client server-Can't find multiple instance of ObjectOutputStream -


this question has answer here:

i having same issue describe in: https://stackoverflow.com/questions/17196588/java-io-streamcorruptedexception-invalid-type-code-ac-client-server

however, not see how creating multiple objectoutputstream. sure op received correct answer , sure somehow creating multiple instances, don't see how.

   public class node {     public static void main(string[] args)     {          file file = new file("hotwords.txt");         appendableobjectoutputstream  oos = null;         outputstream outstream = null;         long filesize = file.length();         arraylist<string> hotwords = new arraylist<string>();         try         {         bufferedreader br = new bufferedreader(new filereader(file));         string currentline;         while (( currentline = br.readline()) != null) {             hotwords.add(currentline);             system.out.println("hotword: " + currentline);             }         br.close();         }         catch(exception e) {             e.printstacktrace();             system.exit(0);          }         socket s = null;         try{          s = new socket("server", 8189);          printwriter writer = new printwriter(s.getoutputstream(), true);          writer.println("node");         outstream = s.getoutputstream();         oos = new appendableobjectoutputstream(outstream);          oos.flush();            }          catch(exception e)          {             e.printstacktrace();             system.exit(1);          }         try{         string os = system.getproperty("os.name").tolowercase();         file logs;         if(os.matches("windows"))         {         logs = new file(".../logs");         system.out.println("opening windows directory");          }         else         {             logs = new file("...logs");             system.out.println("opening linux directory");         }          for( file f : logs.listfiles() )         {             if(f.getname().matches("machine.log"))             //if(f.getname().matches(".*log$"))             {                 system.out.println("found log " + f);                 runnable r  = new filehandler(s, oos, f, hotwords, file, filesize);                 thread t = new thread(r);                 t.start();             }         }         }         catch(exception e)         {             e.printstacktrace();         }      } }        filehandler.java /* create thread log file continuously read through it*/  import java.io.bufferedreader; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.io.objectoutputstream; import java.io.outputstream; import java.net.socket; import java.util.arraylist; import java.util.hashmap; import java.util.linkedhashset;  public class filehandler implements runnable {      socket c;     file file;     arraylist<string> hotwords;     long hws;     file hwf;     appendableobjectoutputstream oos;       public filehandler(socket conn, appendableobjectoutputstream oos , file f, arraylist<string> h, file hotwordfile, long hotwordsize)     {         c=conn;         file=f;         hotwords = h;         hws = hotwordsize;         hwf=hotwordfile;         this.oos = oos;     }      public void run()     {          system.out.println("filehandler:thread started");         string scurrentline;         bufferedreader br = null;         try {         br = new bufferedreader( new filereader(file) );         }         catch(filenotfoundexception e)         {             e.printstacktrace();         }         hashmap<string, linkedhashset<string> > temp = new hashmap<string, linkedhashset<string> >();         temp.put("filemon", new linkedhashset<string>() );         try {         //outputstream outstream = c.getoutputstream();         //appendableobjectoutputstream  oos = new appendableobjectoutputstream(outstream);   moved cache node share same output stream         boolean test = true;         while(test)         {             if(hwf.length() != hws)             {                 hws = hwf.length();                 hotwords.clear();                 try                 {                 bufferedreader hbr = new bufferedreader(new filereader(file));                 string currentline;                 while (( currentline = hbr.readline()) != null) {                     hotwords.add(currentline);                     system.out.println("hotword: " + currentline);                     }                 hbr.close();                 }                 catch(exception e) {                     e.printstacktrace();                     system.exit(0);                  }              }             while((scurrentline = br.readline()) != null)             {                 system.out.println(scurrentline);                 for( string h : hotwords)                 {                      if( scurrentline.matches(h) )                     {                         system.out.println("filehandler:found matching line " + scurrentline);                         temp.get("filemon").add(file.getname() + ": " + scurrentline);                         break;                     }                 }             }              if(!temp.get("filemon").isempty())             {                 if(c.isconnected())                 { oos.writeobject(temp); oos.reset(); }             system.out.println("node:printed object: size of filemon " + temp.get("filemon").size() + " id: " + temp.tostring());             temp.get("filemon").clear();             system.out.print("node:size of filemon after clear: "  + temp.get("filemon").size());              }           }         br.close();         }         catch(ioexception e)         {             e.printstacktrace();          }     } }       hub.java /*this hub runs on seperate machine recieves data nodes*/            public class cachemonitorhub {     public static void main(string[] args)     {         map<socket, appendableobjectoutputstream> clients = collections.synchronizedmap(new hashmap<socket, appendableobjectoutputstream>());         try         {             serversocket s = new serversocket(8189);             while(true)             {                 socket incoming = s.accept();                 system.out.println("spawning " + incoming);                 runnable r = new connectionhandler(incoming, clients);                 thread t = new thread(r);                 t.start();             }         }         catch (ioexception e)         {             e.printstacktrace();         }     } }          handler.java /*lastly, responsible publishing messages clients*/    import java.io.ioexception; import java.io.inputstream; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.outputstream; import java.io.printwriter; import java.net.socket; import java.util.arraylist; import java.util.hashmap; import java.util.linkedhashset; import java.util.list; import java.util.map; import java.util.scanner;  public class connectionhandler implements runnable {      map<socket, appendableobjectoutputstream> sockets;     socket incoming;      public connectionhandler(socket socket, map<socket, appendableobjectoutputstream> others)     {         incoming = socket;         sockets = others;      }      public void run()     {          inputstream instream = null;         outputstream outstream = null;         objectinputstream ois= null;         appendableobjectoutputstream oos =null;         try{         instream = incoming.getinputstream();         outstream = incoming.getoutputstream();           }         catch(ioexception e)         {             e.printstacktrace();         }         system.out.println("creating scanner..");         scanner in = new scanner(instream);         //printwriter out = new printwriter(outstream, true /* autoflush */);         string clientornode = "";          clientornode = in.nextline();         system.out.println("hub: " + clientornode);          if(clientornode.equals("client"))         {              system.out.println("hub:found client!");             /*             appendableobjectoutputstream  oos = null;              try{             oos = new appendableobjectoutputstream(outstream);              }             catch(ioexception e)             {                 e.printstacktrace();                 system.exit(0);             }             */             try{             oos = new appendableobjectoutputstream(outstream);             }             catch(ioexception e)             {                 e.printstacktrace();             }             sockets.put(incoming, oos);         }         else if ( clientornode.equals("node") )         {             try {                 ois = new objectinputstream(instream);              }             catch(ioexception e){                 e.printstacktrace();             }             system.out.println("hub:found node!");             system.out.println("hub:about enter while");             while(1==1)             {                 try{                     system.out.println("hub:in while loop read object");                 hashmap<string, linkedhashset<string>> temp =  null;                  try {                 temp = (hashmap<string, linkedhashset<string>>) ois.readobject();                 }                 catch(exception e)                 {                     e.printstacktrace();                 }                  system.out.println("hub:object recieved " + temp.tostring());                   for(socket s : sockets.keyset())                 {                             system.out.println("hub:writing object " + s.tostring());                         try {                         sockets.get(s).writeobject(temp);                         sockets.get(s).reset();                         }                         catch(exception e)                         {                             sockets.remove(s);                         }                   }                 system.out.println("past loop!!");                 }                 catch(exception e)                 {                     e.printstacktrace();                 }                 try {                  thread.sleep(200);                 }                 catch(exception e)                 {                     e.printstacktrace();                 }             }         }     } }      appendableobjectoutputstream /*just tried adding seen on suggestion other post not helping*/      import java.io.objectoutputstream;     import java.io.outputstream;     import java.io.ioexception;      public class appendableobjectoutputstream extends objectoutputstream {        public appendableobjectoutputstream(outputstream out) throws ioexception {          super(out);        }        @override       protected void writestreamheader() throws ioexception {         // not write header, reset:         // line added after question         // showed problem original         reset();       }      } 

any ideas why getting java.io.streamcorruptedexception: invalid type code: ac?

unless filehandler.run()method synchronized, or there internal synchronization within it, neither of true, don't see how can possibly expect work. you're writing same objectoutputstream multiple threads: you're going interleaving of data. happen @ receiver.

nb testing isconnected() doesn't accomplish useful. did connect socket, when created it, , isconnected() continue tell so, after close it. doesn't example tell whether connection still alive.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -