OGeek|极客世界-中国程序员成长平台

标题: iphone - 创建 TCP 套接字连接并在多个 View 中保持连接打开 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:13
标题: iphone - 创建 TCP 套接字连接并在多个 View 中保持连接打开

我已经使用 TCP 套接字成功地从 iPhone 连接到服务器(这是一台 Windows 机器)。目前,我正在使用一个按钮来执行以下代码:

while(1)
{
    Socket *socket;
    int port = 11005;
    NSString *host = @"9.5.3.63";

    socket = [Socket socket];

    @try
    {
        NSMutableData *data;
        [socket connectToHostName:host port:port];
        [socket readData:data];
        //  [socket writeString"Hello World!"];

        //** Connection was successful **//
        [socket retain]; // Must retain if want to use out of this action block.
    }
    @catch (NSException* exception) 
    {
        NSString *errMsg = [NSString stringWithFormat"%@",[exception reason]];
        NSLog(errMsg);
        socket = nil;
    }
}

这是最简单的部分...我正在尝试在应用程序加载后立即建立套接字连接。我尝试将此代码放在我的 viewDidLoad 中,但循环是无限的,并且 View 永远不会加载。我的项目中有几个 View ,我想打开连接,在所有 View 中始终保持连接打开。

目标:

我对 iOS 开发还是比较陌生,所以我希望尽可能清晰。应该注意的是,我正在使用 SmallSockets 库来打开我的 Sockets 连接。感谢您的帮助!

* 编辑 *

根据下面的答案,这就是我到目前为止所做的:

SocketConnection.h

#import <Foundation/Foundation.h>

@interface SocketConnection : NSObject
{

}

+ (SocketConnection *)getInstance;

@end

SocketConnection.m

静态 SocketConnection *sharedInstance = nil;

@implementation SocketConnection

- (id)init
{
    self = [super init];

    if (self) 
    {
        while(1)
        {
            Socket *socket;
            int port = 11005;
            NSString *host = @"9.5.3.63";

            socket = [Socket socket];

            @try
            {
                NSMutableData *data;
                [socket connectToHostName:host port:port];
                [socket readData:data];
                //  [socket writeString"Hello World!"];

                //** Connection was successful **//
                [socket retain]; // Must retain if want to use out of this action block.
            }
            @catch (NSException* exception) 
            {
                NSString *errMsg = [NSString stringWithFormat"%@",[exception reason]];
                NSLog(errMsg);
                socket = nil;
            }
        }
    }
    return self;
}

+ (SocketConnection *)getInstance
{
    @synchronized(self) 
    {
        if (sharedInstance == nil) 
        {
            sharedInstance = [[SocketConnection alloc] init];
        }
    }
    return sharedInstance;
}

@end

我还没有弄清楚单例类是如何被调用的。我使用上面的代码启动了我的应用程序,但它没有连接到服务器。有什么想法吗?

谢谢!



Best Answer-推荐答案


你应该创建一个单例类来保持你的连接,就像下面的代码:

h 文件:

#import <Foundation/Foundation.h>
    @interface SocketConnection : NSObject
    {
    }

     + (SocketConnection *)getInstance;

    @end;

m 文件:

#import "SocketConnection.h"

static SocketConnection *sharedInstance = nil;

@implementation SocketConnection

- (id)init
{
    self = [super init];

    if (self) {
    }
    return self;
}

+ (SocketConnection *)getInstance
{
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [[SocketConnection alloc] init];
        }
    }
    return sharedInstance;
}

@end;

关于iphone - 创建 TCP 套接字连接并在多个 View 中保持连接打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10850965/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4