AsyncSocket只读取一次?

发布于 2024-11-08 03:04:33 字数 1805 浏览 1 评论 0原文


我制作了没有 CLRFdata 的简单客户端服务器,但我给出了带有长度消息自身的消息头。这是我的代码:

此方法发送数据

- (void)sendMessage:(NSString *)message {
unsigned char lendata = [message length];
 senddata = [NSMutableData dataWithBytes: &lendata length:sizeof(lendata)]  ;
[senddata appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[socket writeData:senddata withTimeout:-1 tag:0];  
}  

此方法从套接字读取数据长度= 1字节,直到标题上的代码很长。

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSString *message = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

if(bheader){
    cdata++;
    if(cdata==2){
        [data getBytes:&idata];
        bheader = NO;
        cdata=0;
        cmessage = [[NSString alloc] initWithFormat:@""];
    }

}
else{  
    if (message){            
        cmessage = [[NSString alloc]initWithFormat:@"%@%@",cmessage,message]  ;
    }
    else
        NSLog(@"Error converting received data into UTF-8 String");

    cdata++;
    if(cdata==idata) {
        msgComplete=YES;
    }

}

if (msgComplete) {
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:cmessage forKey:kNotificationMessage];
    [notificationCenter postNotificationName:kNotification object:self userInfo:userInfo];
    cmessage=@"";
    msgComplete=NO;
    bheader=YES;
    cdata=0;
    [cmessage release];    
}         
[sock readDataToLength:1 withTimeout:-1 tag:0];  
}  

这个方法

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
[sock readDataToLength:1 withTimeout:-1 tag:0];  
}  

我成功地从服务器发送数据和接收数据使用这个代码, 我尝试登录,服务器收到数据和通知,并且我使用此代码接收消息,但之后当我再次发送消息时,我无法从服务器接收消息,但在 TCPDUMP 上,我看到任何发送到服务器的消息,并且服务器用消息响应?

任何人都可以修复我的代码中的问题吗? 谢谢。

I make simple client server with no CLRFdata, but i give header of message with lenght message self. this is my code :

This method send data

- (void)sendMessage:(NSString *)message {
unsigned char lendata = [message length];
 senddata = [NSMutableData dataWithBytes: &lendata length:sizeof(lendata)]  ;
[senddata appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[socket writeData:senddata withTimeout:-1 tag:0];  
}  

This method read data from socket lenght = 1 byte until long of code on header.

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSString *message = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

if(bheader){
    cdata++;
    if(cdata==2){
        [data getBytes:&idata];
        bheader = NO;
        cdata=0;
        cmessage = [[NSString alloc] initWithFormat:@""];
    }

}
else{  
    if (message){            
        cmessage = [[NSString alloc]initWithFormat:@"%@%@",cmessage,message]  ;
    }
    else
        NSLog(@"Error converting received data into UTF-8 String");

    cdata++;
    if(cdata==idata) {
        msgComplete=YES;
    }

}

if (msgComplete) {
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:cmessage forKey:kNotificationMessage];
    [notificationCenter postNotificationName:kNotification object:self userInfo:userInfo];
    cmessage=@"";
    msgComplete=NO;
    bheader=YES;
    cdata=0;
    [cmessage release];    
}         
[sock readDataToLength:1 withTimeout:-1 tag:0];  
}  

and this method

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
[sock readDataToLength:1 withTimeout:-1 tag:0];  
}  

I'm succes send data and recv data from server use this code,
i try make login and server got data and notification, and i recv message with this code, but after that i cant recv message from server when i send message again, but on TCPDUMP i see any message sent to server and server response with a message ?

could any one fix my code where the problem ?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文