Re: reading a fixed-length message




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.


The bigger issue is that the code above is not portable; the message is
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....
No, your code is correct for any platform.
That's good to know. I've been wondering why the code works on both an
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

.



Relevant Pages

  • Re: reading a fixed-length message
    ... pktlength is length of query. ... 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? ... It doesn't disconnect, it doesn't close the connection, it just disappears. ... packed in big-endian order and the above code works purely by chance on ...
    (comp.unix.programmer)
  • Re: truncating files
    ... this sets the eof to the current eof, ... program's functionality through a buffer overflow attack. ... i wanted to put the while in front as i usually do but the compiler ... // fwrite returns error via a short count. ...
    (comp.sys.mac.programmer.help)
  • Re: detecting end of a direct access file
    ... but the EOF signal on exceding the assignd ... Not only does the standard not imply that an EOF should be returned for ... The *ONLY* way that a compiler can return an EOF on a direct ... and record sizes and formats, SAP 16-bit, IBM 12-bit binary and Qantum ...
    (comp.lang.fortran)
  • Re: CLOSE_WAIT vs. EOF
    ... The client cannot send a command at this point, ... What you need is to closethe socket. ... connection, but the local socket is still open. ... The proper thing to do is to read any input until EOF, ...
    (comp.os.linux.networking)
  • Re: HTTP transfer errors
    ... It's main purpose is to let browsers display ... connection close that indicates the end of the file. ... (whether to discard it, use the Content-Length, or use the EOF). ...
    (comp.arch.embedded)