使用 AsyncSocket 时客户端应用程序未按预期工作

发布于 2024-08-11 21:03:45 字数 2787 浏览 7 评论 0原文

我在一个简单的客户端-服务器应用程序中使用 AsyncSocket 类。作为第一步,我希望 - 一旦客户端和服务器之间建立连接,客户端就会向服务器发送一条欢迎消息 - “已连接到 xyz 服务器”,并且服务器将其显示在文本视图中。

//The code in ClientController class is:

 -(void)awakeFromNib{
NSError *error = nil;
  if (![connectSocket connectToHost:@"192.168.0.32" onPort:25242 error:&error]) {
   NSLog(@"Error starting client: %@", error);
   return;
  }

  NSLog(@"xyz chat client started on port %hu",[connectSocket localPort]);
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
               [sock writeData:[@"connected to xyz server" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:30.0 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
               // some relevant code goes here
}
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
               NSLog(@"within didWriteDataWithTag:"); // getting this message, means it should have written something to remote socket but
  // delegate- onSocket:didReadData:withTag: at server side is not getting invoked
}

// The code in ServerController class is:

 - (IBAction)startStop:(id)sender{
 NSLog(@"startStopAction");
 if(!isRunning)
 {
  NSError *error = nil;
  if(![listenSocket acceptOnPort:INPUT_PORT error:&error])
  {
   NSLog(@"Error starting server: %@", error);
   return;
  }

  NSLog(@"Echo server started on port %hu",[listenSocket localPort]);
  isRunning = YES;

  [sender setTitle:@"Stop"];
 }
 else
 {
  // Stop accepting connections
  [listenSocket disconnect];

  // Stop any client connections
  int i;
  for(i = 0; i < [connectedSockets count]; i++)
  {
   // Call disconnect on the socket,
   // which will invoke the onSocketDidDisconnect: method,
   // which will remove the socket from the list.
   [[connectedSockets objectAtIndex:i] disconnect];
  }

  NSLog(@"Stopped Echo server");
  isRunning = false;

  [sender setTitle:@"Start"];
 }
}
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
 [connectedSockets addObject:newSocket];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
 NSLog(@"Accepted client %@:%hu", host, port);  // it is getting displayed
 [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
        NSString *msgReceived = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
 NSLog(@"msgReceived in didReadData- %@",msgReceived); // it is not getting displayed
        [outputView insertText:msgReceived];
        [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}

谁能建议我哪里可能错了? 提前致谢...... Miraaj

I am using AsyncSocket class in a simple client-server application. As a first step I want that - as soon as connection is established between client and server, client transmit a welcome message - "connected to xyz server" to server and server displays it in textview.

//The code in ClientController class is:

 -(void)awakeFromNib{
NSError *error = nil;
  if (![connectSocket connectToHost:@"192.168.0.32" onPort:25242 error:&error]) {
   NSLog(@"Error starting client: %@", error);
   return;
  }

  NSLog(@"xyz chat client started on port %hu",[connectSocket localPort]);
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
               [sock writeData:[@"connected to xyz server" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:30.0 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
               // some relevant code goes here
}
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
               NSLog(@"within didWriteDataWithTag:"); // getting this message, means it should have written something to remote socket but
  // delegate- onSocket:didReadData:withTag: at server side is not getting invoked
}

// The code in ServerController class is:

 - (IBAction)startStop:(id)sender{
 NSLog(@"startStopAction");
 if(!isRunning)
 {
  NSError *error = nil;
  if(![listenSocket acceptOnPort:INPUT_PORT error:&error])
  {
   NSLog(@"Error starting server: %@", error);
   return;
  }

  NSLog(@"Echo server started on port %hu",[listenSocket localPort]);
  isRunning = YES;

  [sender setTitle:@"Stop"];
 }
 else
 {
  // Stop accepting connections
  [listenSocket disconnect];

  // Stop any client connections
  int i;
  for(i = 0; i < [connectedSockets count]; i++)
  {
   // Call disconnect on the socket,
   // which will invoke the onSocketDidDisconnect: method,
   // which will remove the socket from the list.
   [[connectedSockets objectAtIndex:i] disconnect];
  }

  NSLog(@"Stopped Echo server");
  isRunning = false;

  [sender setTitle:@"Start"];
 }
}
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
 [connectedSockets addObject:newSocket];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
 NSLog(@"Accepted client %@:%hu", host, port);  // it is getting displayed
 [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
        NSString *msgReceived = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
 NSLog(@"msgReceived in didReadData- %@",msgReceived); // it is not getting displayed
        [outputView insertText:msgReceived];
        [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}

Can anyone suggest me where I may be wrong??
Thanks in advance...... Miraaj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-08-18 21:03:45

问题解决:刚刚使用:[sock readDataWithTimeout:READ_TIMEOUT tag:0];代替: [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];

Problem solved: just used: [sock readDataWithTimeout:READ_TIMEOUT tag:0]; in place of: [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文