0

I wanna using asyncsocket to communicate between ios app with a windows server(coded by c++). ios send a packet to server is ok, but ios cann't read the packet from server. I have test the server code by client by c++, it works ok. Here is my part of ios code.

- (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);
}

I make breakpoint in each of them, just only didConnectToHost is called, didReadParticalDataOfLength and didReadData are not called.

I have test lots of times, but ios can't read anything. Any ideas what I did wrong?

p.s. i use asyncsocket not GCDasyncsocket. thanks for your time.

Nilesh
  • 701
  • 5
  • 14
Jun
  • 15
  • 4

1 Answers1

0

Missing [readMsg];

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