Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Socket closes when I redeclare BufferedOutputStream for second time - why?

843790Aug 7 2009 — edited Aug 16 2009
I have initialize a socket and made a connection between my simple client application and server application.

In a thread in my server application I have this line:
out = new BufferedOutputStream(socket.getOutputStream());
Then I send some data to my client and then I close the BufferedOutputStream with
out.close();
After that I restart the thread and then this line of code is beeing run again:
out = new BufferedOutputStream(socket.getOutputStream());
The problem is that the socket gets closed down when I run the thread for the second time. I have no clue why.
I have checked the socket state with System.out.println(socket.isConnected()); and it returns true both the times, so the socket is not closed what I can see.

Why does my socket close down when redeclaring the BufferedOutputStream?


Another question:
When I send the byte array "myByteArray" with:
out.write(myByteArray);
my client is getting all the bytes (I print all of them).

But when I just send one byte:
out.write(-10);
my client is not getting that byte.

How come?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 13 2009
Added on Aug 7 2009
35 comments
512 views