Thanks Jeremy and Jens for your quick responses.
I have modified my code to NSStream and removed the connection:close from my http url.
Here is my code:
-(void)startSocket{
NSHost *host = [NSHost hostWithName:@"192.168.1.77"];
if(host) {
[NSStream getStreamsToHost:host port:8080 inputStream:&iStream outputStream:&oStream] ;
[self openStream];
NSLog(@"input stream %@", nil==iStream?@"was not created":@"was created");
NSLog(@"output stream %@", nil==oStream?@"was not created":@"was created");
}
}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch (eventCode) {
case NSStreamEventHasBytesAvailable:
{
NSLog(@"Bytes Available");
if ( !mdata ) {
mdata = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
while ([iStream hasBytesAvailable]){
len = [(NSInputStream *)stream read:buf maxLength:1024];
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 NSStreamEventErrorOccurred:
{
NSError *theError = [stream streamError];
NSAlert *theAlert = [[NSAlert alloc] init];
[theAlert setMessageText:@"Error reading stream!"];
[theAlert setInformativeText:[NSString stringWithFormat:@"Error %i: %@",[theError code], [theError localizedDescription]]];
[theAlert addButtonWithTitle:@"OK"];
[theAlert runModal];
[stream close];
[stream release];
break;
}
case NSStreamEventEndEncountered:
{
[self closeStream];
break;
}
case NSStreamEventNone:
{
break;
}
case NSStreamEventOpenCompleted:{
break;
}
case NSStreamEventHasSpaceAvailable:
{
NSLog(@"Space Available");
NSString * str = [NSString stringWithFormat: @"GET /filereflex/xp.html?user=demo&mac=%@&systemname=macintosh HTTP/1.0\r\n\r\nContent-Length: 100000000000\r\n\r\n",[GetPrimaryMacAddress getmacaddress]];
const uint8_t * rawstring =(const uint8_t *)[str UTF8String];
NSInteger bytesWritten=[oStream write:rawstring maxLength:strlen(rawstring)];
if(bytesWritten==-1)
NSLog(@"written null");
else
NSLog(@"written successfully");
[stream close];
break;
}
default:
break;
}
}
-(void)openStream{
[iStream retain];
[oStream retain];
[iStream setDelegate:self];
[oStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
}
-(void)closeStream{
[iStream close];
[oStream close];
[iStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream setDelegate:nil];
[oStream setDelegate:nil];
[iStream release];
[oStream release];
iStream=nil;
oStream=nil;
}
Again I am facing the same problem. Writing and reading the data successfully at first time and entering to NSStreamEventEndEncountered event. I am not able to maintain the connection in opened status.
Here is my output:2009-08-26 10:47:46.106 FileReflexMacClient[508:10b] input stream was created
2009-08-26 10:47:46.108 FileReflexMacClient[508:10b] output stream was created
2009-08-26 10:47:46.118 FileReflexMacClient[508:10b] Space Available
2009-08-26 10:47:46.122 FileReflexMacClient[508:10b] written successfully
2009-08-26 10:47:46.163 FileReflexMacClient[508:10b] Bytes Available
2009-08-26 10:47:46.163 FileReflexMacClient[508:10b] HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=E757EA68C6CAD6BA0FED9C70E8ACD05C; Path=/filereflex
Date: Wed, 26 Aug 2009 05:10:35 GMT
Connection: close
BEGIN
2009-08-26 10:47:46.330 FileReflexMacClient[508:10b] Bytes Available
2009-08-26 10:47:46.331 FileReflexMacClient[508:10b] No Buffer!
2009-08-26 10:47:46.332 FileReflexMacClient[508:10b] end
Help me in this.
Thanks,
Ramesh.P
>>On Tue, Aug 25, 2009 at 10:31 PM, Jens Alfke
<email@hidden> wrote:
>>This
isn't directly related to the bug in your code (which Jeremy's already
pointed out), but you >>could use the NSStream class instead of the
CFStream API and make your code a lot simpler >>& cleaner.
—Jens
On Tue, Aug 25, 2009 at 8:25 PM, Jeremy Wyld
<email@hidden> wrote:
First off, your HTTP request has a "Connection: Close" which is telling the server to close it.
jeremy
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