java.io.StreamCorruptedException: invalid stream header: 54657374 -
i trying read string send client using socket program, code follows:
import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.lang.classnotfoundexception; import java.net.serversocket; import java.net.socket; public class socketserverexample { //static serversocket variable private static serversocket server; //socket server port on listen private static int port = 5000; public static void main(string args[]) throws ioexception, classnotfoundexception{ //create socket server object server = new serversocket(port); //keep listens indefinitely until receives 'exit' call or program terminates while(true){ system.out.println("waiting client request"); //creating socket , waiting client connection socket socket = server.accept(); //read socket objectinputstream object objectinputstream ois = new objectinputstream(socket.getinputstream()); //convert objectinputstream object string string message = (string) ois.readobject(); system.out.println("message received: " + message); //create objectoutputstream object objectoutputstream oos = new objectoutputstream(socket.getoutputstream()); //write object socket oos.writeobject("hi client "+message); //close resources ois.close(); oos.close(); socket.close(); //terminate server if client sends exit request if(message.equalsignorecase("exit")) break; } system.out.println("shutting down socket server!!"); //close serversocket object server.close(); } }
but getting error follows while reading string client:
exception in thread "main" java.io.streamcorruptedexception: invalid stream header: 54657374 @ java.io.objectinputstream.readstreamheader(objectinputstream.java:803) @ java.io.objectinputstream.<init>(objectinputstream.java:298) @ socketserverexample.main(socketserverexample.java:29)
i searched , not managed find bug. please me.
clearly aren't sending data objectoutputstream:
writing bytes.
- if read
readobject()
must writewriteobject().
- if read
readutf()
must writewriteutf().
- if read
readxxx()
must writewritexxx(),
values of xxx.
Comments
Post a Comment