Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are asking whether or not it is possible to detect, before you call read on a network socket, how many bytes are available to be read, the short answer is no. You can only detect whether or not there are <em>any</em> bytes available to be read but not the number of bytes. The client may also have bytes written to its kernel but blocked by the server's full buffers so the server itself may not know how many bytes are ready to be read. Also, from the client side, there is no way to find out how much data has been written by the client but not consumed by the server.</p> <p>The data is buffered both on the client side and the server side but there is no way in Java to be able to see how many bytes are in those buffers. You might be able to get it with some very specific kernel calls in C but it would be very non portable.</p> <ul> <li>One thing to consider is to use <code>Socket.setKeepAlive(true)</code> which will use the TCP mechanisms to keep the socket alive. I'm not sure how reliable that is from the server side but it should then get a -1 from the read when the socket times out after the client goes away.</li> <li>Another is to make a separate TCP connection for each ping. If that is too expensive then you can reconnect after a certain number of ping tries.</li> <li>A third thing to consider is to send UDP packets from the client which won't be buffered and are a lot less expensive. You should send more often since there is no retry as well.</li> <li>The server could also send back some sort of response so the client knows that the client knows the socket is still good. It can reconnect if it doesn't get the response.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload