iphone - How to set "KEEP ALIVE" for socket connection in iOS? -
we have made chat app iphone using java chat server.
we have used socket this. on ios side 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 app goes in background , socket disconnects. so, have make socket "keep alive"
.
on server side , have achieved socket.setkeepalive(true, 2000);
, cant able set on app (i.e ios) side.
we have searched on these links - link1 , link2
they have specified use code following 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 set keepalive! errno: %s", strerror(errno)); } }
we have tried code not able find vales of socket_handle, sol_socket, so_keepalive, &opt, sizeof( opt )
variables.
so how to make socket connection keep alive in ios (objective-c) ?
thanks.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h>
on .m file
Comments
Post a Comment