From owner-freebsd-hackers Sun Nov 17 09:31:08 1996 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id JAA16233 for hackers-outgoing; Sun, 17 Nov 1996 09:31:08 -0800 (PST) Received: from quagmire.ki.net (root@quagmire.ki.net [205.150.102.1]) by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id JAA16211 for ; Sun, 17 Nov 1996 09:30:51 -0800 (PST) Received: from localhost (scrappy@localhost) by quagmire.ki.net (8.8.2/8.7.5) with SMTP id MAA19670 for ; Sun, 17 Nov 1996 12:30:50 -0500 (EST) Date: Sun, 17 Nov 1996 12:30:49 -0500 (EST) From: "Marc G. Fournier" To: hackers@freebsd.org Subject: Advise needed (readv()/writev()) Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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