How to maintain the socket connection in open status and send datas repeatedly
How to maintain the socket connection in open status and send datas repeatedly
- Subject: How to maintain the socket connection in open status and send datas repeatedly
- From: email@hidden
- Date: Tue, 25 Aug 2009 10:04:04 +0000
Hi all,
I need to connect the server using socket connection and need to send the heartbeat at particular time intervals in the same opened connection. I created the socket connection using the CFSocket. Here is my code.
#import "CFSocketController.h"
#import "GetPrimaryMacAddress.h"
@implementation CFSocketController
NSMutableData *mdata;
static void ReadStreamClientCallBack(CFReadStreamRef stream, CFStreamEventType type, void *clientCallBackInfo ) {
NSLog(@"entering end");
switch (type)
{
case kCFStreamEventEndEncountered:
{
CFReadStreamClose(stream);
break;
}
case kCFStreamEventErrorOccurred:
break;
case kCFStreamEventHasBytesAvailable:
{
if ( !mdata ) {
mdata = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
while([stream hasBytesAvailable]){
len = [(NSInputStream *)stream read:buf maxLength:1024];
NSLog(@"length:%@",[[NSString alloc]initWithFormat:@"%i",len]);
if (len ) {
[mdata appendBytes:(const void *)buf length:len];
NSString *response=[[NSString alloc]initWithData:mdata encoding:NSASCIIStringEncoding];
NSLog(@"%@",response);
}else {
NSLog(@"No Buffer!");
}
mdata=nil;
}
break;
}
case kCFStreamEventNone:
break;
case kCFStreamEventOpenCompleted:
break;
}
}
static void WriteStreamClientCallBack( CFWriteStreamRef stream, CFStreamEventType type, void *clientCallBackInfo ) {
switch (type)
{
case kCFStreamEventEndEncountered:
{
NSLog(@"end o");
CFWriteStreamClose(stream);
break;
}
case kCFStreamEventErrorOccurred:
break;
case kCFStreamEventCanAcceptBytes:
{
NSString * reqStr = [NSString stringWithFormat: @"GET /filereflex/xp.html?user=demo&mac=%@&systemname=macintosh HTTP/1.0\r\n\r\nConnection: Close\r\nContent-Length: 100000000000\r\n\r\n",[GetPrimaryMacAddress getmacaddress]];
const UInt8 *rawstring = (const UInt8 *)[reqStr UTF8String];
CFWriteStreamWrite(stream, rawstring, strlen((char *)rawstring));
}
case kCFStreamEventNone:
break;
case kCFStreamEventOpenCompleted:
break;
}
}
- (void)connect{
NSString *iHostname = [[NSString alloc]initWithString:@"192.168.1.77"];
NSString *iPort = [[NSString alloc]initWithString:@"8080"];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
static const CFOptionFlags kReadNetworkEvents = kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred |
kCFStreamEventHasBytesAvailable |
kCFStreamEventOpenCompleted |
kCFStreamEventNone;
static const CFOptionFlags kWriteNetworkEvents = kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred |
kCFStreamEventCanAcceptBytes |
kCFStreamEventOpenCompleted |
kCFStreamEventNone;
CFStreamClientContext ctxt = {0,(void*)NULL,NULL,NULL,NULL};
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault,(CFStringRef)iHostname);
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, hostRef, [iPort intValue],
&readStream, &writeStream);
//CFSocketStreamPairSetSecurityProtocol(readStream, writeStream, kCFStreamSocketSecurityNone);
CFReadStreamSetClient(readStream, kReadNetworkEvents, ReadStreamClientCallBack, &ctxt);
CFWriteStreamSetClient(writeStream, kWriteNetworkEvents, WriteStreamClientCallBack, &ctxt);
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);
}
@end
Using the above code i can write and read the data only once. After that it automatically calls the kCFStreamEventEndEncountered event and closing the connection. But i need the connection should remains open always and need to send the data at particular intervals. How can i achieve this? Is there anything related to my server?
Thanks in advance.
Ramesh.P
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden