2

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に入れてみましたが、ループが無限であり、ビューが読み込まれません。プロジェクトにいくつかのビューがあります。接続を開き、すべてのビューで常に接続を開いたままにします。

目的:

  • アプリが最初に読み込まれるときにTCPソケット接続を開きます
  • 私がどのビューにいても、接続を無限に維持します(プロジェクトの複数のビュー)

私はまだiOS開発に慣れていないので、できるだけ明確に感謝します。SmallSocketsライブラリを使用してSockets接続を開いていることに注意してください。助けてくれてありがとう!

* 編集 *

以下の答えに基づいて、これは私がこれまでに行ってきたことです:

SocketConnection.h

#import <Foundation/Foundation.h>

@interface SocketConnection : NSObject
{

}

+ (SocketConnection *)getInstance;

@end

SocketConnection.m

static 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

シングルトンクラスがどのように呼び出されるかはまだわかりません。上記のコードでアプリを起動しましたが、サーバーに接続していません。何か案は?

ありがとう!

4

1 に答える 1

4

以下のコードのように接続を維持するには、シングルトン クラスを作成する必要があります。

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;
于 2012-06-01T14:55:07.280 に答える