The GCDAsyncUdpSocket does call didSendDataWithTag , but not call didReceiveData

292 views
Skip to first unread message

pl...@mobileiron.com

unread,
Sep 24, 2018, 9:01:27 PM9/24/18
to CocoaAsyncSocket
I almost copy the GCD UDP echo server / client implementation in my project, but the GCDAsyncUdpSocket does call didSendDataWithTag when sent, but not call didReceiveData even though I saw the incoming UDP packets come to the bound port from WireShark.

in .h

@interface UdpDirect : UdpHandler <GCDAsyncUdpSocketDelegate>


in .m


        _udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

                    // delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)];

        if (![_udpSocket bindToPort:[localEndpoint.port intValue] error:&error]) {

            NELogError(error, @"%@ Error when bind UDP socket to port %@", _connName, localEndpoint.port);

            _udpSocket = nil;

            return NO;

        }

        if (![_udpSocket beginReceiving:&error]) {

            NELogError(error, @"%@ Error when begin receiving from UDP socket", self.connName);

            [_udpSocket close];

            _udpSocket = nil;

            return NO;

        }

pl...@mobileiron.com

unread,
Sep 24, 2018, 9:08:08 PM9/24/18
to CocoaAsyncSocket
This use case is running in iOS network extension.

Arno Gramatke

unread,
Sep 25, 2018, 2:12:50 AM9/25/18
to cocoaasy...@googlegroups.com
Maybe a typo in the delegate method? Can you send all relevant code?

--
You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsock...@googlegroups.com.
To post to this group, send email to cocoaasy...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cocoaasyncsocket/cf03d1bc-a63c-4e98-a3f7-775d27e444d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pl...@mobileiron.com

unread,
Sep 25, 2018, 12:09:20 PM9/25/18
to CocoaAsyncSocket
Thank you, Arno! Here is the delegate code below.
 

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag

{

    NELogD(@"%@ Datagram sent %lu", _connName, tag);

}


- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error

{

    // You could add checks here

    NELogError(error, @"%@ Could not send datagram %lu", _connName, tag);

}


- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data

                                            fromAddress:(NSData *)address

                                            withFilterContext:(nullable id)filterContext

{

    NELogS(@"%@ Read UDP Datagrams", _connName);

    NWHostEndpoint * remoteEP = [UdpDirect fromGCDAddress:address];

    NSString * remoteEpName = [UdpHandler endpointName:remoteEP];

    NELogS(@"%@ Read UDP Datagrams from remote %@", _connName, remoteEpName);

    [self.connection respond:data fromEndpoint:remoteEP];

}

Arno Gramatke

unread,
Sep 25, 2018, 12:23:25 PM9/25/18
to cocoaasy...@googlegroups.com
This looks ok so far. Are you retaining the socket correctly, i.e. strong property or similar?

Maybe also try to implement 

- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError  * _Nullable)error;

Maybe for some reason the socket is closed. That could give you a hint what is going on.

Is „localEndpoint.port“ initialized correctly and not nil? Maybe you are binding to port 0 instead of what you would expect and the OS assigns a random port.

If this doesn’t help, you probably should post the whole class (.h and .m) and not only the snippets. That’s what I meant with „all relevant code“. 

-- 
You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsock...@googlegroups.com.
To post to this group, send email to cocoaasy...@googlegroups.com.

pl...@mobileiron.com

unread,
Sep 25, 2018, 2:55:38 PM9/25/18
to CocoaAsyncSocket
Arno, 

I added strong on the private udpSocket declaration and overwrite the udpSocketDidClose

@property (nonatomic, strong) GCDAsyncUdpSocket *udpSocket;


But that is not help for my problem. From my app log, my app sent 17 udp packets in three seconds retrying and socket was not closed during retrying. From Wireshark, each packet has been sent from the port my app bound with and got the response packet from server to the same local port. But just udpSocket:didReceiveData has never been called.


Is the delegate queue matter?


Thank you!



On Tuesday, September 25, 2018 at 9:23:25 AM UTC-7, Arno Gramatke wrote:
This looks ok so far. Are you retaining the socket correctly, i.e. strong property or similar?

Maybe also try to implement 

- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError  * _Nullable)error;

Maybe for some reason the socket is closed. That could give you a hint what is going on.

Is „localEndpoint.port“ initialized correctly and not nil? Maybe you are binding to port 0 instead of what you would expect and the OS assigns a random port.

If this doesn’t help, you probably should post the whole class (.h and .m) and not only the snippets. That’s what I meant with „all relevant code“. 
Am 25.09.2018 um 18:09 schrieb pl...@mobileiron.com:

Thank you, Arno! Here is the delegate code below.
 
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag
{
    NELogD(@"%@ Datagram sent %lu", _connName, tag);
}

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error
{
    // You could add checks here
    NELogError(error, @"%@ Could not send datagram %lu", _connName, tag);
}

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
                                            fromAddress:(NSData *)address
                                            withFilterContext:(nullable id)filterContext
{
    NELogS(@"%@ Read UDP Datagrams", _connName);
    NWHostEndpoint * remoteEP = [UdpDirect fromGCDAddress:address];
    NSString * remoteEpName = [UdpHandler endpointName:remoteEP];
    NELogS(@"%@ Read UDP Datagrams from remote %@", _connName, remoteEpName);
    [self.connection respond:data fromEndpoint:remoteEP];
}

-- 
You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsocket+unsub...@googlegroups.com.

Arno Gramatke

unread,
Sep 25, 2018, 2:58:10 PM9/25/18
to cocoaasy...@googlegroups.com
Again, show us all the code, not just parts. Otherwise we cannot help.

To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsock...@googlegroups.com.

To post to this group, send email to cocoaasy...@googlegroups.com.

pl...@mobileiron.com

unread,
Sep 25, 2018, 5:10:14 PM9/25/18
to CocoaAsyncSocket
Here is my code which related with GCDAsyncUdpSocket with the latest version from CocoaPots.

Thank you again!

Paul Ling
To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsocket+unsubscribe...@googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsocket+unsub...@googlegroups.com.
To post to this group, send email to cocoaasy...@googlegroups.com.
MIUdpDirect.h
MIUdpDirect.m

Arno Gramatke

unread,
Sep 25, 2018, 5:49:40 PM9/25/18
to cocoaasy...@googlegroups.com
Again not the full code. Superclass and imports are missing. I have no idea what the rest of your code does...

Do you call your open method more than once? If so, you are creating a new socket on every call while the existing one is deallocated. It is possible that you run into race conditions here. The first if-statement in this method is always true. The else statement is never called. But I am just guessing.

To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsock...@googlegroups.com.

To post to this group, send email to cocoaasy...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
<MIUdpDirect.h><MIUdpDirect.m>

Message has been deleted

pl...@mobileiron.com

unread,
Sep 25, 2018, 7:27:32 PM9/25/18
to CocoaAsyncSocket
Thank you, Arno! 

For certain reason, I cannot give you all sources in my project, but I believe that other files are not related with UDP sending and receiving. If you look the MIUdpDirect.m, it has all codes deals with GCDAsyncUdpSocket. I shared related log lines below instead.

From my app's log, there isn't sign showed open method has been called more than once for one udp socket. "if (socket == nil)" will be always true because of socket management change history due to troubleshooting of this UDP receiving issue.

I tried to build project with GCDAsyncUdpSocket log by change #if 0 to #if 1 at line #32 of GCDAsyncUdpSocket.m, but build got failed. And the breakpoint at line #4384 has never been hit during debugging.

Paul Ling

default 14:13:36.348512 -0700 AppProxy-iOS [Dbg] Open UDP connection conn_udp_15 -[MIConnectionUDP open]:510
default 14:13:36.348736 -0700 AppProxy-iOS [Dbg] Opening UDP flow conn_udp_15 from Endpoint  192.168.168.195:50014 -[MIConnectionUDP open]:521
default 14:13:36.348942 -0700 AppProxy-iOS [Vrb] Allocate new UDP socket for conn_udp_15 from EP 192.168.168.195:50014 -[MIUdpDirect open:]:61
default 14:13:36.349096 -0700 AppProxy-iOS [Dbg] Added UDP socket from 192.168.168.195:50014 for conn_udp_15 -[MIUdpDirect open:]:80
default 14:13:36.350635 -0700 AppProxy-iOS [Dbg] Opening UDP flow conn_udp_16 from Endpoint  192.168.168.195:0 -[MIConnectionUDP open]:521
default 14:13:36.350804 -0700 AppProxy-iOS [Vrb] Allocate new UDP socket for conn_udp_16 from EP 192.168.168.195:0 -[MIUdpDirect open:]:61
default 14:13:36.350971 -0700 AppProxy-iOS [Dbg] Added UDP socket from 192.168.168.195:62281 for conn_udp_16 -[MIUdpDirect open:]:80
default 14:13:36.351978 -0700 AppProxy-iOS [Dbg] App com.microsoft.lync2013.iphone added connection conn_udp_15 (connection count = 10) -[MIApplication addConnection:]:60
default 14:13:36.352119 -0700 AppProxy-iOS [Dbg] UDP flow for conn_udp_15 opened. -[MIConnectionUDP open]_block_invoke:534
default 14:13:36.352598 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:36.359318 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:36.359807 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:36.366290 -0700 AppProxy-iOS [Dbg] Opening UDP flow conn_udp_17 from Endpoint  192.168.168.195:0 -[MIConnectionUDP open]:521
default 14:13:36.366849 -0700 AppProxy-iOS [Vrb] Allocate new UDP socket for conn_udp_17 from EP 192.168.168.195:0 -[MIUdpDirect open:]:61
default 14:13:36.367207 -0700 AppProxy-iOS [Dbg] Added UDP socket from 192.168.168.195:53274 for conn_udp_17 -[MIUdpDirect open:]:80
default 14:13:36.481817 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:36.483824 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:36.496380 -0700 AppProxy-iOS [Dbg] Opening UDP flow conn_udp_18 from Endpoint  192.168.168.195:0 -[MIConnectionUDP open]:521
default 14:13:36.497780 -0700 AppProxy-iOS [Vrb] Allocate new UDP socket for conn_udp_18 from EP 192.168.168.195:0 -[MIUdpDirect open:]:61
default 14:13:36.498587 -0700 AppProxy-iOS [Dbg] Added UDP socket from 192.168.168.195:59478 for conn_udp_18 -[MIUdpDirect open:]:80
default 14:13:36.899711 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:36.914380 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 0 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:36.914761 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:36.915067 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 1 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:36.915222 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:36.915445 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 2 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:36.924936 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:36.925529 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:36.938708 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:36.939094 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 3 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:36.966165 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:36.966723 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:36.980298 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:36.980545 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 4 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:37.286512 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:37.287716 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:37.329970 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:37.330750 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 5 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:37.415582 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:37.416027 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:37.427072 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:37.427150 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 6 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:37.716891 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:37.717881 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:37.746091 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:37.748643 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 7 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:37.878586 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:37.882323 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:37.885896 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:37.891030 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 8 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.021831 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.023076 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.037769 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.038213 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 9 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.097993 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.098521 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.099218 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.099297 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 10 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.152687 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.152959 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.178454 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.178980 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.262602 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.263354 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 11 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.264967 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.266669 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 12 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.339998 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.341279 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.351278 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.351911 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 13 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.398882 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.399301 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.403680 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.403873 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 14 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.523345 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.526356 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.528804 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.531842 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 15 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.760144 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.761500 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.765482 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.766094 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 16 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.841998 -0700 AppProxy-iOS [Dbg] conn_udp_15 Send UDP packet 140 bytes to Endpoint mediaedge1a.online.lync.com:3478 -[MIConnectionUDP setupDataFlow]_block_invoke:610
default 14:13:38.842575 -0700 AppProxy-iOS [Dbg] Starting UDP flow timer for conn_udp_15 -[MIConnectionUDP startInactivityTimer]:658
default 14:13:38.853236 -0700 AppProxy-iOS [Dbg] Datagram written -[MIUdpDirect udpSocket:didSendDataWithTag:]:149
default 14:13:38.853706 -0700 AppProxy-iOS [Dbg] conn_udp_15 Non DNS UDP sent 17 -[MIUdpDirect udpSocket:didSendDataWithTag:]:150
default 14:13:38.943253 -0700 AppProxy-iOS [Inf] UDP conn_udp_15 end of stream/datagrams -[MIConnectionUDP setupDataFlow]_block_invoke:557
default 14:13:38.943448 -0700 AppProxy-iOS [Inf] conn_udp_15 UDP handler closed. Outgoing Total packets = 18 read from flow, 18 delivered to endpoint -[MIConnectionUDP closeUdpHandler]:496
default 14:13:38.943611 -0700 AppProxy-iOS [Inf] conn_udp_15 UDP handler closed. Incoming Total packets = 0 recieved from endpoint, 0 delivered to flow -[MIConnectionUDP closeUdpHandler]:498
default 14:13:38.943790 -0700 AppProxy-iOS [Dbg] Close UDP 192.168.168.195:50014 with conn_udp_15 -[MIUdpDirect close]:97
default 14:13:38.943993 -0700 AppProxy-iOS [Inf] conn_udp_15 UDP connection closed. Total bytes = 2520 read, 0 written -[MIConnectionUDP closeConnection:]:487
default 14:13:38.944152 -0700 AppProxy-iOS [Dbg] App com.microsoft.lync2013.iphone removed connection conn_udp_15 (connection count = 9) -[MIApplication connectionDidExit:]:89
default 14:13:38.947025 -0700 AppProxy-iOS [Vrb] conn_udp_15 Closed UDP Socket -[MIUdpDirect udpSocketDidClose:withError:]:174
default 14:13:48.016393 -0700 AppProxy-iOS [Dbg] conn_udp_15 UDP activity timer = 1 / 3 (idle) -[MIConnectionUDP startInactivityTimer]_block_invoke:668
default 14:13:58.012649 -0700 AppProxy-iOS [Dbg] conn_udp_15 UDP activity timer = 2 / 3 (idle) -[MIConnectionUDP startInactivityTimer]_block_invoke:668
default 14:14:08.038257 -0700 AppProxy-iOS [Dbg] conn_udp_15 UDP activity timer = 3 / 3 (idle) -[MIConnectionUDP startInactivityTimer]_block_invoke:668
default 14:14:08.041559 -0700 AppProxy-iOS [Dbg] UDP connection conn_udp_15 has timed out -- exiting. -[MIConnectionUDP startInactivityTimer]_block_invoke:670
default 14:14:08.043422 -0700 AppProxy-iOS [Inf] conn_udp_15 UDP handler closed. Outgoing Total packets = 18 read from flow, 18 delivered to endpoint -[MIConnectionUDP closeUdpHandler]:496
default 14:14:08.044166 -0700 AppProxy-iOS [Inf] conn_udp_15 UDP handler closed. Incoming Total packets = 0 recieved from endpoint, 0 delivered to flow -[MIConnectionUDP closeUdpHandler]:498
default 14:14:08.045066 -0700 AppProxy-iOS [Inf] conn_udp_15 UDP connection closed. Total bytes = 2520 read, 0 written -[MIConnectionUDP closeConnection:]:487
default 14:14:08.045564 -0700 AppProxy-iOS [Dbg] App com.microsoft.lync2013.iphone removed connection conn_udp_15 (connection count = 6) -[MIApplication connectionDidExit:]:89

pl...@mobileiron.com

unread,
Sep 26, 2018, 1:30:11 AM9/26/18
to CocoaAsyncSocket
I think I found the reason. My app is running as NEAppProxyProvider, get UDP flow from iOS for the specified third-party apps. Looks like iOS has already taken the UDP port that third party app specified during its datagram socket bind() call, my app may not be able bind to this port any more.

The weird thing is that GCDAsyncUdpSocket doesn't report bind error for this case, my app can bind without failure and send outgoing UDP packets (verified from WireShark), but somehow incoming packets doesn't notify to my app.

You can try make NEAppProxyProvider app and test with UDP echo client which bind to special port.

Thank you, Arno!

Paul Ling

Arno Gramatke

unread,
Sep 26, 2018, 2:54:13 AM9/26/18
to cocoaasy...@googlegroups.com
I don’t know about the NEAppProxyProvider, but after reading about it a little bit it seems that you should not use the port of the local endpoint to bind to. Try to use „0“ to get port assigned by the OS.

After reading the documentation, it seems, that the data to and from the app to be proxied is intercepted by NEAppProxyUDPFlow. So your app communicates through NEAppProxyUDPFlow with the app that you are proxying. Therefore localEndpoint represents the socket that was created by the app to be proxied.

The connection to the real endpoint should probably use a different port. Use „0“ as port to get a random port assigned by the OS when binding.

So for a packet coming from the proxied app going to the real endpoint would take the following route:

<proxied app sends packet through its socket> => <Your app receives packets through NEAppProxyUdpFlow with calling readDatagramsWithCompletionHandler:> => <Your app sends data through your GCDAsyncUdpSocket to the real destination>.

For the reverse direction it would be like this:

<Your GCDAsyncUdpSocket receives packets through the delegate callback on its own port> => <Your app sends data to the proxied app through the NEAppProxyUdpFlow with writeDatagrams:sentByEndpoints:completionHandler:.

Does this make sense?

--
You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocoaasyncsock...@googlegroups.com.
To post to this group, send email to cocoaasy...@googlegroups.com.

pl...@mobileiron.com

unread,
Sep 26, 2018, 1:02:36 PM9/26/18
to CocoaAsyncSocket
Arno, you're absolutely right. 

But when I inspected the local port number that NWAppProxyUdpFlow tells my app, I found lot of flows with port 0, few flows with special non-zero port. if the proxied app has to be bound before sending packets via its socket, then the iOS should not send port 0, should be a port that iOS assigned to this udp client socket without explicit bind. From this fact, I guess the iOS doesn't bind for the proxied apps because that is not really necessary if UDP packets pass between the proxied app and my app. If iOS does this for not explicit bind socket, why not does the same thing with explicit bind socket and in turn ask my app to bind to the specified port for the proxied app? NEAppProxyFlow has another method openwithLocalEndpoint:completionHandler: which ask for my app to provide real local endpoint.

If iOS treats the bind socket and unbind socket in the different way, then there is no way to support some apps those require verify source port with another protocol, such as SIP/SDP.

Thank you for your helping!

Paul Ling

Arno Gramatke

unread,
Sep 26, 2018, 5:38:39 PM9/26/18
to cocoaasy...@googlegroups.com

pl...@mobileiron.com

unread,
Sep 26, 2018, 7:06:00 PM9/26/18
to CocoaAsyncSocket
For the GCDAsyncUdpSocket library, it works if my app doesn't bind with the same local port as the UDP flow. But it should not successfully bind with the same port still.

Paul

Arno Gramatke

unread,
Sep 26, 2018, 8:09:40 PM9/26/18
to cocoaasy...@googlegroups.com
Are you sure that the port you try to bind to is already in use when you issue the bind?

Also, maybe iOS enables SO_REUSEPORT on the socket and you could use it too, if you enable the same on your socket (see enableReusePort: error:).

I don’t know what magic Apple is doing to move the proxy in between ...

Reply all
Reply to author
Forward
0 new messages