0

我有一个使用套接字的应用程序

在我的应用程序中,我正在尝试建立套接字连接,连接套接字后我需要向服务器发送肥皂请求。我在将肥皂请求发送到服务器时遇到问题。我附上了代码。请帮助我

 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;

    }

}
4

1 回答 1

1

如果我没记错的话,SOAP 是基于 HTTP 协议的,所以你不能为此使用原始套接字。如果您确实需要 SOAP,可以参考这个问题:how to send/receive soap object with objective c for ipad

但是,如果您只是想通过原始套接字发送 XML,那么您需要告诉接收者何时停止读取。

HTTP 结束流的方式是添加\r\n\r\n到字符串的末尾,因此您可以使用它。

另一种方法是发送一个 4 字节的标头,其中包含要发送的字节数,所以添加这个(未经测试):

int s = [data length];
NSData *size = [NSData dataWithBytes:&s length:4];
[self.outputStream write:[size bytes] maxLength:[size length]];

在这之前

[self.outputStream write:[data bytes] maxLength:[data length]];

服务器需要意识到这一点并将每前 4 个字节视为标头,然后读取标头中提到的字节数。

我认为使用HTTP的方法会更容易,服务器在遇到时停止读取\r\n\r\n

于 2013-09-20T07:20:03.073 回答