• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to maintain the socket connection in open status and send datas repeatedly
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to maintain the socket connection in open status and send datas repeatedly


  • Subject: Re: How to maintain the socket connection in open status and send datas repeatedly
  • From: Jeremy Wyld <email@hidden>
  • Date: Tue, 25 Aug 2009 07:55:36 -0700

First off, your HTTP request has a "Connection: Close" which is telling the server to close it.

jeremy

On Aug 25, 2009, at 3:04 AM, email@hidden wrote:

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:
@apple.com


This email sent to email@hidden

_______________________________________________ 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
  • Follow-Ups:
    • Re: How to maintain the socket connection in open status and send datas repeatedly
      • From: Ramesh P <email@hidden>
References: 
 >How to maintain the socket connection in open status and send datas repeatedly (From: email@hidden)

  • Prev by Date: CFHTTPStream returns long error code (EAI_NONAME?)
  • Next by Date: Re: How to maintain the socket connection in open status and send datas repeatedly
  • Previous by thread: How to maintain the socket connection in open status and send datas repeatedly
  • Next by thread: Re: How to maintain the socket connection in open status and send datas repeatedly
  • Index(es):
    • Date
    • Thread