Can any of the boffins here help with the following. I'm learning sockets with a book i found online called Definitive Guide to Linux Network Programming. The section of code I am struggling with is for a server which reads a file with handle fd and sends it through the socket socket2.

Things are slightly more complicated since I'm also translating the code from Posix functions to Ansi-c.

This is the original code.

Code:
    int fd;
    int readCounter, writeCounter;
    char* bufptr;
    char buf[MAXBUF];
    
    while((readCounter = read(fd, buf, MAXBUF)) > 0)
    {
        writeCounter = 0;
        bufptr = buf;
        while (writeCounter < readCounter)
        {
            readCounter -= writeCounter;
            bufptr += writeCounter;
            writeCounter = write(socket2, bufptr, readCounter);
            if (writeCounter == -1)
            {
                fprintf(stderr, "Could not write file to client!\n");
                close(socket2);
                continue;
            }
        }
    }

This is the translation into ansi-c, which works ok.


Code:
    int readCounter, writeCounter;
    char* bufptr;
    char buf[MAXBUF];
    FILE *fd;

    /* read the file, and send it to the client in chunks of size MAXBUF */
    while((readCounter = fread(buf,1, MAXBUF,fd)) > 0)
    {
        writeCounter = 0;
        bufptr = buf;
        while (writeCounter < readCounter)
        {
            readCounter -= writeCounter;
            bufptr += writeCounter;
            writeCounter = send(socket2, bufptr, readCounter,0);
            if (writeCounter == -1)
            {
                fprintf(stderr, "Could not write file to client!\n");
                closesocket(socket2);
                continue;
            }
        }
    }
But I'm unsure of the code. So I just tried the following and it also works fine.

Code:
    /* read the file, and send it to the client in chunks of size MAXBUF */
    while((readCounter = fread(buf,1, MAXBUF,fd)) > 0)
    {

            writeCounter = send(socket2, buf, readCounter,0);
            if (writeCounter == -1)
            {
                fprintf(stderr, "Could not write file to client!\n");
                closesocket(socket2);
                continue;
            }

    }
So what IS the additional code of the original doing?