On Tue, 06 Oct 2009 10:24:59 -0700, Daniel Pitts
<newsgroup.spamfilter@virtualinfinity.net> wrote:
Mark wrote:
Is it possible to do a non-destructive read on a socket in Java? I
want the functionality similar to the C library recv() using the
MSG_PEEK flag.
More information:
I am implementing an API in Java which needs to send a "are you alive"
application message over a socket. In reply it will get a "I am
alive" message. However there may be another type of message already
waiting which the API must not read. The presence of any message will
be enough for the API call to succeed. The "I am alive" message can
just be discarded if it is read later.
Rather than either of those, why not use a message dispatcher that reads
any available message, and dispatches it accordingly, including the "I
am alive" message.
The problem is that there may not be anything that can consume some
types of message at that time. The API is defined this way.
For example there are API methods defined, including:
test() - Send a keepalive message and peek into the tcp/ip buffer to
see if there is a response.
get() - Read an application message (if present).
If the consumer calls test() then there is nothing to handle an
application message.
So you stick it in a queue. Next time someone calls get(), they get the
message from the queue. Simple.