From: Mark on 6 Oct 2009 04:58 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. I realize I could buffer the messages internally, but I would prefer to avoid this if possible owning to the risk of losing a message if the application is terminated.
From: Lothar Kimmeringer on 6 Oct 2009 05:21 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. Not officially, but you can give out the classname of the inputstream returned by Socket and check the sources if there is a method that does this kind of thing (maybe to implement available()) But be aware that you leave the trails of the official API- definition, so your code might break with different versions of the JVM. Regards, Lothar -- Lothar Kimmeringer E-Mail: spamfang(a)kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81) Always remember: The answer is forty-two, there can only be wrong questions!
From: Peter Duniho on 6 Oct 2009 05:28 On Tue, 06 Oct 2009 01:58:33 -0700, Mark <i(a)dontgetlotsofspamanymore.net> 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. AFAIK, no. > [...] > I realize I could buffer the messages internally, but I would prefer > to avoid this if possible owning to the risk of losing a message if > the application is terminated. I don't see how using MSG_PEEK changes the risk of losing a message. And in fact, it can cause other problems, including the failure to ever receive any message. It's just not a good thing to do. Here's an article from MSDN that discusses the issue a bit: http://support.microsoft.com/kb/192599 It's specific to the Windows Winsock API, but for sure the same issues would apply if you had access to MSG_PEEK via Java on Windows (because Java is built on the OS API), and as far as I can recall, even the BSD socket API doesn't provide any more guarantees regarding the operation of MSG_PEEK than Winsock does. So a Unix or Linux platform, and probably others for that matter, would have the same issue. Don't use MSG_PEEK, even outside of Java. It's not solving the problem you seem to think it is, and even if it did, it would create more headaches than it would fix. Fortunately, unless I'm mistaken, you don't have the option in Java to do so. :) Pete
From: GArlington on 6 Oct 2009 06:23 On 6 Oct, 09:58, Mark <i...(a)dontgetlotsofspamanymore.net> 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. > > I realize I could buffer the messages internally, but I would prefer > to avoid this if possible owning to the risk of losing a message if > the application is terminated. Just my thought on that... What should happen if there ARE other messages in your incoming queue and you send "are you alive" query and do NOT get a reply? All those messages should still be processed, aren't they? If they are then you should only send your "are you alive" query when there are NO messages in your incoming queue for some time, and as soon as there ARE ANY you can assume that the sender IS alive...
From: Andreas Leitgeb on 6 Oct 2009 06:41
Peter Duniho <NpOeStPeAdM(a)nnowslpianmk.com> wrote: > On Tue, 06 Oct 2009 01:58:33 -0700, Mark <i(a)dontgetlotsofspamanymore.net> > 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. > AFAIK, no. > Here's an article from MSDN that discusses the issue a bit: > http://support.microsoft.com/kb/192599 I think this doesn't really apply to the OP, as he only wants to see if "at least *anything*" is there, no reliable counts. Nevertheless, a better approach would be to have the real consumer update some time-stamp on receiving the "Yes, I'm alive"-message, or even better for any message it gets, and some watchdog watches that time stamp and if it's further back than threshold A, then it sends the "Are you alive"-poll, and if it's even further back than threshold B, then it will assume the other party is no longer alive/connected/ willing-to-respond. The difference between A and B should be large enough to allow for a round-trip of packets plus maximum reaction-time for the other party. |