S
Scott W Gifford
Hello,
I'm working on a streaming data server which accepts a query, then
sends any matching data that comes in after the query was submitted.
The problem I'm having is that when a client sends a query then later
goes away, I'm not always able to detect the situation to cancel their
query. I can usually detect a client is gone when I get EOF when
reading or writing to it, but I have nothing to read after the initial
query, and as long as no data matches the query, nothing to write
either.
My implementation uses a BufferedReader and BufferedWriter to interact
with the client, so I can use readLine(). In general these will be
connected to a socket, but for testing they're connected to pipes.
In Perl or C, I can use select or a nonblocking file descriptor to
check if EOF has been reached. How can I do the same thing in Java?
I've tried using BufferedReader.ready() and BufferedReader.read(new
char[1], 0, 0), but neither indicates whether I've reached EOF. If I
instead use BufferedReader.read(new char[1], 0, 1), it tells me about
the EOF, but blocks. I tried creating a seperate thread to do a
blocking read, and that works fine for pipes, but for some reason it
breaks sockets (my writes all block). I've also looked at the nio
classes, but I'm reluctant to change my interfaces (currently I accept
a Reader/Writer pair or a Socket to communicate over), and anyways it
looks like massive overkill for detecting EOF.
Ideally I'd like a way to get interrupted when an EOF happens, by
interrupt() or an EOF exception or whatever. If that's not possible,
I can periodically poll the socket if I haven't written anything to
it.
Thanks for any ideas or hints!
----Scott.
I'm working on a streaming data server which accepts a query, then
sends any matching data that comes in after the query was submitted.
The problem I'm having is that when a client sends a query then later
goes away, I'm not always able to detect the situation to cancel their
query. I can usually detect a client is gone when I get EOF when
reading or writing to it, but I have nothing to read after the initial
query, and as long as no data matches the query, nothing to write
either.
My implementation uses a BufferedReader and BufferedWriter to interact
with the client, so I can use readLine(). In general these will be
connected to a socket, but for testing they're connected to pipes.
In Perl or C, I can use select or a nonblocking file descriptor to
check if EOF has been reached. How can I do the same thing in Java?
I've tried using BufferedReader.ready() and BufferedReader.read(new
char[1], 0, 0), but neither indicates whether I've reached EOF. If I
instead use BufferedReader.read(new char[1], 0, 1), it tells me about
the EOF, but blocks. I tried creating a seperate thread to do a
blocking read, and that works fine for pipes, but for some reason it
breaks sockets (my writes all block). I've also looked at the nio
classes, but I'm reluctant to change my interfaces (currently I accept
a Reader/Writer pair or a Socket to communicate over), and anyways it
looks like massive overkill for detecting EOF.
Ideally I'd like a way to get interrupted when an EOF happens, by
interrupt() or an EOF exception or whatever. If that's not possible,
I can periodically poll the socket if I haven't written anything to
it.
Thanks for any ideas or hints!
----Scott.