Async Socket没有通过wifi连接到TCP串口

时间:2011-10-18 15:34:17

标签: iphone ssl tcp asyncsocket

我有一个我在模拟器上运行的iPhone应用程序。 XCode版本3.2.6 / 4.3。我试图通过wifi在一台PC的串口上收音机进行通信,两者都在同一台服务器上...我已经尝试过NSStream和GCDAsyncSocket(只是为了确保)。收音机有自己的IP地址和端口号。它实际上是一个TCP / IP wifi模块。更改PC上的远程访问以接受我的IP地址后,我终于可以连接但是我立即开始,我在尝试读取或写入时假设它。使用Telnet时会发生相同情况,然后断开连接。当有人连接时,无线电发出 HELLO ,因此Telnet必须尝试读取数据,因为数据已发送。我在猜测。我想,因为我能够连接,我应该能够读/写。 (是的,新手在这里)

我很感激任何想法或方向。我现在已经研究了一个多星期,并且正在疯狂地进行研究。

感谢。我添加了以下代码以及错误消息。

这是错误消息:     socketDidDisconnect:withError:" Error Domain = NSOSStatusErrorDomain     代码= -9844"操作无法完成。 (OSStatus错误     -9844)&#34。 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]];
    }

}

1 个答案:

答案 0 :(得分:0)

我找到了部分解决方案(仅仅是因为意外事故)。看起来无线电或无线电的设置方式只允许我连接/读/写,如果我的应用程序或iMac启动连接。对另一台PC来说很好。两者都可以启动连接,一切正常。仍然需要解决这个问题,但至少我知道我的代码正在运行。 感谢任何提出我问题的人。