Why am I getting extra bytes? - TCP-IP

This is a discussion on Why am I getting extra bytes? - TCP-IP ; 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: ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Why am I getting extra bytes?

  1. Why am I getting extra bytes?

    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;
    }
    }
    }


  2. Re: Why am I getting extra bytes?

    Mark0978@gmail.com wrote:
    > I must have lost my mind because I cannot for the life of me see why
    > this is happening.

    ....
    > Here is the server code:

    ....
    > } else {
    > printf("Read %d Bytes, Wrote %d Bytes for
    > '%s'\n", nTotalRead, nTotalRead, cmd);


    Not the main issue, but in the above printf you are using nTotalRead for
    the second argument in the final server printout when you intended to use
    nTotalWritten.

    Also, in the server code, where you use the following conditional:

    if(nRead) {

    try changing that to:

    if(nRead > 0) {

    Otherwise a read() returning a -1 error into nRead will cause problems.
    Likewise, place the line:

    nTotalRead += nRead;

    inside the above conditional. The reason, again, is that you are doing the
    addition before you've checked to see if it is valid and not reporting an
    error. (Same issue exists in your client code - you add nRead and nWritten
    to the totals _before_ you've checked to see if they are non-negative.)

  3. Re: Why am I getting extra bytes?

    Jim Logajan writes:

    > Mark0978@gmail.com wrote:
    >> I must have lost my mind because I cannot for the life of me see why
    >> this is happening.
    >> ...

    > ...
    >
    > Also, in the server code, where you use the following conditional:
    >
    > if(nRead) {
    >
    > try changing that to:
    >
    > if(nRead > 0) {
    >
    > Otherwise a read() returning a -1 error into nRead will cause problems.


    The problem Jim points out would neatly explain the problem: if read()
    *did* return an error, the bytes read count would be decremented and a
    write() of MAX_INT bytes would have followed. The would also fail
    because of the excessive size of the request or a memory protection
    fault, so the write() would also return -1, and the write count
    (although not reported, as you point out) would also be decremented.

    The check for nRead != nWrite would fail -- they're both -1 -- so the
    loop will go back for another read().

    Hard to say whether it's possible for read() to return an error in
    this particular program without more info, but 7 such transient errors
    would explain the deficient of 7 in the read byte count. This could be
    confirmed if we knew whether the server's number or the client's
    number represents the correct count of bytes read from the source or
    written to the final destination.

    -don provan



  4. Re: Why am I getting extra bytes?

    don provan wrote:

    > Hard to say whether it's possible for read() to return an error in
    > this particular program without more info


    Any socket read() call can return an error any time, nothing to do with
    the program. It's a network.

  5. Re: Why am I getting extra bytes?

    Esmond Pitt writes:

    > don provan wrote:
    >
    >> Hard to say whether it's possible for read() to return an error in
    >> this particular program without more info

    >
    > Any socket read() call can return an error any time, nothing to do
    > with the program. It's a network.


    Of course, in this case we were speaking of a transient error,
    one followed immediately by a successful read. Are you thinking
    of a case like that that can happen at any time on a TCP socket
    because of its network nature?

    It's true that this can happen for a few reasons in most socket
    environments, but the reasons I know about are sparked by local
    events in the system, not by external network activity.

    -don

+ Reply to Thread