cocoansdatatcpsocketnsfilehandlenssocketport

Send more than 32768 Bytes at once from one TCP-Socket to an other


Hey guys, I don't know if this question has been asked yet but imagine following situation:

I have two TCP-Sockets (opened with NSSocketPort und listening with two NSFileHandle) and now I want to send some NSData between them.

@try {
    [fileHandle writeData:data];
}
@catch (NSException * e) {
    // Do some alert
}

Everything is right until I want to send a NSData instance with more than a length of 32768 Bytes. More than this number of bytes will not be transfered. So here are my questions:

1) Why isn't Cocoa able to send more than 32768 Bytes at once?
2) Do I have to make workaround?
3) If yes, I would split the data, but how would you do it? And how would the other socket know when all data is sent?

By the way, after sending this single NSData instance both sockets should be closed again.


Solution

  • The amount of data sent at a time depends on the size of the buffer which the underlying frameworks and libraries use. While it may be configurable, it's mostly irrelevant. The advantage of TCP is that it either guarantees to deliver your data (in one or more packets) or fails gracefully.

    1. You don't have to split your data before sending. The underlying system will do that for you.
    2. On the receiving end you can read the available data, then wait until more bytes arrive, process them, and so on, until no more data is available. When the sender completes sending its data, it will close the socket and the receiver will get notified.