-1

GCDAsyncSocket を使用してサーバー接続をテストしようとしています。

https://github.com/robbiehanson/CocoaAsyncSocket

IP + ポートに接続して、機能したかどうかに関係なくメッセージを取得したい。

私は今ここまでです。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{ // アプリケーションの起動後にカスタマイズするポイントをオーバーライドします。

dispatch_queue_t mainQueue = dispatch_get_main_queue();

asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];


    NSString *host = @"www.google.de";
    uint16_t port = 21;

    NSError *error = nil;

    if(![asyncSocket connectToHost:host onPort:port error:&error])
    {
        NSLog(@"ERROR %@!!!", error);
    }
    else{
        NSLog(@"NO ERROR %@!!!", error);
    }

    [asyncSocket writeData:@"DATA" withTimeout:3 tag:0];

return YES;

}

しかし、データが書き込まれたかどうかを確認するにはどうすればよいですか?

if(![asyncSocket connectToHost:ホスト onPort:ポート エラー:&エラー])

常にノーエラーになります。

4

1 に答える 1

0

socket:didWriteDataWithTag:デリゲート メソッドを実装する必要があります。「GCDAsyncSocket.h」から:

/**
 * Called when a socket has completed writing the requested data. Not called if there is an error.
**/
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;

書き込みがタイムアウトすると、接続が閉じられ、デリゲート メソッド

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;

と呼ばれます。

于 2013-05-21T11:51:51.833 に答える