[comp.unix.sysv386] reading n bytes from stream socket

mjhammel@Kepler.dell.com (Michael J. Hammel) (12/04/90)

Hope this is the right place for this:

I have a stream socket, s, in use on a client from which I wish to read
n bytes. 
I have no way of knowing if that socket has n bytes available for reading.  How
do I guarantee that I can get that many bytes from that socket?  I know that
the server will (when it catches up to the clients reads) be sending the data,
I just need to know when its all gotten there.  Maybe have the read()
block until
it gets all it requested?  But I don't know how to do this (isn't the read() 
blocked by default? ie don't you have to specifically set the read to be 
non-blocking with some sort of ioctl()?)

In Steven's _UNIX Network Programming_ he lists an ioctl() request called
FIONREAD which, according to him, should allow me to find out how many bytes
are available to read from a file descriptor.  He also says this should work
with sockets as well.  But I can't find where FIONREAD is defined, so can't
use it (yet).

There are some other calls (like select() or poll()) that can tell me when
theres data in the socket, but they don't tell me how much is there.  Is it
possible to find out how much is there?

The following was said to wait either WAITTIME time intervals or
till MINCHARS showed up on s (which is just what I need), but when its 
compiled I get "invalid argument" for the ioctl() call.  TCGETA is valid (I
checked).  My use of pointers could be wrong for term or theres something
wrong with using a socket descriptor.  Any guesses?

#include <stdio.h>
#include <fcntl.h>
#include <sys/termio.h>

#define MINCHARS	40
#define WAITTIME	1

struct termio term;

	if (ioctl(s, TCGETA, &term) < 0) {
		perror("ioctl");
		exit(1);
	}

	term.c_cflag = speed | CLOCAL | HUPCL | CREAD | CS8 | CSTOPB;
	term.c_iflag = term.c_oflag = term.c_lflag = 0;
	term.c_cc[VMIN] = MINCHARS;
	term.c_cc[VTIME] = WAITTIME;

	if (ioctl(s, TCSETA, &term) < 0) {
		perror("ioctl");
		exit(1);
	}

Michael J. Hammel        | mjhammel@{Kepler|socrates}.dell.com
Dell Computer Corp.      | {73377.3467|76424.3024}@compuserve.com
#include <disclaim/std>  | zzham@ttuvm1.bitnet | uunet!uudell!feynman!mjhammel
"oh oh, kwyjeebo on the loose!"