我有一个使用套接字的应用程序
在我的应用程序中,我正在尝试建立套接字连接,连接套接字后我需要向服务器发送肥皂请求。我在将肥皂请求发送到服务器时遇到问题。我附上了代码。请帮助我
BOOL status;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
urlString = @"hoegamestg.hogaming.info";
// urlString = @"247liveagent.hointeractive.com";
if (![urlString isEqualToString:@""])
{
NSURL *website = [NSURL URLWithString:urlString];
if (!website)
{
NSLog(@"%@ is not a valid URL", urlString);
status = NO;
}
else
{
NSLog(@"URL IS VALID%@",website );
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef) [website host], 5654, &readStream, &writeStream);
// CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef) urlString, 5654, &readStream, &writeStream);
//USE TOLL-FREE BRIDGING FOR CONVERTING CORE-FOUNDATION STREAMS TO NSSTREAMS.
self.inputStream = (__bridge_transfer NSInputStream *)readStream;
self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;
//SET DELEGATES TO STREAMS.
[self.inputStream setDelegate:self];
[self.outputStream setDelegate:self];
//AVOID BLOCKING OPERATIONS BY SCHEDULING THEM ON TO RUN LOOPS.
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//FINALLY OPEN THE STREAMS.
[self.inputStream open];
[self.outputStream open];
}
}......
委托方法和soap请求
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
// Start Logging events.
// NSString *str = [NSString stringWithFormat:@"%d",eventCode];
// NSMutableString *subscribeTableString;
NSData *data;
NSString *soapMessage = [[NSString alloc]init];
switch (eventCode) {
case NSStreamEventNone:
NSLog(@"NSStreamEventNone");
break;
case NSStreamEventOpenCompleted:
NSLog(@"NSStreamEventOpenCompleted");
break;
case NSStreamEventHasBytesAvailable:
NSLog(@"NSStreamEventHasBytesAvailable");
[self readDataFromStream];
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"NSStreamEventHasSpaceAvailable");
soapMessage=[soapMessage stringByAppendingString:[NSString stringWithFormat:@"<subscribe channel=\"table-bc7ire5oi4uhetfd\" ><player id=\"%@\" />",socketValue ]];
// soapMessage=[soapMessage stringByAppendingString:[NSString stringWithFormat:@"<subscribe channel=\"table-bc7ire5oi4uhetfd\" ><player id= \"c2da1a80c52542dd\" />" ]];
soapMessage =[soapMessage stringByAppendingString:[NSString stringWithFormat:@"<sessionid id=\"%@\"",socketname]];
soapMessage =[soapMessage stringByAppendingString:[NSString stringWithFormat:@"></sessionid></subscribe>"]];
NSLog(@"THE STRING IS : %@", soapMessage);
data = [[NSData alloc] initWithData:[soapMessage dataUsingEncoding:NSASCIIStringEncoding]];
[self.outputStream write:[data bytes] maxLength:[data length]];
NSLog(@"THE STRING IS : %@", self.outputStream);
[self setMWriteData:[NSData dataWithBytes:(__bridge const void*)soapMessage length:
[soapMessage length]]];
[self writeDataToStream];
// <subscribe channel="table-l8i2hq4jo2hjj9ca"><player id="b82fe3c52020494b" /><sessionid id="246421321cc873d080b550bcc555de0e9d9d29d8cba6f243ec56d38c5785"></sessionid></subscribe>
break;
case NSStreamEventErrorOccurred:
NSLog(@"NSStreamEventErrorOccurred");
NSLog(@"THE ERROR IS : %@", [aStream streamError]);
break;
case NSStreamEventEndEncountered:
break;
default:
break;
}
}