0
votes

I set my .plist to run the application while background and I've set up the connection like this:

CFReadStreamRef     readStream;
CFWriteStreamRef    writeStream;

//open c socket:
int rc; // ret code of s.c. 
int my_socket;
struct sockaddr_in dest_addr;

my_socket = socket(AF_INET, SOCK_STREAM, 0); 
assert(my_socket >= 0);

dest_addr.sin_family = AF_INET; 
dest_addr.sin_port = htons(SERVER_LOC_PORT); 
dest_addr.sin_addr.s_addr = inet_addr(SERVER_IP); 

memset(dest_addr.sin_zero, 
       '\0',
       sizeof dest_addr.sin_zero);

rc = connect(my_socket,
             (struct sockaddr *)&dest_addr,
             (socklen_t) sizeof(dest_addr));

if(rc != 0){
    return NO ;
}

NSLog(@"Start connection");

// Open a stream based on the existing socket.  Then configure 
// the stream for async operation.

CFStreamCreatePairWithSocket(NULL, my_socket, &readStream, &writeStream);
assert(readStream != NULL);

self.networkStreamIn = (NSInputStream *) readStream;
self.networkStreamOut = (NSOutputStream *) writeStream;

//releasers
CFRelease(readStream);
CFRelease(writeStream);

self.networkStreamIn.delegate = self;
self.networkStreamOut.delegate = self;
BOOL res = [self.networkStreamIn setProperty:NSStreamNetworkServiceTypeVoIP 
                   forKey:NSStreamNetworkServiceType];

if(!res){
    NSLog(@"Cannot connect") ;
    // @TODO: Error handling here
}

[self.networkStreamIn scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[self.networkStreamIn open];
[self.networkStreamOut open];

Also I set my stream() to be called when data is received. Then I read an write from/in server. It's work. on backgrount too. but after few minutes (in backgound mode only!) the server print into the log "java.net.SocketTimeoutException: Read timed out". and no more connection with the client (iOS device).
I've not set background handler to retiain connection - but it should be work without this handler (I think..)!

Did I missed something with this socket configurations?

1

1 Answers

0
votes

VoIP app has restrictions at background. In short:

  • While app is at background, it is suspended most of the time
  • While it is suspended, the OS keeps your socket alive
  • App wakes up from suspention for: incoming data, network changes and performing KeepAlive to allow the server know its alive
  • While Audio is active (There is an active VoIP call), app may run fully at background.

Here is a link to a more detailed answer on another thread: VoIP app behavior at background