与 iPhone 和 Android 建立 Socket 连接的不同方法?

发布于 2024-11-08 02:24:22 字数 74 浏览 0 评论 0原文

我正在编写一段代码来打开与具有特定端口的服务器的套接字连接。由于某种原因我无法让它工作。我不确定我所做的是否正确,但需要一些示例代码。

I am writing a code to open a socket connectiokn with server with a specific port. For some reason I am not able to make it working. I am not sure that whatever I am doing is correct or not but need some sample code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2024-11-15 02:24:22

另请检查以下代码,效果很好......

- (IBAction)sendCommand:(id)sender {

    NSHost *host = [NSHost hostWithAddress:@"111.111.111.111"];
    if (host != nil)
    {       
        // iStream and oStream are instance variables
        [NSStream getStreamsToHost:host port:2222 inputStream:&iStream outputStream:&oStream];

                //iStream is instance var of NSSInputStream
        [iStream retain];
        [iStream setDelegate:self];
        [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [iStream open];

                //oStream is instance var of NSSOutputStream
        [oStream retain];
        [oStream setDelegate:self];
        [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [oStream open];     

        NSError *streamError;
        streamError = [iStream streamError];
        streamError = [oStream streamError];

        NSStreamStatus streamStatus;
        streamStatus = [iStream streamStatus];
        streamStatus = [oStream streamStatus];
    }
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
    NSString *io;
    if (theStream == iStream) io = @">>";
    else io = @"<<";

    NSString *event;
    switch (streamEvent)
    {
        case NSStreamEventNone:
            event = @"NSStreamEventNone";
            resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
            resultText.textColor = [UIColor whiteColor];
            resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"];
            break;
        case NSStreamEventOpenCompleted:
            event = @"NSStreamEventOpenCompleted";
            break;
        case NSStreamEventHasBytesAvailable:
            event = @"NSStreamEventHasBytesAvailable";
            if (theStream == iStream)
            {
                                //read data
                uint8_t buffer[1024];
                int len;
                while ([iStream hasBytesAvailable])
                {
                    len = [iStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0)
                    {
                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                        NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
                        if (nil != output)
                        {
                                                   //do something with data
                        }
                    }
                }
            }
            break;
        case NSStreamEventHasSpaceAvailable:
            event = @"NSStreamEventHasSpaceAvailable";
            if (theStream == oStream)
            {
                                //send data
                uint8_t buffer[11] = "I send this";             
                int len;

                len = [oStream write:buffer maxLength:sizeof(buffer)];
                if (len > 0)
                {
                    NSLog(@"Command send");
                    [oStream close];
                }
            }
            break;
        case NSStreamEventErrorOccurred:
            event = @"NSStreamEventErrorOccurred";
            resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
            resultText.textColor = [UIColor whiteColor];
            resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"];
            break;
        case NSStreamEventEndEncountered:
            event = @"NSStreamEventEndEncountered";
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [theStream release];
            theStream = nil;

            break;
        default:
            event = @"** Unknown";
    }

    NSLog(@"%@ : %@", io, event);
}

Also check the following code, It works great...

- (IBAction)sendCommand:(id)sender {

    NSHost *host = [NSHost hostWithAddress:@"111.111.111.111"];
    if (host != nil)
    {       
        // iStream and oStream are instance variables
        [NSStream getStreamsToHost:host port:2222 inputStream:&iStream outputStream:&oStream];

                //iStream is instance var of NSSInputStream
        [iStream retain];
        [iStream setDelegate:self];
        [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [iStream open];

                //oStream is instance var of NSSOutputStream
        [oStream retain];
        [oStream setDelegate:self];
        [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [oStream open];     

        NSError *streamError;
        streamError = [iStream streamError];
        streamError = [oStream streamError];

        NSStreamStatus streamStatus;
        streamStatus = [iStream streamStatus];
        streamStatus = [oStream streamStatus];
    }
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
    NSString *io;
    if (theStream == iStream) io = @">>";
    else io = @"<<";

    NSString *event;
    switch (streamEvent)
    {
        case NSStreamEventNone:
            event = @"NSStreamEventNone";
            resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
            resultText.textColor = [UIColor whiteColor];
            resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"];
            break;
        case NSStreamEventOpenCompleted:
            event = @"NSStreamEventOpenCompleted";
            break;
        case NSStreamEventHasBytesAvailable:
            event = @"NSStreamEventHasBytesAvailable";
            if (theStream == iStream)
            {
                                //read data
                uint8_t buffer[1024];
                int len;
                while ([iStream hasBytesAvailable])
                {
                    len = [iStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0)
                    {
                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                        NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
                        if (nil != output)
                        {
                                                   //do something with data
                        }
                    }
                }
            }
            break;
        case NSStreamEventHasSpaceAvailable:
            event = @"NSStreamEventHasSpaceAvailable";
            if (theStream == oStream)
            {
                                //send data
                uint8_t buffer[11] = "I send this";             
                int len;

                len = [oStream write:buffer maxLength:sizeof(buffer)];
                if (len > 0)
                {
                    NSLog(@"Command send");
                    [oStream close];
                }
            }
            break;
        case NSStreamEventErrorOccurred:
            event = @"NSStreamEventErrorOccurred";
            resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0];
            resultText.textColor = [UIColor whiteColor];
            resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"];
            break;
        case NSStreamEventEndEncountered:
            event = @"NSStreamEventEndEncountered";
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [theStream release];
            theStream = nil;

            break;
        default:
            event = @"** Unknown";
    }

    NSLog(@"%@ : %@", io, event);
}
乖乖公主 2024-11-15 02:24:22

您可以参考以下链接,其中给出了示例。

http://dev.im.ethz.ch/wiki/Socket_communication_on_the_iPhone

you can refer following link which gives an example.

http://dev.im.ethz.ch/wiki/Socket_communication_on_the_iPhone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文