2015年9月4日 星期五

筆記 - 在 iOS 上傳送資料給伺服器使用 TCP Client

操作環境:Mac OS 10.10.4、Xcode 6.4

以下以在一個 view controller 內做初始化以及傳資料的動作為例子。
(基本上是原文,加上自己文字敘述和 Xcode 的版本差異修改,未來在複習時比較好理解… XD)

  1. 首先在 h 檔放上紅框內的 code
  2.  並在相對應的 m 檔,可以做預先處理的位置處理 code
  3. 這樣就可以傳送資料給伺服器了
  4. handleEvent 是 NSStreamDelegate 用來處理回傳值的 method,在這篇例子裡把有 release 及部份行做修改。
    //****************************************
    //****************************************
    //********** TCP CLIENT RECEIVE **********
    //****************************************
    //****************************************
    - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)StreamEvent
    {
        switch (StreamEvent)
        {
            case NSStreamEventOpenCompleted:
                NSLog(@"TCP Client - Stream opened");
                break;
            case NSStreamEventHasBytesAvailable:
                if (theStream == InputStream)
                {
                    uint8_t buffer[1024];
                    int len;
                    
                    while ([InputStream hasBytesAvailable])
                    {
                        len = [InputStream read:buffer maxLength:sizeof(buffer)];
                        if (len > 0)
                        {
                            NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                            
                            if (nil != output)
                            {
                                NSLog(@"TCP Client - Server sent: %@", output);
                            }
                        }
                    }
                }
                break;
            case NSStreamEventErrorOccurred:
                NSLog(@"TCP Client - Can't connect to the host");
                break;
            case NSStreamEventEndEncountered:
                NSLog(@"TCP Client - End encountered");
                [theStream close];
                [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                break;
            case NSStreamEventNone:
                NSLog(@"TCP Client - None event");
                break;
            case NSStreamEventHasSpaceAvailable:
                NSLog(@"TCP Client - Has space available event");
                if (OutputData != nil)
                {
                    //Send rest of the packet
                    int ActualOutputBytes = [OutputStream write:[OutputData bytes] maxLength:[OutputData length]];
                    
                    if (ActualOutputBytes >= [OutputData length])
                    {
                        //It was all sent
                        OutputData = nil;
                    }
                    else
                    {
                        //Only partially sent
                        [OutputData replaceBytesInRange:NSMakeRange(0, ActualOutputBytes) withBytes:NULL length:0];  //Remove sent bytes from the start
                    }
                }
                break;
            default:
                NSLog(@"TCP Client - Unknown event");
        }
    }
    
  5. 最後,不需要使用時就做清空的動作

Source: http://www.ios-developer.net/iphone-ipad-programmer/development/tcpip/tcp-client

沒有留言:

張貼留言