Search code examples
iosiphonesocketstcpudp

Mechanism of UDP communication between PC and iPhone?


I am trying to create a test app to open up UDP connection between a PC and iPhone.

As far as i know,

For UDP, 2 PC's can communicate with each other via each other's IP address and a port number.

How can this be done between a PC and iPhone?

I understand iPhone has its own ip address just like a PC, but what about a port number to listen to/ send?


Solution

  • You can use NSStream to implement UDP socket communication.

    Use this code for establishing a socket connection:

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

    This tutorial will help you : http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server