可可 - 如何使用ARC制作TCP套接字?

时间:2012-07-17 16:45:00

标签: cocoa sockets stream tcpclient

你知道一些很好的教程,教你如何建立一个简单的TCP套接字连接到ip,并只是发送JSON到它?

我感到很沮丧,因为我找不到容易实现的东西(我还是新手)。

非常感谢你们!

1 个答案:

答案 0 :(得分:3)

我刚刚关注了raywenderlich教程:

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

并对流的启动做了一点调整:

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