0

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 !");
}
FisherMartyn
  • 826
  • 1
  • 8
  • 17
  • Did server send any data to your client ? Do you implement the method in its protocol `- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err;` and get any error ? – KudoCC Mar 16 '15 at 02:40
  • no,I get no error, and problem found. I replace [sock readDataToData:nil withTimeout:100 tag:1] with [sock readDataToData:[AsyncSocket CRLFData] withTimeout:100 tag:1] and i can readData now. – FisherMartyn Mar 16 '15 at 02:43

1 Answers1

0

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.

FisherMartyn
  • 826
  • 1
  • 8
  • 17