Asyncsocket iPhone读取数据

发布于 2024-10-23 20:11:31 字数 1424 浏览 2 评论 0原文

我正在尝试使用以下代码捕获来自 .NET 服务器的响应。

进行 telnet 测试有效(我得到响应);但是,使用此代码,我没有得到响应。

-(IBAction)connectClicked:(id)sender {
    if (![socket connectToHost:@"192.168.100.192" onPort:1337 error:nil]) {
        NSLog(@"connection failed");
    }    
}

-(IBAction)fireClicked:(id)sender {
    NSString *welcomeMsg = @"GetId";
    NSData *welcomeData = [welcomeMsg dataUsingEncoding:NSUTF8StringEncoding];
    [socket writeData:welcomeData withTimeout:-1 tag:0];
    NSLog(@"message sent!");
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString*)host port:(UInt16)port {
    NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}

-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag {
    NSLog(@"onSocket:%p didReadData:%i", tag);    
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"onSocket:didWriteDataWithTag:%i", tag);    
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}

我确实与服务器建立了连接,并且能够向服务器发送信息,但没有得到回复。

这是我在日志记录中得到的;

2011-03-21 10:00:32.424 CtC[33521:207] onSocket:0x4c3ebc0 didConnectToHost:192.168.100.192 port:1337
2011-03-21 10:00:35.846 CtC[33521:207] message sent!

为什么我没有收到回复?

I'm trying to catch a response from a .NET server using the following code.

Doing a telnet test works (I get the response); but, using this code, I don't get a response.

-(IBAction)connectClicked:(id)sender {
    if (![socket connectToHost:@"192.168.100.192" onPort:1337 error:nil]) {
        NSLog(@"connection failed");
    }    
}

-(IBAction)fireClicked:(id)sender {
    NSString *welcomeMsg = @"GetId";
    NSData *welcomeData = [welcomeMsg dataUsingEncoding:NSUTF8StringEncoding];
    [socket writeData:welcomeData withTimeout:-1 tag:0];
    NSLog(@"message sent!");
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString*)host port:(UInt16)port {
    NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}

-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag {
    NSLog(@"onSocket:%p didReadData:%i", tag);    
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"onSocket:didWriteDataWithTag:%i", tag);    
    [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}

I do get the connection with the server and I'm able to send information to the server, but I do not get a reply.

This is what I get in the logging;

2011-03-21 10:00:32.424 CtC[33521:207] onSocket:0x4c3ebc0 didConnectToHost:192.168.100.192 port:1337
2011-03-21 10:00:35.846 CtC[33521:207] message sent!

Why am I not receiving a reply?

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

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

发布评论

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

评论(3

沉鱼一梦 2024-10-30 20:11:31

我也有同样的问题。我在写入数据时模拟了“输入”按钮,因为服务器不知道命令何时结束。

所以这条线

NSString *welcomeMsg = @"GetId"; 

应该变成

NSString *welcomeMsg = @"GetId\r\n";

现在可以工作了。

i also have the same problem. i simulated the 'enter' button when write data since the server don't know when the command ended.

So this line

NSString *welcomeMsg = @"GetId"; 

should become

NSString *welcomeMsg = @"GetId\r\n";

It worked now.

北座城市 2024-10-30 20:11:31

我不清楚可能会出现什么问题,因为代码片段还不够。但这就是我要继续的方式:

  1. 检查当您连接到主机时它不会引发错误
  2. 在您的 fireClicked 方法中确保套接字已正确启动
  3. 也许重写 didConnectToHost 方法并添加 NSLog 语句以查看您是否连接
  4. 使用didReadData 和 didWriteDataWithTag 中给定的套接字“sock”而不是变量“socket”
  5. 告诉我们您收到的日志记录语句
  6. 告诉我们您如何创建套接字以及如何连接到主机。

希望有帮助 - 安迪

I am not clear on what could go wrong because the code snippet is not enough. But that is how I would go on:

  1. Check that when you connect to the host that it does not raise an error
  2. In you fireClicked method make sure that socket is properly initiated
  3. Maybe override didConnectToHost method and add a NSLog statement to see if you connected
  4. Use the given socket "sock" inside didReadData and didWriteDataWithTag instead of the variable "socket"
  5. Tell us what logging statements you get
  6. Tell us how you created the socket and how you connected to the host.

Hope that helps - Andy

淡紫姑娘! 2024-10-30 20:11:31

WireShark 在这些情况下总是有用的。快速比较 telnet 和您的应用程序可能会发现差异。

尝试删除 didConnectToHost 中的“readDataWithTimeout”,因为无论如何您都会在写入后调用它。
我想知道您是否断开连接,从而导致读取超时,即您的有线协议错误。 WireShark 应该能帮上忙。

WireShark is always useful in these situations. A quick comparison between telnet and your app might indicate a difference.

Try removing the 'readDataWithTimeout' that you have in the didConnectToHost, since you call this after a write anyway.
I'm wondering if you are getting a disconnect which is causing the timeout on the read, ie your wire protocol is wrong. WireShark should help there.

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