Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem sending large buffers with iovecs



I have some networking code ported from Solaris to Cocoa (OS X 10.4.8) that works, almost. I'm using writev() and readv() with TCP/ IP sockets to transmit datasets of size 230586 bytes each over the internet. The problem is, I don't receive all the bytes in a single readv() call; on OS X I have to call readv() multiple times to get all the data.

Sender code snippet:

    int byteswritten;
    char *outbuf = < pointer to my data >;
    size_t buflen = 230586;

    struct iovec out_iovec[1];
    out_iovec[0].iov_base = outbuf;
    out_iovec[0].iov_len  = buflen;

    byteswritten = writev(outsocket,out_iovec,1);
    printf("Status of writev: %d\n", byteswritten);
    close(outsocket);

Receiver code snippet:

    int bytesread,buflen,remaining;
    char *inbuf = calloc(1,230586);
    struct iovec in_iovec[1];

    size_t remaining = 230586;
    do {
        in_iovec[0].iov_base = &inbuf[buflen];
        in_iovec[0].iov_len  = remaining;

        bytesread = readv(insocket,in_iovec,1);
        printf("Status of readv: %d\n", bytesread);
        if (bytesread < 0) { perror("readv"); break; }
        remaining -= bytesread;
        buflen += bytesread;
    } while(remaining);
    close(insocket);

The single writev() call returns with a status of 230586, so it writes all the data in one call. But the receive loop reads 114324 bytes, then 82076, and finally 34186 bytes. The next time I might read 48996, 97992, 48996, and 34602 bytes. Etc. I'm no expert at this, so I'm probably not doing this right, but why is the loop necessary to receive all the data?

Or is there a preferred Cocoa way to accomplish sending large buffers?

Thanks,
Roland

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/macnetworkprog/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.