GCDAsyncUdpSocket y envío y recepción de multidifusión
En primer lugar creo una aplicación cliente-servidor, basada enmuestraproyecto , que envían algunos datos al servidor.
Legend:
sender
address = reciver ip
port = reciver port
reciver
address = null since he is listening
port = in my case 55555
Código de trabajoSaltar la verificación de errores es intencional solo por razones públicas
Remitente
-(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++;
}
Receptor / Oyente
-(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
}
MultidifusiónPero luego quiero que el cliente lo envíe a muchos receptores. El formulario que sé que sendre o listener debería usar [GCDAsyncUdpSocket joinMulticastGroup: error] ;. Ejecuté throu stackoverflow, el tío Google y CococaAsyncSocket (donde no hay una palabra sobre udp), uní información recopilada y di con este código. Estoy perfectamente seguro de que eso no funciona, pero no tengo ni idea de por qué.
Legend:
sender
address = sender ip
port = sender port in my case 55555
reciver
address = sender ip
port = sender port in my case 55555
Código no funciona-(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++;
}
Receptor / Oyente
-(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
}
ACTUALIZAR:Resulta que cuando uso un poco de uso de IP comoaddress
por ejemplo, @ "224.0.1.1" entonces funciona, pero se siente un poco raro. ¿Lo estoy haciendo bien?