sockets - Java - Error StreamCorruptedException: invalid type code: AC -


this question has answer here:

first of all, know , aware of duplicate question, in other answers, can't understand it!
so, i'm doing client-server connection using java's sockets. have got class named listener, which, each time new client, assign client socket. repeatedly listening client, , if answer client, send method in interface "class"(this last thing isn't question, don't want asking it)
code:

         while(!s.isclosed()){             ob = new objectinputstream(s.getinputstream());             try {                 handler.objectreceived(ob.readobject(), instance);             } catch (classnotfoundexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }            } 

where "s" socket, , "ob" objectoutputstream, , "handler" interface mentioned above. but, after while using it, error java.io.streamcorruptedexception: invalid type code: ac in handler.objectreceived(ob.readobject(), instance); line...

any help??

note: please, not mark question duplicate. can't understand other answers, , i'm asking explain clearly. after, mods can delete if want, please answer! i'm getting annoyed error!

your cause keep recreating objectinputstream. if change code should work fine assuming stream not corrupted.

ob = new objectinputstream(s.getinputstream()); while(!s.isclosed()){      try {         handler.objectreceived(ob.readobject(), instance);     } catch (classnotfoundexception e) {         // todo auto-generated catch block         e.printstacktrace();     } } 

i assume code writes object create objectoutputstream 1 single time. when create objectoutputstream writes header, consisting of 4 bytes.

bout.writeshort(stream_magic); bout.writeshort(stream_version); 

now when create objectinputstream read header(this call gets done in constructor).

now because keep re-creating objectinputstream attempts read headers not exist in data stream.

in essence create objectoutputstream , objectinputstream 1 time.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -