winsock的socket函数中的协议参数有什么用?

发布于 2024-09-08 10:24:39 字数 581 浏览 3 评论 0原文

winsock 函数套接字期望第三个参数为协议,对于套接字类型 SOCK_STREAM,通常为 IPROTO_TCP;对于套接字类型 SOCK_DGRAM,通常为 IPROTO_UDP。当我传递 0 值作为协议参数时,TCP 和 UDP 按预期工作。

    SOCKET s = socket(AF_INET, SOCK_DGRAM, 0)

    // s is a valid socket

IPROTO_IP 协议参数值的含义是什么?如果它只是为了与 SOCK_RAW 一起使用,为什么会有这种冗余?

socket(AF_INET, SOCK_STREAM, IPROTO_TCP);
socket(AF_INET, SOCK_DGRAM, IPROTO_UDP);

协议参数实际上指定了什么?当我可以使用另一个值时,看起来它并不重要。

我想将 UDP 数据包(包括广播)从一台具有多个网卡的 PC 发送到特定的以太网段。虽然 IP 路由通常选择网卡(和源地址),但我想指定适配器并考虑原始套接字或任何其他方法来实现此目标。也许这个 IPPROTO_IP 在这种情况下可能会有所帮助。

The winsock function socket expects as third parameter the protocol what usually is IPROTO_TCP for socket type SOCK_STREAM and IPROTO_UDP for socket type SOCK_DGRAM. When I pass a 0 value as the protocol parameter, TCP and UDP work as expected.

    SOCKET s = socket(AF_INET, SOCK_DGRAM, 0)

    // s is a valid socket

What is the IPROTO_IP protocol parameter value meant to? If it's only intented to be used with SOCK_RAW, why is there this kind of redundancy?

socket(AF_INET, SOCK_STREAM, IPROTO_TCP);
socket(AF_INET, SOCK_DGRAM, IPROTO_UDP);

What actually does the protocol parameter specify? when I can just use another value, it looks like that it's unimportant.

I want to send UDP packets (including broadcasts) from a PC with more than one netword card to a specific ethernet segment. While the IP routing normally select the network card (and source address) I would like specify the adapter(s) and think about raw sockets or any other means to achieve this goal. Probably this IPPROTO_IP may help in this case.

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

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

发布评论

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

评论(1

2024-09-15 10:24:39

我认为套接字的文档(可以在这里找到: http://msdn.microsoft.com/en-us/library/ms740506(VS.85).aspx) 非常清楚该值的用途以及为什么如果您不关心则传递 0 就可以了。

您可能想要传递不同内容的情况是,如果您想为不寻常的连接类型设置套接字;例如蓝牙,或者如果您想创建 PGM 可靠的多播套接字等。

您的第二个问题与原始套接字或协议参数无关。您需要做的只是将套接字绑定到您要使用的本地接口的地址;因此,您不需要绑定到 INADDR_ANY 并让堆栈为您决定,而是告诉它要使用哪个接口。

I think the documentation for socket (which can be found here: http://msdn.microsoft.com/en-us/library/ms740506(VS.85).aspx) is pretty clear on what the value is for and why passing 0 is fine if you don't care.

A situation where you might want to pass something different is if you wanted to set up a socket for an unusual connection type; such as bluetooth, or if you wanted to create a PGM reliable multicast socket, etc.

Your second question is unrelated to raw sockets or the protocol parameters. What you need to do is simply bind your socket to the address of the local interface that you want to use; so rather than binding to INADDR_ANY and allowing the stack to decide for you, you tell it which interface to use.

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