如何查找分配给 UDP 客户端的端口号(在 .net/C# 中)?

发布于 2024-08-02 12:04:36 字数 277 浏览 5 评论 0原文

如果我使用创建套接字

var socket = new UdpClient(0,AddressFamily.InterNetwork);

然后如何找到套接字的端口?

我可能很愚蠢,但我在 MSDN/Google 上运气不佳(可能是因为现在是星期五 4:42,阳光明媚)。

背景:

我想要做的是找到一个开放的端口,然后报告给另一个进程在该端口上将消息转发给我。可能有多个客户端,所以我不想使用固定端口。

谢谢。

If I create a socket using

var socket = new UdpClient(0,AddressFamily.InterNetwork);

How do I then find the port of the socket?

I'm probably being daft, but I'm not having luck in MSDN/Google (probably because it is 4:42 on a Friday and the sun is shining).

Background:

What I want to do is find an open port, and then report to another process to forward messages to me on that port. There may be multiple clients, so I don't want to used a fixed port.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2024-08-09 12:04:36

UdpClient 是 Socket 类的包装器,它通过 LocalEndPoint 属性公开其绑定到的端点。由于您使用的是 UDP/IP 客户端,因此它是一个具有所需端口属性的 IPEndPoint:

int port = ((IPEndPoint)socket.Client.LocalEndPoint).Port;

UdpClient is a wrapper around the Socket class which exposes the endpoint it's bound to through the LocalEndPoint property. Since you're using an UDP/IP client it's an IPEndPoint which has the desired Port property:

int port = ((IPEndPoint)socket.Client.LocalEndPoint).Port;
笛声青案梦长安 2024-08-09 12:04:36

对于那些需要使用 RAW 套接字的人(像我一样),这里是解决方法。

目标:

  1. 在任意端口上创建 RAW UDP 套接字
  2. ,了解系统选择的端口。

预期:(socket.LocalEndPoint as IPEndPoint).Port

问题

  • ,而 DGRAM UDP 套接字知道其 (socket.LocalEndPoint as IPEndPoint).Port
  • RAW UDP 套接字始终返回零

解决方案:

  1. 创建一个普通的 DGRAM UDP 套接字
  2. 绑定该套接字
  3. 找出其端口
  4. 关闭普通套接字
  5. 创建原始UDP套接字

注意:

  • 使用修改后的local IPEndPoint变量来了解端口,因为套接字将始终报告零。

代码:

public Socket CreateBoundRawUdpSocket(ref IPEndPoint local)
{
    if (0 == local.port)
    {
        Socket wasted = new Socket(local.AddressFamily,
                                   SocketType.Dgram,
                                   ProtocolType.Udp);
        wasted.Bind(local);
        local.Port = (wasted.LocalEndPoint as IPEndPoint).Port;
        wasted.Close();
    }
    Socket goal = new Socket(local.AddressFamily,
                             SocketType.Raw,
                             ProtocolType.Udp);
    goal.Bind(local);
    return goal;
}

For those (like me) who need to use a RAW socket, here is the workaround.

goal:

  1. to create a RAW UDP socket on an arbitrary Port
  2. learn what port the system chose.

expected: (socket.LocalEndPoint as IPEndPoint).Port

problems

  • whereas a DGRAM UDP Socket knows its (socket.LocalEndPoint as IPEndPoint).Port
  • a RAW UDP Socket always returns zero

solution:

  1. create a normal DGRAM UDP socket
  2. bind that socket
  3. find out its Port
  4. close that normal socket
  5. create the RAW UDP socket

CAVEAT:

  • Use the modified local IPEndPoint variable to know the port, as the socket will always report zero.

code:

public Socket CreateBoundRawUdpSocket(ref IPEndPoint local)
{
    if (0 == local.port)
    {
        Socket wasted = new Socket(local.AddressFamily,
                                   SocketType.Dgram,
                                   ProtocolType.Udp);
        wasted.Bind(local);
        local.Port = (wasted.LocalEndPoint as IPEndPoint).Port;
        wasted.Close();
    }
    Socket goal = new Socket(local.AddressFamily,
                             SocketType.Raw,
                             ProtocolType.Udp);
    goal.Bind(local);
    return goal;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文