Monday, October 29, 2012

What does it mean to flush a socket?

There's a certain amount of overhead involved in writing to a network socket and sending data. If data were sent every time byte entered the socket, you'd end up with 40+ bytes of TCP header for every byte of actual data. (Assuming you're using a TCP socket, of course. Other sockets will have different values). In order to avoid such inefficiency, the socket maintains a local buffer, which is usually somewhat over 1000 bytes. When that buffer is filled, a header is wrapped around the data and the packet is sent off to its destination.
In many cases, you don't need each packet to be sent immediately; if you're transferring a file, early data may not be of any use without the final data of the file, so this works well. If you need to force data to be sent immediately, however, flushing the buffer will send any data which has not yet been sent.
Note that when you close a socket, it automatically flushes any remaning data, so there's no need to flush before you close.

your source code is correct and it IS working! think about a buffer like a toilet. only when the toilet is filled with water does flushing it have any impact. 

System.out.println("Enter your choice") automatically flushes the buffer. the "ln" of that method empties the toilet.. 

EX:

double radius = Double.parseDouble(jtf.getText().trim());
toServer.writeDouble(radius);
toServer.flush();//its size is not full a package size yet, but flush it immediately

No comments:

Post a Comment