【问题标题】:How to create TCP IP Server using Objective-C on iOS如何在 iOS 上使用 Objective-C 创建 TCP IP 服务器
【发布时间】:2020-01-26 10:19:36
【问题描述】:

我已经设法创建了一个带有objective-c的客户端,它可以连接到任何服务器,我的应用程序在iPad上运行..到目前为止,我已经在不同的服务器telnet、nodejs甚至基于web的php上测试了客户端服务器,它工作正常。 但是我的范围发生了变化,因为我需要我的应用程序充当服务器,因此它应该首先打开一个套接字并等待客户端连接并在两者之间建立通信,而不是连接到给定的套接字。

这是我在 ipad 上的客户端如何连接到服务器的 sn-p:

NSLog(@"Setting up connection to %@ : %i", _ipAddressText.text, [_portText.text intValue]);
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) _ipAddressText.text, [_portText.text intValue], &readStream, &writeStream);

messages = [[NSMutableArray alloc] init];

[self open];

我已经尝试过 cocoaAsyncSocket,但是在解决它时遇到了麻烦: https://github.com/robbiehanson/CocoaAsyncSocket

【问题讨论】:

    标签: objective-c xcode tcp server ios12


    【解决方案1】:

    现在有比 CocoaAsyncSocket 更好的原生替代品,即 Apple 的网络框架。

    下面是一个使用 nw 函数的简单 TCP 客户端和服务器的示例代码: https://developer.apple.com/documentation/network/implementing_netcat_with_network_framework

    【讨论】:

      猜你喜欢
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2011-01-18
      相关资源
      最近更新 更多