Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 17 Nov 1996 12:30:49 -0500 (EST)
From:      "Marc G. Fournier" <scrappy@ki.net>
To:        hackers@freebsd.org
Subject:   Advise needed (readv()/writev())
Message-ID:  <Pine.NEB.3.95.961117121157.18169B-100000@quagmire.ki.net>

next in thread | raw e-mail | index | archive | help


Hi...

	I've pretty much gone through "Unix Network Programming" and
tried everything I can think of as relevant insofar as writing information
to a socket and then reading it back out at the other end...and have yet
to find a method of doing *exactly* what I'd like to do.

	What I want to do is send a block of data across a link (X bytes)
and have the other side read in X bytes.  Then, I want the sending send to
send over another X bytes, and the receiving end to receive another X bytes.

	The trick seems to be in getting the receiving end to *know* that
X bytes have been sent.

	Think of the following example.  

	I have a server that opens up a file that consists 1 page of
information, and each file acts as a page break.  I want the receiving end to
receive page one and write it to a file.  Then receive page two and write
that to a seperate file.

	Now, from what I've been able to accomplish, I can get sender to 
send one page, and receiver to receive one page, but if I get sender to
send two pages, then receiver receives both as one.

	What I've thought of doing was to use 'readv/writev', since then
I could send across a page as an iovec structure, have readv read it in,
process it and then go back and read the next page...and from what I've read
in the book, this *should* work...but writev is returning -1 for the following
sequence of events, which looks like:

    if( (fd = open(TESTFILE, O_RDONLY)) != -1) {
      fstat(fd, &fi); /* get file info...needed for file size */
      iov[0].iov_base = mmap(NULL, fi.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
      iov[0].iov_len = fi.st_size;
      if(iov[0].iov_base == (caddr_t) -1) {
        fprintf(stderr, "MMAP() failed\n");
        exit(1);
      }
      close(fd);
      n = writev(newsockfd, &iov[0], 1);
      printf("wrote %d bytes\n", n);
    } else {
      printf("failed to open: %s\n", TESTFILE);
    }

	So, can anyone suggestion what it is that I'm doing wrong here, that
the write is failing?  I'm pretty much blindly following the example in the
book, and can't see anything syntatically incorrect :(

PS. my understanding from "the book" is that with readv, I'm assured that
    the read will return with the number of bytes specified by iov_len, so
    that the multiple reads are hidden behind the function call...

Marc G. Fournier                                  scrappy@ki.net
Systems Administrator @ ki.net               scrappy@freebsd.org




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.NEB.3.95.961117121157.18169B-100000>