In first approach I create client-server app ,based on sampleProject , which send some data to server.
Legend:
sender
address = reciver ip
port = reciver port
reciver
address = null since he is listening
port = in my case 55555
Working code
skipping error checking is intentional only for public reasons
Sender
-(id*)initForSender:(NSString*)address port:(int)port
{
self = [super init];
if (self) {
_port = port;
_address = address;
tag = 0;
_udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[_udpSocket bindToPort:0 error:&error];
[_udpSocket beginReceiving:&error];
return self;
}
}
-(void)send:(NSData*)data{
[_udpSocket sendData:data toHost:_address port:_port withTimeout:-1 tag:tag];
tag++;
}
Receiver / Listener
-(id*)initForReceiver:(NSString*)address port:(int)port
{
self = [super init];
if (self) {
_port = port;
_udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
[_udpSocket bindToPort:0 error:&error];
[_udpSocket beginReceiving:&error];
}
return self;
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
//Do something with receive data
}
Multicast
But then I want client which will send to many receivers. Form I know sendre or listener should use [GCDAsyncUdpSocket joinMulticastGroup:error];. I ran throu stackoverflow, uncle google and CococaAsyncSocket (where is no word abou udp), match gathered pieces of information and came up with this code. I'm perfectly sure, that is not working but I don't have a clue why.
Legend:
sender
address = sender ip
port = sender port in my case 55555
reciver
address = sender ip
port = sender port in my case 55555
Not working code
-(id*)initForSender:(NSString*)address port:(int)port
{
self = [super init];
if (self) {
_port = port;
_address = address;
_udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
[_udpSocket bindToPort:_port error:&error];
[_udpSocket joinMulticastGroup:_address error:&error];
[_udpSocket enableBroadcast:YES error:&error];
}
return self;
}
-(void)send:(NSData*)data{
[_udpSocket sendData:data toHost:_address port:_port withTimeout:-1 tag:tag];
tag++;
}
Receiver / Listener
-(id*)initForReceiver:(NSString*)address port:(int)port
{
self = [super init];
if (self) {
_port = port;
_address = address;
_udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
[_udpSocket bindToPort:_port error:&error];
[_udpSocket joinMulticastGroup:_address error:&error];
[_udpSocket beginReceiving:&error])
}
return self;
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
//Do something with receive data
}
UPDATE:
It's turn out that when I use some unuse IP as address
for example @"224.0.1.1" then it wroks, but it feels weird a bit. Am I doing it correct?