Re: reading a fixed-length message
- From: "toby" <toby@xxxxxxxxxxxxxxxxxxx>
- Date: 9 Jan 2007 20:37:14 -0800
CptDondo wrote:
toby wrote:
Oops, ignore what I wrote, I didn't properly look at your code. If
you're using [4] and [5] to compute the packet length, you should be
waiting for six bytes to arrive, not five?
Well, the length of the message is contained in bytes 4 and 5.... So I
need to read until I get those bytes, then read until I get the length.
Ah yes, in your loop, i is *one fewer* than bytes read. My bad.
(No, I didn't make up this protocol....)
...
OK, thanks.... One more question:
It seems to me that you're still relying on an EOF; I'm not sure if an
EOF is generated under all circumstances in a timely fashion?
That's right; if the data stops arriving, the loop will block. If other
processing is to continue, you either need threading or a nonblocking
read, as you figured.
Assume,
for example, that the client sends 3 bytes, then hangs. It doesn't
disconnect, it doesn't close the connection, it just disappears. Would
an EOF be generated in this case, or would the connection simply hang
for some amount of time (or forever?)
We are on an extremely unreliable, congested network, so unreliable
connections are the norm. We have something like 600 clients on a wi-fi
network spanning a hundred square miles. The whole system is automated,
so reliability is essential.
The protocol we're using specifies very short timeouts; if the message
isn't completed in a very short time we throw it away, so a timeout of 5
sec would be sufficient.
That's good to know. I've been wondering why the code works on both anThe bigger issue is that the code above is not portable; the message isNo, your code is correct for any platform.
packed in big-endian order and the above code works purely by chance on
my platform. I'd like a big-endian-to-host-short function, something
like ntohs....
intel and an arm platform.... I take it the compiler generates the
appropriate code?
The compiler only has to compute the expression you've specified,
nothing more. You have told it in an endian-independent way how to
compute the right value from 2 bytes in BigEndian order. I would do it
exactly the same way (if tcpreq[x] is unsigned).
Interesting.... I guess I overthought that one, eh? :-)
Better safe than sorry. :)
--Yan
.
- References:
- reading a fixed-length message
- From: CptDondo
- Re: reading a fixed-length message
- From: toby
- Re: reading a fixed-length message
- From: CptDondo
- Re: reading a fixed-length message
- From: toby
- Re: reading a fixed-length message
- From: CptDondo
- reading a fixed-length message
- Prev by Date: Re: Which data store to use for fast, highly available access?
- Next by Date: Re: I need the technical explanation to this terminal behavior..
- Previous by thread: Re: reading a fixed-length message
- Next by thread: Re: reading a fixed-length message
- Index(es):
Relevant Pages
|