AsycnSocket无法通过wifi连接到TCP串口

发布于 2024-12-10 08:08:48 字数 3401 浏览 0 评论 0原文

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

我将不胜感激任何想法或方向。我已经研究了一个多星期了,而且快要疯了。

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

这是错误消息: socketDidDisconnect:withError:“错误域= NSOSStatusErrorDomain 代码=-9844“操作无法完成。(OSStatus错误 -9844。)“用户信息=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 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]];
    }

}

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

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

发布评论

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

评论(1

说好的呢 2024-12-17 08:08:48

我找到了部分解决方案(纯属偶然)。看来收音机或收音机的设置方式仅允许我在我的应用程序或 iMac 启动连接时进行连接/读取/写入。用另一台电脑就没问题。两者都可以发起连接并且一切正常。仍然需要解决这个问题,但至少我知道我的代码正在运行。
感谢任何对我的问题进行思考的人。

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.

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