Udp didReceiveData gets two times

I had a problem sending an udp message (broadcast) to the client and receiving a response, but this will be displayed twice. When I test the connection on my PC with the UDP listener, there is only one message.

Maybe someone can give me information on how I can resolve this.

I use the button to start sending a message!

#import "ViewController.h" #import "GCDAsyncUdpSocket.h" @interface ViewController () { long tag; GCDAsyncUdpSocket *udpSocket; } @end @implementation ViewController - (void)setupSocket { udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *error = nil; if (![udpSocket bindToPort:1000 error:&error]) { NSLog(@"Error binding: %@", error); return; } if (![udpSocket beginReceiving:&error]) { NSLog(@"Error receiving: %@", error); return; } [udpSocket enableBroadcast:YES error: &error]; NSLog(@"Ready"); } - (void)viewDidLoad { [super viewDidLoad]; if (udpSocket == nil) { [self setupSocket]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)send:(id)sender{ NSString *host = @"192.168.2.255"; if ([host length] == 0) { NSLog(@"Address required"); return; } NSLog(@"%@",host); int port = 8888; NSString *msg = @"1,0,1,2"; NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding]; [udpSocket sendData:data toHost:host port:port withTimeout:-1 tag:tag]; NSLog(@"SENT (%i): %@", (int)tag, msg); tag++; } - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext { NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; if (msg) { NSLog(@"RECV: %@", msg); tag++; NSLog(@"%li",tag); } else { NSString *host = nil; uint16_t port = 0; [GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address]; NSLog(@"RECV: Unknown message from: %@:%hu", host, port); } } @end 

Here is the conclusion!

 2013-09-11 09:49:00.132 udptest[5145:907] 15 2013-09-11 09:49:08.218 udptest[5145:907] 192.168.2.255 2013-09-11 09:49:08.220 udptest[5145:907] SENT (15): 1,0,1,2 2013-09-11 09:49:08.319 udptest[5145:907] RECV: 0,0,0,0,0,0,0,0 2013-09-11 09:49:08.321 udptest[5145:907] 17 2013-09-11 09:49:08.323 udptest[5145:907] RECV: 0,0,0,0,0,0,0,0 2013-09-11 09:49:08.324 udptest[5145:907] 18 

I would really appreciate it if someone could help me.

+4
source share
2 answers

I have the same strange unwanted and unresolved behavior: the "sender" sends one UDP broadcast message, and the "receiver" receives two messages.

I researched as much as I could, and these are my findings:

1) Wireshark receives only one UDP message.

2) udpSocket: didReceiveData: fromAddress: withFilterContext: is launched twice!

3) Parsing the address section using [GCDAsyncUdpSocket getHost: port: fromAddress:] causes host = :: ffff: 192.168.1.118 FIRST time, when host = 192.168.1.118 - SECOND time.

Hope this would be useful in some way ...

EDIT (with possible SOLUTION)

The FIRST address (see paragraphs 2 and 3) is the actual IPv6 address. Therefore, I assume that udpSocket: didReceiveData: ... fired twice as fast when the sender is an IPv6 address and the second time the sender is an IPv4 address of the same address .

And so my solution is to only enable IPv4 in a UDP socket:

 [udpSocket setIPv4Enabled:YES]; [udpSocket setIPv6Enabled:NO]; 
+9
source

Whether the response message and the request message are the same in terms of what they contain. If so, then here is one of the scenarios you may come across. Maybe the first package is a broadcast that you listen to yourself, and the second is the answer. To be more precise, when sending a broadcast (pkt p1), the sender can also receive a copy of p1. Then, when the receiver sends the response (pkt p2), you also see the response.

So how do we check it. You can look at the sender address (UDP provides an option), and then check if this is your address or the address of another host.

0
source

Source: https://habr.com/ru/post/1501614/


All Articles