iOS cocoaAsyncSocket get data parse String返回null

时间:2015-01-08 10:29:47

标签: ios xcode sockets parsing cocoaasyncsocket

我尝试将telnet连接到iOS中的telnet服务器端口23。

我使用" cocoaAsyncSocket"图书馆。

我可以从telnet收到数据。

但是我尝试解码到字符串,数据不是我期望的。

会显示

 (null)

如果我使用NSASCIIStringEncodeing进行解码。

会显示

 ÿýÿý ÿý#ÿý'

我使用terminal命令telnet myIP 23。

我可以在终端下面。

  james$ telnet 192.168.2.94 23
 Trying 192.168.2.94...
 Conn ected to txxxxx.xxxxx.com.
 Escape character is '^]'.
 Password: 

我如何解析"密码:" iOS中的字符串?

谢谢。

我的连接代码如下:

 - (void)viewDidLoad {
     [super viewDidLoad];
     asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self    delegateQueue:dispatch_get_main_queue()];

     buffer = [[NSMutableData alloc] init];
      [asyncSocket connectToHost:@"192.168.2.94" onPort:23 error:nil];
 }

 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
     NSLog(@"did connect to host");
      [asyncSocket readDataWithTimeout:-1 tag:0];
 }

 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
          NSLog(@"didReadData(%lu): %@", tag, @([data length]));
          [buffer setLength:0];

      NSString* message = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
 //            NSString* message = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
     NSLog(@"message is: \n%@",message);



     [asyncSocket readDataWithTimeout:-1 buffer:buffer bufferOffset:[buffer length] tag:tag];
 }

 - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
 {
   NSLog(@"socket disconnect to host");

 }

0 个答案:

没有答案