Re: reading a fixed-length message



Jason Curl <j.m.curl@xxxxxxxxxxxxxxx> writes:

[...]

You've already said you know the maximum size of the message. Why
don't you use the select() function to wait when there is incoming
data from the socket stream?

The purpose of select is synchronous I/O multiplexing, meaning, if you
do not have at least two descriptors to deal with, it just adds overhead.

[...]

I already use this mechanism to wait for input on stdio, sockets,
pipes and serial ports simultaneously. It's good when handling signals
too (i.e. ctrl-c) as select() will interrupt allowing you to clean up
properly (close the socket, serial port, etc.)

If you do not want to risk losing signals ('lost wakeup'), those you
handle should be blocked during 'normal' execution and you should use
pselect instead of select.

You can then leave your sockets as blocking for read (select will
return when there actually is data to read).

This may backfire, depending on the descriptors you are using. An
example from one of the Stevens books: A descriptor refers to a
listening socket and a connection arrives. 'select' returns with the
descriptor marked as readable, but the other end closes the connection
again before the listenting process has called accept => process
blocks in accept.

So a single threaded application can look to behave managing multiple
inputs.

e.g. (psuedo code)

while(1) {
populate your fd set (for read, write and exceptions)
select()
if socketfd for read
read in all available data
check buffer if it's valid
if serialfd for write (assuming you've got something to write)
write data over serial
if serialfd for read
read data from serial port
if socketfd for write (assuming you've read something from serial)
write data over tcp/ip
}

A nicer loop:

initialize pollfd array;
while (1) {
poll;
process I/O
}

.



Relevant Pages

  • Re: reading a fixed-length message
    ... It's good when handling signals too as selectwill interrupt allowing you to clean up properly (close the socket, serial port, etc.) ... if serialfd for write ... 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. ...
    (comp.unix.programmer)
  • [patch] SOCK_CLOEXEC, SOCK_NONBLOCK and MSG_CMSG_CLOEXEC
    ... MSG_CMSG_CLOEXEC to recvmsgmakes file descriptors ... I do not pass the SOCK_* flags to MAC because this may cause incorrect ... +.Xr socket 2, ...
    (freebsd-hackers)
  • Re: Good MB for a mid-range PC?
    ... Probably something Socket 939, then. ... > - Serial port and 2 ATA ports ... > - Onboard sound is preferred ... only single-channel and current/future CPUs ...
    (alt.comp.periphs.mainboard.asus)
  • Re: How to access a device on a LAN?
    ... serial port, using a file descriptor provided by the openfunction. ... a socket with the socketfunction and then call connect. ... or a book like W. Richard Stevens' "Unix Network Programming". ...
    (comp.os.linux.development.apps)
  • Re: Multiple handlers/nonblocking question
    ... I develop a tcl application with both socket and serial port connections. ... vwait problems. ... As all event handlers are checked on vwait, ...
    (comp.lang.tcl)