From: Martin Gregorie on 6 Oct 2009 07:20 On Tue, 06 Oct 2009 09:58:33 +0100, 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. > Why not piggyback the "are you alive/I'm alive" request/response on every every message? Done properly, this would only require a free-standing request or response to be sent if there is no other message to attach it to or to imply it. I think this approach gets round the problem of another message being ahead of the watchdog request/response in the queue. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
From: Mark on 6 Oct 2009 07:54 On 06 Oct 2009 10:41:09 GMT, Andreas Leitgeb <avl(a)gamma.logic.tuwien.ac.at> wrote: >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. That would be sufficient. >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. Good idea. I think this would work. It would mean that "are you alive" messages would be sent when not explictly requested but this would be relatively harmless.
From: Mark on 6 Oct 2009 08:09 On 06 Oct 2009 10:41:09 GMT, Andreas Leitgeb <avl(a)gamma.logic.tuwien.ac.at> wrote: >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. Actually, using a non-blocking SocketChannel could I just use a Selector and use SelectionKey.isReadable() to check if there is any data (without actually reading it)?
From: Peter Duniho on 6 Oct 2009 12:20 On Tue, 06 Oct 2009 05:09:39 -0700, Mark <i(a)dontgetlotsofspamanymore.net> wrote: > [...] > Actually, using a non-blocking SocketChannel could I just use a > Selector and use SelectionKey.isReadable() to check if there is > any data (without actually reading it)? Even using a plain Socket, you could look at the InputStream.available() return value. But IMHO the approach Andreas suggested is much better. The polling of the socket is pointless. If you are getting data from the socket, you have to be reading it anyway (and presumably as soon after data arrives as you can). You might as well just keep track of when the last time you successfully read from the socket, and if you must poll something, poll that time difference, rather than the socket itself. Pete
From: Knute Johnson on 6 Oct 2009 12:28
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. > > 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 think you are going about this the wrong way. If you have messages that you don't want to lose in the input buffer, read them, store them if you have to but read them. If the sending end of the socket is closed you will know that immediately. The sending end can also send a keep alive packet if it hasn't sent any data in some time period. If the receiving end doesn't get something from the sender every period and a half say, it disconnects and retries. The sender can even be stupid and just send a time hack. -- Knute Johnson email s/nospam/knute2009/ -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ------->>>>>>http://www.NewsDemon.com<<<<<<------ Unlimited Access, Anonymous Accounts, Uncensored Broadband Access |