2016-02-05 42 views
0

我试图创建一个测试应用程序来打开PC和iPhone之间的UDP连接。PC和iPhone之间的UDP通信机制?

据我所知,

对于UDP,2电脑可以互相通过对方的IP地址和端口号进行通信。

这怎么能在PC和iPhone之间完成?

我知道iPhone有自己的IP地址就像PC一样,但是如何收听/发送端口号?

回答

1

您可以使用NSStream实现UDP套接字通信。

使用此代码建立一个socket连接:

NSInputStream *inputStream; 
NSOutputStream *outputStream; 

- (void)initNetworkCommunication { 
    CFReadStreamRef readStream; 
    CFWriteStreamRef writeStream; 
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 80, &readStream, &writeStream); 
    inputStream = (NSInputStream *)readStream; 
    outputStream = (NSOutputStream *)writeStream; 
} 

本教程将帮助您: http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server