Asyncsocket iPhone读取数据
我正在尝试使用以下代码捕获来自 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也有同样的问题。我在写入数据时模拟了“输入”按钮,因为服务器不知道命令何时结束。
所以这条线
应该变成
现在可以工作了。
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
should become
It worked now.
我不清楚可能会出现什么问题,因为代码片段还不够。但这就是我要继续的方式:
希望有帮助 - 安迪
I am not clear on what could go wrong because the code snippet is not enough. But that is how I would go on:
Hope that helps - Andy
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.