How to get remote ip address and port from active udp connection?

2.3k Views Asked by At

I'm working on a graphical windows application. So solutions on c++ and c# are prefered.

For my application, I need to get the remote address and port from active udp connection from a specific process.

I tried IP Helper API but the methods for UDP don't give remote address and port.

I've already see these posts Get Destination Ip/Port of active udp Connection? and Remote address of active UDP connections in Windows using IP Helper.

I understand why IP Helper can't do the job (udp is connectionless and need to capture packet) but I found nothing concrete how to accomplish that.

Have you a solution ready to use or something close to it?

1

There are 1 best solutions below

3
On

As you have already discovered, UDP is connectionless, so the OS doesn't track remote party info, like it does for TCP. Unlike a TCP socket, a UDP socket can communicate with multiple remote parties at a time, where sendto() specifies a destination ip/port, and recvfrom() reports a sender's ip/port.

I understand why IP Helper can't do the job (udp is connectionless and need to capture packet) but I found nothing concrete how to accomplish that.

You need to use a packet capture library, such as libpcap, and manually keep track of the ips/ports of outgoing packets you see.