Send "Hello World" message via udp from iPhone

iphone, objective-c, sockets, udp

Solution

download GCDAsyncUdpSocket from here. then you may send packets as:

 GCDAsyncUdpSocket *udpSocket ; // create this first part as a global variable
 udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

 NSData *data = [ 
                   [NSString stringWithFormat:@"%Hello Wold" 
                    dataUsingEncoding:NSUTF8StringEncoding
                ];

[udpSocket sendData:data toHost:@"192.168.10.111" port:550 withTimeout:-1 tag:1];

Problem

I am new to objective C and I need to send a simple message via UDP. The server part is working cause it is implemented in C#. The server code in C# is: ``` var server = new UdpClient(8585); var groupEP = new IPEndPoint(IPAddress.Parse("192.168.0.120"),8585); byte[] bytes = server.Receive(ref groupEP); ``` and the client part in c# is: ``` System.Net.Sockets.UdpClient client; client = new System.Net.Sockets.UdpClient("192.168.0.120",8585); client.Send(new byte[]{1,2,3,4},4); ``` how can I do the same client part in objective c? I know there are a lot of tutorials on the internet such as this library. when I import that library to my project I do not know how to instantiate a new object. I have tried: ``` [[GCDAsyncUdpSocket alloc] initWithSocketQueue:... ??? I don't know how to initialize it. ``` I will appreciate if someone can show me a simple example of how could I replicate the client part into objective c.

Original source

Related problems