I have an iPhone app that I run on the simulator. XCode ver 3.2.6/4.3. I am trying to communicate with a radio on a serial port of a PC over wifi, both on the same server… I’ve tried NSStream and GCDAsyncSocket (just to make sure). The radio has its own IP address and port number. It’s actually a TCP/IP wifi module. After changing the remote access on the PC to accept my IP address, I am finally able to connect but I get kicked off immediately, I’m assuming it’s when I try to read or write. Same happens when using Telnet, connects then disconnects. The radio issues HELLO when someone connects, so Telnet must try to read since data is sent. I’m guessing. I thought since I am able to connect, I should be able to read/write. (Yes, newbie here)
I would appreciate any thoughts or direction. I’ve been researching for over a week now and going bonkers.
Thanks. I added the code below as well as the error message.
This is the error message:
socketDidDisconnect:withError: “Error Domain=NSOSStatusErrorDomain
Code=-9844 “The operation couldn’t be completed. (OSStatus error
-9844.)” UserInfo=0x4c38a60 {}”
- (IBAction)performConnection:(id)sender
{
asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
uint16_t port = [[[self serverPort] text] intValue];
if (![asyncSocket connectToHost:[serverAddr text] onPort:port error:&error])
{
DDLogError(@"Unable to connect due to invalid configuration: %@", error);
[self debugPrint:[NSString stringWithFormat:@"Unable to connect due to invalid configuration: %@", error]];
}
else
{
DDLogVerbose(@"Connecting...IP:%@, port:%i", [serverAddr text], port);
}
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
DDLogInfo(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:3];
[settings setObject:@"XXX.XXX.X.XXX"
forKey:(NSString *)kCFStreamSSLPeerName];
// In fact, don't even validate the certificate chain
[settings setObject:[NSNumber numberWithBool:NO]
forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];
[settings setObject:(NSString*)kCFStreamPropertySocketSecurityLevel
forKey:(NSString*)kCFStreamSocketSecurityLevelNegotiatedSSL];
DDLogVerbose(@"Starting TLS with settings:\n%@", settings);
[sock startTLS:settings];
[self debugPrint:[NSString stringWithFormat:@"socket:didConnectToHost:%@ port:%hu", host, port]];
//[sock readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0];
[sock readDataWithTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
DDLogVerbose(@"socket:didWriteDataWithTag:");
[sock readDataWithTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
DDLogVerbose(@"socket:didReadData:withTag:");
NSString *response = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"read response:%@", response);
[self debugPrint:[NSString stringWithFormat:@"Read: \n%@",response]];
[response release];
//NSData *newline = [@"\n" dataUsingEncoding:NSASCIIStringEncoding];
//[sock readDataToData:newline withTimeout:-1 tag: 0];
[sock readDataWithTimeout:-1 tag:0];
}
- (IBAction)sendBuf:(id)sender
{
if ([[bufOut text] length] > 0)
{
NSString *requestStr = [NSString stringWithFormat:@"%@\r\n", [bufOut text]];
NSLog(@"Sending:%@",requestStr);
NSData *requestData = [requestStr dataUsingEncoding:NSASCIIStringEncoding];
[asyncSocket writeData:requestData withTimeout:-1.0 tag:0];
[self debugPrint:[NSString stringWithFormat:@"Sent: \n%@",requestStr]];
}
}
I found a partial solution (by sheer accident). It seems the radio or how the radio is setup only allows me to connect/read/write if my app or iMac initiates the connection. It’s fine with another PC. Both can initiate a connection and everything works. Still need to resolve this but at least I know my code is working.
Thanks to anyone who gave my problem some thought.