From: Peter Duniho on 20 Apr 2010 11:25 Mike Amling wrote: > EJP wrote: >> On 20/04/2010 12:13 AM, Mike Amling wrote: >>> >>> You need some kind of ping to make sure the device. >> >> No you don't, you just need a read timeout. Socket.setSoTimeout(). > > A read timeout does not distinguish between a dead device and a > healthy device that has nothing to say. That's true, and ordinarily I would be with you in suggesting that setting a timeout is inappropriate. It's usually not the right way to handle things (especially since a socket timeout, at least with Winsock, renders the socket useless after the timeout…there's no way to just ask the user "should the code wait some more?"). But in this case, the original problem statement seems to involve a very specific device, and a loop reading from it where there is no such thing as "…a healthy device that has nothing to say". That is, the loop that's getting blocked is always expected to run to completion given a healthy device and connection. It seems to me that the advice offered by "EJP" here is a pretty good solution. Especially in contrast to using something like the keep-alive feature in TCP, which by default will take a couple of _hours_ to detect a broken connection, or the incorrect suggestion that without changing anything, a broken connection will eventually be detected (it really won't, not until the local endpoint attempts to send data). Pete
From: RedGrittyBrick on 20 Apr 2010 12:42 On 20/04/2010 11:39, Roedy Green wrote: > On Mon, 19 Apr 2010 12:10:10 +0200, Felce e Mirtillo > <dario.fantoni(a)gmail.com> wrote, quoted or indirectly quoted someone > who said : > >> count = socket.getInputStream().read(); >> while (count> 0&& count != 3) { > > you are going two layers lower protocol layer than you need. Two? What layer of the TCP/IP protocol suite lies between TCP and HTTP? > > See http://mindprod.com/jgloss/http.html What makes you think his industrial balance speaks HTTP? Aren't newbie onlookers more likely to be confused by this answer than helped? Just my ¤0.02 worth. -- RGB
From: Arne Vajhøj on 19 Apr 2010 19:48 On 19-04-2010 06:10, Felce e Mirtillo wrote: > Hi all, > > I'm writing an application to interface a device ( industrial balance ) > to a PC via TCP/IP... well the device acts as Server and my application > is a listening client. > > Th client receive a single string > <stx>dd/mm/yyyy;hh:mm;0000000000;A;0000000000<etx> > that I read with... a method like this: > > ... > byte[] buffer = new byte[100]; > int count = 0, offset = 0; > > count = socket.getInputStream().read(); > while (count > 0 && count != 3) { > buffer[offset++] = count; > count = socket.getInputStream().read(); > } > String line = new String(buffer, 1, offset); > String[] dati = new String[5] > offset = 0; > for(StringTokenizer t = new StringTokenizer(line, ";"); > t.hasMoreTokens();) dati[offset++] = t.nextToken(); > ... > > well the method is in a loop where I'd would trap > some events. The most important is... if someone > swich off the device and then switch off... the > previous method remain 'blocked' waiting the next > byte ... > I've tryed several Socket methods.. but no one of > them is usefull in order to intercept the death > of tcp/ip connection... > > What can I do... ??? Eventually you will get an exception, but it takes time. TCP/IP was designed to be able to handle slow unreliable network, so it is patient. To lower it you could try: socket.setSoTimeout(1000); Arne PS: I think the code would be simpler if you wrapped the InputStream in a BufferedReader and used String.split instead of StringTokenizer. > (I apologize for my poor english.. I hope it is better than your average > italian :P :P )
From: EJP on 19 Apr 2010 20:13 On 20/04/2010 12:13 AM, Mike Amling wrote: > > You need some kind of ping to make sure the device. No you don't, you just need a read timeout. Socket.setSoTimeout().
From: Arne Vajhøj on 20 Apr 2010 20:14 On 20-04-2010 12:42, RedGrittyBrick wrote: > On 20/04/2010 11:39, Roedy Green wrote: >> On Mon, 19 Apr 2010 12:10:10 +0200, Felce e Mirtillo >> <dario.fantoni(a)gmail.com> wrote, quoted or indirectly quoted someone >> who said : >> >>> count = socket.getInputStream().read(); >>> while (count> 0&& count != 3) { >> >> you are going two layers lower protocol layer than you need. > > Two? What layer of the TCP/IP protocol suite lies between TCP and HTTP? > >> >> See http://mindprod.com/jgloss/http.html > > What makes you think his industrial balance speaks HTTP? > > Aren't newbie onlookers more likely to be confused by this answer than > helped? But he got posted a link to his web site, which seems to be more important for him than to relate to the question. Arne
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How to set version to jar? Next: Runtime.getRuntime().exec |