Nsstream Tcp Keep-Alive iOS

Is it possible to activate TCP keepalive on Apple iOS devices

see: http://en.wikipedia.org/wiki/Keepalive#TCP_keepalive

Usually, keepalive time (net.inet.tcp.keepidle) is 7200sec by default.
I don't know that is true in iOS or not, but "no less than 2 hours" is clearly required by RFC 1122.

This interval MUST be
configurable and MUST default to no less than two hours.

So, how could you configure it in your application? try:

#import <sys/types.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <netinet/tcp.h>

int on = 1;
int delay = 120;
setsockopt(socket_handle, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
setsockopt(socket_handle, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay));

see man tcp.

I confirmed this works on iOS Simulator (iPhone 5s / iOS 8.0).

Swift: TCP socket keep-alive

CFDataRef is "toll-free bridged" to NSData, therefore the first part can be
more simply written as

var socketData = CFReadStreamCopyProperty(inputStream, kCFStreamPropertySocketNativeHandle) as NSData
var socket: CFSocketNativeHandle = 0
socketData.getBytes(&socket, length: sizeofValue(socket))

The "option_value" argument of setsockopt() is the address of an int (which is mapped to Swift as UInt32), and the last argument has to be the size of that variable:

var on: UInt32 = 1;
if setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &on, socklen_t(sizeofValue(on))) == -1 {
let errmsg = String.fromCString(strerror(errno))
println("setsockopt failed: \(errno) \(errmsg)")
}

<sys/socket.h> is imported by default.

iOS NSStream tcp server client can't communicate

It seems, that you use two variables currentDataOffset -- an instance variable and a property -- for the same purpose. If you indeed need both of them, you should set 0 to currentDataOffset as well. But I think the fragment should look like:

readBytes += currentDataOffset;
NSUInteger dataLength = [data length];
NSUInteger lengthOfDataToWrite = MIN(dataLength - currentDataOffset, 1024);
NSInteger bytesWritten = [_outputStream write:readBytes maxLength:lengthOfDataToWrite];
if (bytesWritten > 0) {
currentDataOffset += bytesWritten;
if (currentDataOffset == dataLength) {
[self.dataWriteQueue removeLastObject];
currentDataOffset = 0;
}
}

Async NSURLConnection and keep-alive

After some fiddling, got Wireshark to work on Mac. The TCP conversation is unbroken between requests - to me, this suggests keep-alive connections are working as advertised. iOS 7.0.3 on simulator.

iOS NSStream Socket Read TCP erratic and truncated

TCP streams are just that, byte streams, they are inherently not message oriented and individual network packets may be reorganized, split, and merged at will. The only thing that is guaranteed from end-to-end is that all of the bytes will arrive in the order they were sent. You'll need to handle buffering partial lines (or more frequently combined lines) yourself by splitting them out on newlines and saving partial results.



Related Topics



Leave a reply



Submit