如何从 UDP 套接字获取 IP 信息 (Windows C++)?

发布于 2024-11-19 11:51:59 字数 728 浏览 0 评论 0原文

我试图找到我的UDP套接字绑定到的IP地址(假设我不想使用其他方法来查找计算机的IP地址)。这怎么能做到呢?下面的代码适用于端口号,但始终返回 0.0.0.0 作为地址:

struct sockaddr_in sin;
int addrlen = sizeof(sin);
if(getsockname(clientSock, (struct sockaddr *)&sin, &addrlen) == 0 &&
    sin.sin_family == AF_INET &&
    addrlen == sizeof(sin)){
    printf("RETURNING ADDR: %s: len = %d\n", inet_ntoa(sin.sin_addr),
        strlen(inet_ntoa(sin.sin_addr)));
}

使用以下代码绑定套接字:

sockaddr_in local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;//inet_addr("127.0.0.1");
local.sin_port = 0; //assign given port
result = bind(clientSock, (sockaddr*)&local, sizeof(local));

感谢您提供的所有帮助。我很感谢您的时间!

I tried to find the IP address that my UDP socket is bound to (assuming I don't want to use another method to find the computer's IP address). How can this be done? The code below works for the PORT number, but always returns 0.0.0.0 for the address:

struct sockaddr_in sin;
int addrlen = sizeof(sin);
if(getsockname(clientSock, (struct sockaddr *)&sin, &addrlen) == 0 &&
    sin.sin_family == AF_INET &&
    addrlen == sizeof(sin)){
    printf("RETURNING ADDR: %s: len = %d\n", inet_ntoa(sin.sin_addr),
        strlen(inet_ntoa(sin.sin_addr)));
}

The socket was bound using the following code:

sockaddr_in local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;//inet_addr("127.0.0.1");
local.sin_port = 0; //assign given port
result = bind(clientSock, (sockaddr*)&local, sizeof(local));

Thank you for any and all help. I appreciate your time!

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

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

发布评论

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

评论(1

带刺的爱情 2024-11-26 11:52:00

0.0.0.0INADDR_ANY,这意味着套接字绑定到主机上的所有本地地址,而不仅仅是一个地址。您正在请求一个地址,但您并未绑定到一个地址,因此 getsockname() 无法报告特定地址。

如果您希望 getsockname() 报告特定地址,则必须 bind() 到该特定地址。因此,请使用 GetAdaptersAddresses< /a> 枚举本地主机上的所有接口,并为每个地址bind()一个单独的套接字,而不是在单个地址上绑定INADDR_ANY 插座。

否则,您可以 bind() 将单个套接字绑定到 INADDR_ANY,然后使用 WSARecvMsg() (而不是 recv()recvfrom()WSARecvFrom()) 读取传入数据包。如果您使用 setsockopt()

0.0.0.0 is INADDR_ANY, meaning the socket is bound to all local addresses on the host, not just one address. You are asking for one address, but you are not bound to one address, so getsockname() cannot report a specific address.

If you want getsockname() to report a specific address, you have to bind() to that specific address. So use GetAdaptersAddresses to enumerate all interfaces on the local host and bind() a separate socket to each address, instead of binding INADDR_ANY on a single socket.

Otherwise, you can bind() a single socket to INADDR_ANY, and then use WSARecvMsg() (instead of recv(), recvfrom(), or WSARecvFrom()) to read the incoming packets. WSARecvMsg() can report details about each packet's arrival interface and destination address, if you enable the appropriate options with setsockopt().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文