使用iPhone和Android进行套接字连接的不同方法?

时间:2011-05-18 01:10:09

标签: iphone android sockets

我正在编写一个代码来打开一个带有特定端口的服务器的套接字connectiokn。出于某种原因,我无法使其正常工作。我不确定无论我做什么都是正确的,但需要一些示例代码。

2 个答案:

答案 0 :(得分:2)

同时检查以下代码,效果很好......

- (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);
}

答案 1 :(得分:0)

你可以参考以下链接给出一个例子。

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