I must have lost my mind because I cannot for the life of me see why
this is happening. I'm on linux 2.6, gcc compiler and code that is
too simple to be playing tricks like this.

The server reports:
Read 216809472 Bytes, Wrote 216809472 Bytes for 'longmetro05.raw'

The client reports:
Read 216809479 bytes, Wrote 216809479 bytes to 'longmetro05.raw'

Please point out how stupid I am and where the problem is.

Here is the client code:
int nTotalRead = 0, nTotalWritten = 0;
while(1) {
int nRead = read(socketfd, buf, sizeof(buf));
nTotalRead += nRead;
if(nRead > 0) {
int nWritten = write(fd, buf, nRead);
//printf("%d - %d\n", startat, nWritten);
nTotalWritten += nWritten;
if(nWritten != nRead) {
fprintf(stderr, "Error writing '%s'", argv[2]);
exit(-3);
}
} else if(nRead == 0) {
// Connection closed by other end
close(fd);
break;
} else if(nRead < 0) {
close(fd);
perror("Error reading socket");
exit(-4);
}
}

printf("Read %d bytes, Wrote %d bytes to '%s'\n", nTotalRead,
nTotalWritten, argv[2]);

Here is the server code:

int nTotalRead = 0, nTotalWritten=0;
while(1) {
int nRead = read(fd, bufr, sizeof(bufr));
nTotalRead += nRead;
if(nRead) {
lastOffset = lseek(fd, 0, SEEK_CUR);
int nWritten = write(pInfo->clientfd, bufr, nRead);
nTotalWritten += nWritten;
//printf("%d - %d\n", lastOffset, nWritten);
while(nWritten != nRead) {
fd_set writefds, exceptfds;
struct timeval timeout = {5, 500000};

FD_ZERO(&writefds); FD_ZERO(&exceptfds);
FD_SET(pInfo->clientfd, &writefds); FD_SET(pInfo->clientfd,
&exceptfds);

int nSelected = select(pInfo->clientfd+1, NULL, &writefds,
&exceptfds, &timeout);
if(nSelected) {
if(FD_ISSET(pInfo->clientfd, &writefds)) {
int nWrote = write(pInfo->clientfd, &bufr[nWritten], nRead-
nWritten);
//printf("%d %d\n", lastOffset, nWrote);
nWritten += nWrote;
nTotalWritten += nWrote;
}
} else {
if(FD_ISSET(pInfo->clientfd, &exceptfds)) {
close(fd);
return -10; // Exit thread
}
}
}
} else {
printf("Read %d Bytes, Wrote %d Bytes for '%s'\n", nTotalRead,
nTotalRead, cmd);
//We are finished
close(fd);
return -10;
}
}
}