T
Tomek Gruca
Hello,
I would like to send objects as well as simple strings over a tcp/ip
connection.
I already have much code using InputStreamReader and PrintWriter for
sending strings, now I want to add some functionality with
ObjectInput/OutputStream (serialization).
For sending strings I use:
Socket socket = ........;
InputStreamReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
and then:
String s;
if( in.ready() ) s = in.readLine();
out.println(s);
Now can I send an object over _opened tcp connection_ (same sockets as
above)?
objectIn = new
ObjectOutputStream objectOut = new
ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectIn = new
ObjectInputStream(socket.getOutputStream());
and then:
someObj = (SomeClass) in.readObject();
in.writeObject(someObj);
When I do it this way I get "StreamCorruptedException: invalid stream
header".
Is there something wrong or it's just impossible to have 2 wrappers
over same socket streams?
I could send strings also as objects using only
ObjectInput/OutputStream but the functionality I miss here is
in.ready() method...
I would like to send objects as well as simple strings over a tcp/ip
connection.
I already have much code using InputStreamReader and PrintWriter for
sending strings, now I want to add some functionality with
ObjectInput/OutputStream (serialization).
For sending strings I use:
Socket socket = ........;
InputStreamReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
and then:
String s;
if( in.ready() ) s = in.readLine();
out.println(s);
Now can I send an object over _opened tcp connection_ (same sockets as
above)?
objectIn = new
ObjectOutputStream objectOut = new
ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectIn = new
ObjectInputStream(socket.getOutputStream());
and then:
someObj = (SomeClass) in.readObject();
in.writeObject(someObj);
When I do it this way I get "StreamCorruptedException: invalid stream
header".
Is there something wrong or it's just impossible to have 2 wrappers
over same socket streams?
I could send strings also as objects using only
ObjectInput/OutputStream but the functionality I miss here is
in.ready() method...