From: Felce e Mirtillo on
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... ???
Any Idea ???
Thanks in advance...


Dario
(I apologize for my poor english.. I hope it is better than your average
italian :P :P )
From: Roedy Green on
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.

See http://mindprod.com/jgloss/http.html

--
Roedy Green Canadian Mind Products
http://mindprod.com

It�s amazing how much structure natural languages have when you consider who speaks them and how they evolved.
From: Mike Amling on
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.

--Mike Amling
From: Mike Amling on
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... ???
> Any Idea ???
> Thanks in advance...


You need some kind of ping to make sure the device. When the device
is turned off, all the control blocks for the TCP connection on the
machine running Java remain valid. If no packets are being exchanged,
there's no indication that anything is wrong.
Is there a harmless message you could send to the device that would
have no side effects? If so, send it every once in a while from a second
Thread, and after the device is off, you should get an I/O error on the
send, which will probably cause your blocked read to get either
end-of-file or an I/O error.

There's also a Socket.setKeepAlive(true), but I don't know exactly
what it's defined to do.

--Mike Amling
From: Mike Amling on
Mike Amling wrote:
>
> You need some kind of ping to make sure the device.

...to make sure the device is still running.