gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:50:43 25 4
gpt4 key购买 nike

我是套接字编程的新手,我是通过引用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com