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!

Close OutputStream to send data finally?

843790Nov 28 2006 — edited Oct 24 2007
Hello,

I'm currently using a ServerSocket. Here is my code reduced to the simple core:
ServerSocket ss = new ServerSocket(port, backlog);
Socket connectedSocket = ss.accept();

InputStream inputStream = ss.getInputStream();
int c = -1;
while (true)
{
    while ((c=inputStream.read()) != -1)
    {
        System.out.println((char) c);
    }
}
On the client side I use a Socket to connect to this ServerSocket and try to send data multiple times after 2 seconds of delay:
Socket s = new Socket(remoteipaddr, remoteport);
OutputStream os = s.getOutputStream();
for (int i=1; i<4; i++)
{
    os.write(i + " Hello Server!".getBytes());	
    os.flush();
    // I don't want to close the stream
    //os.close
    Thread.sleep(2000);
}
At the server's side nothing is received! When I close the Outputstream at the client's side, the data is sent. The problem: I would have to reconnect every time to get a new Socket object and write the next part of data (3 times in the above example).

Isn't it possible to connect to the ServerSocket just once and then have the client write as much data as I want to and have it immediately received (and printed to console) by the server?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2007
Added on Nov 28 2006
11 comments
682 views