[Sockets] Problem with ObjectInput/OutputStream

L

Louis Cyphre

Hi,
I encountered a problem using the ObjectInputStream and
ObjectOutputStream. I wrore:

ObjectInputStream input;
ObjectOutputStream output;

/* here the execution stops */
input = new ObjectInputStream( socket.getInputStream() );
output = new ObjectOutputStream( socket.getOutputStream() );

....where i wrote the comment the execution stops without exceptions (the
application goes on running).

At the contrary, if I write:

DataInputStream input;
DataOutputStream output;

input = new DataInputStream( socket.getInputStream() );
output = new DataOutputStream( socket.getOutputStream() );

....everything works and I can write and read through the socket.
But my intention is to write and read OBJECTS through the socket.

Is there something wrong in my code? How can I exchange objects through
a socket?

Thanks in advance.
 
E

Esmond Pitt

Louis said:
/* here the execution stops */
input = new ObjectInputStream( socket.getInputStream() );
output = new ObjectOutputStream( socket.getOutputStream() );

Change this to:

output = new ObjectOutputStream( socket.getOutputStream() );
output.flush();
input = new ObjectInputStream( socket.getInputStream() );

Change it at both ends for safety.
 
J

jupiter49

Esmond Pitt said:
Change this to:

output = new ObjectOutputStream( socket.getOutputStream() );
output.flush();
input = new ObjectInputStream( socket.getInputStream() );

Change it at both ends for safety.

What's the rationale for doing the output statement first? What's the point
of a flush() before you've written to the stream?
 
E

Esmond Pitt

jupiter49 said:
What's the rationale for doing the output statement first? What's the point
of a flush() before you've written to the stream?

The constructor of an ObjectOutputStream writes a header to the stream,
and the constructor of an ObjectInputStream reads it. This is why you
were deadlocked in constructing an ObjectInputStream that nobody had
written to yet because the other end was deadlocked the same way.
 
J

jupiter49

Louis,

This works:

ObjectOutputStream out = new
ObjectOutputStream(responseSocket.getOutputStream());
out.flush();
System.out.println("Flushed.");
Socket inSocket = new Socket("localhost", 6069);
ObjectInputStream in = new
ObjectInputStream(inSocket.getInputStream());
System.out.println("Got instream.");

However, it will hang at creating the InputStream if inSocket is not
initialized to a functioning, valid listener (in my case there is one on
6069.) If you experiment with intentionally invalid port numbers you'll
find that it will abort at runtime. If you give it a listener-port that is
not going to communicate, it will just hang. Or Wait. Or whatever. With
no port provided (an improperly initialized socket object, it will also
hang/wait.

Good luck. Once you get this far you can .writeObject() to a stream. paul



Louis Cyphre said:
Hi,
I encountered a problem using the ObjectInputStream and
ObjectOutputStream. I wrore:

ObjectInputStream input;
ObjectOutputStream output;

/* here the execution stops */
input = new ObjectInputStream( socket.getInputStream() );
output = new ObjectOutputStream( socket.getOutputStream() );

...where i wrote the comment the execution stops without exceptions (the
application goes on running).

That's pretty weird.
 
E

Esmond Pitt

jupiter49 said:
Louis,

This works:

ObjectOutputStream out = new
ObjectOutputStream(responseSocket.getOutputStream());
out.flush();
System.out.println("Flushed.");
Socket inSocket = new Socket("localhost", 6069);
ObjectInputStream in = new
ObjectInputStream(inSocket.getInputStream());
System.out.println("Got instream.");

I'm sorry, I don't see the point of using another socket. The suggestion
I gave earlier is sufficient.

What you have exposed here is the curious fact that socket connections
to local ports always seem to work but the sockets then fail in use.
This is not a Java issue, it happens in every TCP stack I have ever used.
 
J

jupiter49

Esmond Pitt said:
I'm sorry, I don't see the point of using another socket. The suggestion
I gave earlier is sufficient.

Another socket? That was the only socket. I think maybe you thought I was
the original poster? I'm not. All I did was use the typical constructor
for the socket.


What you have exposed here is the curious fact that socket connections
to local ports always seem to work but the sockets then fail in use.

I'm guessing that the original poster had posted something else about trying
to use a remote connection? I've never had that problem with character
streams. This was my first look at object streams, and I sorta jumped into
the middle of it with very little depth of what is going on there. I did
notice that something got written to the stream by the OutputStream because
I was catching it on the other end with a pre-existing character stream and
the header appeared as some type of upper ASCII garbage. Actually, it
didn't even resemble anything in an ASCII character set, so that was a new
one on me.
 
E

Esmond Pitt

jupiter49 said:
Another socket? That was the only socket. I think maybe you thought I was
the original poster? I'm not. All I did was use the typical constructor
for the socket.

I can see responseSocket and newSocket in your posting, this makes two
sockets.
I'm guessing that the original poster had posted something else about trying
to use a remote connection?

Well, I'm not guessing, I'm reading and responding to what people wrote,
including yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top