iphone - 使用套接字接收损坏/不完整的消息

标签 iphone ios sockets

我是套接字编程的新手,我是通过引用 http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server 来完成的,我正在使用 php 服务器,我面临的问题是,我可以使用套接字发送/接收消息,但是我收到的消息被破坏了,就像我收到的消息应该是“你好 abcd ”但它给了我“”然后过了一段时间“ llo ”然后过了一段时间“ abcd ”。
我正在使用以下代码接收消息:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {


    switch (streamEvent) {

        case NSStreamEventNone:
        NSLog(@"Stream event none");
            break;

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;
        case NSStreamEventHasBytesAvailable:


        if (theStream == inputStream) {

                int len=0;
                uint8_t *buffer = (uint8_t *)calloc(1, (16*1024));

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];


                   NSLog(@"byte available %d",len);

                    if (len > 0) {
                        NSMutableData* data=[[NSMutableData alloc] initWithLength:0];
                        //
                        [data appendBytes:(const void *)buffer length:len];


//                        NSString *s = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
//                                        NSLog(@"rs %@",s);

                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];


                        if (nil != output) {
                            NSLog(@"server said: %@", output);
                            [self messageReceived:output];

                        }
                    }
                }

            }



            break;

        case NSStreamEventHasSpaceAvailable:
            NSLog(@"event space available");

            break;

        case NSStreamEventErrorOccurred:

            NSLog(@"Can not connect to the host!");
            break;

        case NSStreamEventEndEncountered:
            NSLog(@"end");
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [theStream release];
            theStream = nil;

            break;


        default:
            NSLog(@"Unknown event");
    }

}

最佳答案

我面临同样的问题。我所做的不是在委托(delegate)方法中附加字符串,而是将字符串传递给另一个方法,检查输入流中的字节并将字符串附加到那里。这是我的应用程序的片段。希望能帮助到你。

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {

    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        break;

    case NSStreamEventHasBytesAvailable:
        if (theStream == inputStream) {
            uint8_t buffer[1024];
            NSInteger len = [inputStream read:buffer maxLength:sizeof(buffer)];
            NSString *output;
            if (len > 0) {
                output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                [self messageReceived:output];
            }
        }

        break;

    case NSStreamEventErrorOccurred:
    {
        NSLog(@"Can not connect to the host!");
        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

        UIAlertView *errorOccured = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Some error occured." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [errorOccured show];
    }
        break;

    case NSStreamEventEndEncountered:
        NSLog(@"Stream end occured");
        break;

    default:
        NSLog(@"Unknown event");
  }

}
- (void) messageReceived:(NSString *)message {
    if (inputStream.hasBytesAvailable) {
      [streamResponseStr appendString:message];
    }else{
      [streamResponseStr appendString:message];
      [self parseServerResponse:streamResponseStr];

      streamResponseStr = nil;
      streamResponseStr = [[NSMutableString alloc] init];
    }
}

关于iphone - 使用套接字接收损坏/不完整的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16141035/

相关文章:

ios - 在应用程序上覆盖文本以显示调试信息

iphone - Facebook Graph API 无法点赞

ios - 使用类中的项目填充 UIPickerView

php - 只有变量应该通过引用传递 - socket_select()

c++ - 用于获取 URL 之类的简单 C++(或 c)库

ios - 是否可以使flexbox在iphone 4/iOS4上运行或提供后备功能?

iphone - Cocos2d 中链接类方法

ios - 如何将 "$123.4"之类的字符串更改为 floatValue

调用 accept() 给出 errno 14 错误地址

java - 使用单个 TCP/IP 套接字从多个并发线程发送数据的问题