我正在使用 CocoaAsyncSockets 库来在我的应用程序中创建一个 tcp 套接字连接。目前,我已通过在 appDelegate.m 文件中的 didFinishLaunchingWithOptions 方法中打开套接字连接来成功创建连接。我的代码如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
socket = [[AsyncSocket alloc] initWithDelegate:self];
[self connect];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[tekMatrixViewController alloc] initWithNibName:@"tekMatrixViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
这是我的连接方法:
- (void)connect
{
[socket connectToHost:@"9.5.3.6" onPort:11005 error:nil];
}
现在,我一直在苦苦挣扎的是如何在我的视图中使用读/写方法,而无需重新建立连接。
- 我在应用程序启动时建立了一个 tcp 套接字连接
- 我在 didFinishLaunchWithOptions 方法中打开套接字,以便我可以创建连接并在我的应用程序运行的整个过程中保持连接
- 我不知道如何从我的视图中读取/写入服务器
- 我对 socket/iOS 开发很陌生,所以我希望有最简单的解决方案
我会很感激我能得到的任何帮助。我对 iOS 还是很陌生,并且还在学习语法,所以任何人都可以提供的细节越多,我就会越好。
非常感谢你的帮忙!