2

你知道一些很好的教程,教如何建立一个简单的 TCP 套接字连接到一个 IP,然后发送 JSON 给它吗?

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

非常感谢你们!

4

1 回答 1

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];
}
于 2012-07-24T15:29:54.340 回答