GCDAsyncUdpSocket und Multicast senden und empfangen
Im ersten Ansatz erstelle ich eine Client-Server-App, basierend aufsampleProject , die einige Daten an den Server senden.
Legend:
sender
address = reciver ip
port = reciver port
reciver
address = null since he is listening
port = in my case 55555
ArbeitscodeDas Überspringen der Fehlerprüfung ist nur aus öffentlichen Gründen beabsichtigt
Absender
-(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++;
}
Empfänger / Zuhörer
-(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
}
MulticastAber dann möchte ich einen Client, der an viele Empfänger sendet. Formular, von dem ich weiß, dass Sendre oder Listener [GCDAsyncUdpSocket joinMulticastGroup: error] verwenden sollten ;. Ich lief durch Stackoverflow, Onkel Google und CococaAsyncSocket (wo kein Wort über UDP steht), stimmte mit gesammelten Informationen überein und fand diesen Code. Ich bin mir ganz sicher, dass das nicht funktioniert, aber ich habe keine Ahnung warum.
Legend:
sender
address = sender ip
port = sender port in my case 55555
reciver
address = sender ip
port = sender port in my case 55555
Code funktioniert nicht-(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++;
}
Empfänger / Zuhörer
-(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
}
AKTUALISIEREN:Es stellt sich heraus, dass wenn ich eine unbenutzte IP als verwendeaddress
zum Beispiel @ "224.0.1.1", dann funktioniert es, aber es fühlt sich ein bisschen komisch an. Mache ich es richtig?