I create a socket client using asyncsocket and i can get the connection and the data in the server, but i cannot get the data on the client end, which means the onSocket: didReadData method have not been called.
-(void) connectToServer
{
NSError *err;
[self.sock connectToHost:[[NSUserDefaults standardUserDefaults] objectForKey:@"ip"] onPort:[[[NSUserDefaults standardUserDefaults] valueForKey:@"port"] intValue] error:&err];
}
-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"connect to host");
NSData *sockdata = [@"test data\r\n" dataUsingEncoding: NSUTF8StringEncoding];
[self.sock writeData:sockdata withTimeout:-1 tag:1];
}
-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
NSLog(@"write data finish");//this works good
[sock readDataToData:nil withTimeout:100 tag:1];
}
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag//this cannot get called
{
NSLog(@"read data !");
}
Problem found. I replace:
[sock readDataToData:nil withTimeout:100 tag:1];
with
[sock readDataToData:[AsyncSocket CRLFData] withTimeout:100 tag:1];
and now I can read the correct data.