0

We have made the chat app for iphone using java chat server.

We have used socket for this. On iOS side we have used following code ;

#import <CFNetwork/CFNetwork.h>
#import <sys/socket.h>
#import <sys/types.h>

- (void) viewDidLoad
{
     [self initNetworkCommunication];
}

#pragma mark Network connection
- (void) initNetworkCommunication
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"xx.xx.xx.xx", xxxx, &readStream, &writeStream);

    inputStream = (NSInputStream *)readStream;
    outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];

}

But whenever the app goes in background , the socket disconnects. So, we have to make socket "KEEP ALIVE".

On server side , we have achieved this by socket.setKeepAlive(true, 2000); , but we cant be able to set this on app (i.e iOS) side.

We have searched on these links - Link1 and Link2

They have specified to use code like following to make socket keep alive ;

CFDataRef data = (CFDataRef)CFWriteStreamCopyProperty(( CFWriteStreamRef)outputStream, kCFStreamPropertySocketNativeHandle);

    if(data)
    {
        CFSocketNativeHandle socket_handle = *(CFSocketNativeHandle *)CFDataGetBytePtr(data);
        CFRelease(data);

            NSLog(@"SOCK HANDLE: %x", socket_handle);

            //Enabling keep alive
        if( setsockopt( socket_handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof( opt ) ) < 0 )
        {
            NSLog(@"Yikes 2: failed to set keepalive! ERRNO: %s", strerror(errno));
        }
   }

We have tried this code but could not be able to find the vales of socket_handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof( opt ) variables.

So how to to make socket connection keep alive in ios (objective-c) ?

Thanks.

Community
  • 1
  • 1
Rohan
  • 2,939
  • 5
  • 36
  • 65
  • [this](http://stackoverflow.com/questions/11387560/keeping-a-socket-connection-alive-in-ios) post may help you, let me know the insight. good luck – Dipen Panchasara May 14 '13 at 05:52
  • @DipenPanchasara: That is "Link1" mentioned in the question :-) – Martin R May 14 '13 at 05:54
  • Does this help: http://stackoverflow.com/a/15787080/1187415 ? - And don't forget to `#include `. – Martin R May 14 '13 at 05:56
  • Did you try setting the opt value to 1? – Michele Percich May 14 '13 at 06:04
  • i found one post regarding this, [refer here](http://stackoverflow.com/questions/5987495/how-to-maintain-voip-socket-connection-in-background) – Dipen Panchasara May 14 '13 at 06:15
  • @MartinR thanks for reply. I have just read out your answer for the same problem. I have also tried that , but could not solved my problem. – Rohan May 14 '13 at 06:16
  • @MichelePercich - thanks for reply. I have set opt value to 1. But not helpful. – Rohan May 14 '13 at 06:18
  • did it help or still same problem? – Dipen Panchasara May 14 '13 at 06:20
  • 1
    What exactly is your problem? Does the code not compile, do you get errors, or does it not work as expected. - Note that SO_KEEPALIVE is only a method to *check* that the remote endpoint of the connection is still reachable and alive. Setting SO_KEEPALIVE does not prevent the iOS app from going into the background! - See also http://developer.apple.com/library/ios/#technotes/tn2277/_index.html: *"When the app is suspended, no code within the app's process executes. This makes it impossible for the app to handle incoming network data."* – Martin R May 14 '13 at 06:44
  • The problem is that whenever the app goes in background, the socket goes disconnected. I want my **socket to be alive even when the app goes in background or when the app is untouched for certain time (approx 5 min)**. – Rohan May 14 '13 at 06:55
  • 1
    @Rohan: That is not possible, if I understand "Technical Note TN2277" (link in my previous comment) correctly. You probably have to reconnect if necessary. - The only other possibility is to set UIBackgroundModes in Info.plist (as mentioned in the answer linked by Dipen Panchasara) to let the app continue running in the background. But that mode is restricted to special services (such as Voice-over-IP, ...), compare http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW22. – Martin R May 14 '13 at 07:09
  • @MartinR - Means should I re-call the **initNetworkCommunication()** method in app delegate's **didEnterBackground()** method ? – Rohan May 14 '13 at 07:15
  • 2
    @Rohan: I assume that you should close the connection in `didEnterBackground` and reopen in `willEnterForeground`, but I cannot really advise you here. "Technical Note TN2277" seems to contain valuable information, perhaps you start with that. - Your question was "how to set SO_KEEPALIVE on a socket", but your real problem seems to be "how to handle open connections when the app goes into background". That are different things, so you might consider updating your question or perhaps start at new one. – Martin R May 14 '13 at 07:42
  • So, is there a way to make a socket a 'voip' socket through setsockopt() ? – Paul Praet Mar 25 '14 at 08:24

1 Answers1

0
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

on your .m file

Hardik Mamtora
  • 1,642
  • 17
  • 23