简体   繁体   English

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

[英]Different ways to make a Socket Connection with iPhone and Android?

I am writing a code to open a socket connectiokn with server with a specific port. 我正在编写一个代码来打开一个带有特定端口的服务器的套接字connectiokn。 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. 我不确定我所做的事情是否正确但需要一些示例代码。

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

you can refer following link which gives an example. 你可以参考下面给出一个例子的链接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM