Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the data go when writing to a socket in java?

Tags:

java

sockets

Say I have the following code which sets up a Socket and a in and out stream for reading and writing to the socket.

toMonitor = new Socket(sd.m_sMonName, sd.m_iMonPort);
out = new PrintWriter(toMonitor.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(toMonitor.getInputStream()));

Also say I have the method below called SendIt, which writes a String to the Sockets outputstream

public void SendIt (String message) throws IOException 
{
    try 
    {        
        newStatus("MessageParser [SendIt]: sent:\n\t" + message, true);

        out.println(message);
        if (out.checkError() == true) 
            throw (new IOException());
        out.flush();
        if (out.checkError() == true) 
            throw (new IOException());
    } 
    catch (IOException e) {} //Bubble the Exception upwards
}

When I call out.println(message); above, will that message sit in the buffer until the other end of the socket reads it? - I take it the underlying writer object will break it into bytes, send it to the Sockets output stream where it is assembled into packets and sent out, thus clearing the Buffer as it is written?

Will it sit there until I call out.flush()?

Does out.flush() ALWAYS clear the buffer, or could something stay in there, somehow..?

EDIT:

I am currently using Netbeans as my IDE, is there are way in which I could watch the actual value of the output buffer in real time as the program runs?

like image 517
KDecker Avatar asked Jan 12 '23 10:01

KDecker


1 Answers

When I call out.println(message); above, will that message sit in the buffer until the other end of the socket reads it?

No, it will go to your network interface which transmits it to the remote node as soon as the buffer is full or you flush the io writer/stream. If there is no connection to the remote node, the socket is closed and will not generally accept writes and throw an exception (as long as it knows)

Will it sit there until I call out.flush()?

It may or may not. Note that the buffer that you flush here is the PrintWriter's buffer, not the network socket's buffer. Hence, out.flush() does not as such guarantee transmission, although that is what usually happens next given the connection is still ok. As the Java reference states:

(...) flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Finally, your last question:

Does out.flush() ALWAYS clear the buffer, or could something stay in there, somehow..?

No, nothing will stay in any of your io writer or stream buffers, again according to the reference:

one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

like image 174
miraculixx Avatar answered Jan 18 '23 23:01

miraculixx