不调用didreaddata方法。使用asyncsocket

时间:2015-11-02 14:17:33

标签: ios asyncsocket

我想使用asyncsocket在ios app与windows服务器之间进行通信(用c ++编码)。 ios向服务器发送数据包是可以的,但是ios无法从服务器读取数据包。 我用c ++通过客户端测试了服务器代码,它运行正常。 这是我的ios代码的一部分。

- (int) connectServer: (NSString *) hostIP port:(int) hostPort{

_isBusy = YES;
client = nil;
client = [[AsyncSocket alloc] initWithDelegate:self];
self.HOST_IP = hostIP;
self.HOST_PORT = hostPort;
NSError *err = nil;
if([client connectToHost:hostIP onPort:hostPort error:&err])
    return SRV_CONNECT_SUC;
else{
    return SRV_CONNECT_FAIL;
}
}

- (void)sendMsg:(NSString *)msg
{
if ([client isConnected]){
    NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
    [client writeData:data withTimeout:-1 tag:0];
}
}

-(void*) readMsg
{
[client readDataWithTimeout:-1 tag:0];
}

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Hava received datas is :%@",aStr);
[aStr release];
[client readDataWithTimeout:-1 tag:0];
}

-(void)onSocket:(AsyncSocket*)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"Socket is connected!");
[client readDataWithTimeout:-1 tag:0];
}

-(void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
NSLog(@"Received bytes: %d",partialLength);
}

我在每个中创建断点,只调用 didConnectToHost didReadParticalDataOfLength didReadData不被调用。

我有很多次测试,但ios无法读取任何内容。 我做错了什么想法?

P.S。我使用asyncsocket而不是GCDasyncsocket。 谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

缺少[readMsg];

 - (void)sendMsg:(NSString *)msg {
        if ([client isConnected]){
            NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
            [client writeData:data withTimeout:-1 tag:0];
            [readMsg];
        }
    }