Asyncsocket iPhone read data
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] withTime开发者_开发百科out:-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?
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.
I am not clear on what could go wrong because the code snippet is not enough. But that is how I would go on:
- Check that when you connect to the host that it does not raise an error
- In you fireClicked method make sure that socket is properly initiated
- Maybe override didConnectToHost method and add a NSLog statement to see if you connected
- Use the given socket "sock" inside didReadData and didWriteDataWithTag instead of the variable "socket"
- Tell us what logging statements you get
- Tell us how you created the socket and how you connected to the host.
Hope that helps - Andy
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.
精彩评论