2012-07-17 79 views
2

你知道一些很好的教程,教導如何使一個簡單的TCP套接字連接到一個IP,併發送JSON到它?可可 - 如何用ARC創建TCP Socket?

我越來越沮喪,因爲找不到容易實現的東西(我是新鮮的noob)。

非常感謝你們!

回答

3

好吧,我只是跟着raywenderlich教程:

http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server

和maked稍加調整到流的啓動:

- (void) initNetworkCommunication { 
    CFReadStreamRef readStream; 
    CFWriteStreamRef writeStream; 
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"xx.xxx.xxx.xxx", 1256, &readStream, &writeStream); 
    inputStream = objc_unretainedObject(readStream); 
    outputStream = objc_unretainedObject(writeStream); 
    [inputStream setDelegate:self]; 
    [outputStream setDelegate:self]; 
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [inputStream open]; 
    [outputStream open]; 
} 
+0

您還可能有興趣在CocoaAsyncSocket其中,這一切很好。 https://github.com/robbiehanson/CocoaAsyncSocket/ – 2012-07-24 15:32:25