Windows最低级别的套接字编程?
我对 winsock 的级别有多低有点困惑?我想在 Windows 上编写一个非常基本的客户端-服务器程序。我真的不想使用臃肿的 TCP 甚至 UDP,只是一些非常基本和低延迟的东西。 winsock 适合这个吗?或者winsock与windows网络功能相同,只是全部打包(并且可能更慢)?在本机 Windows 网络功能上使用 PInvoke 会更好吗?
I'm having a little bit of confusion regarding how low-level winsock is? I am wanting to write a VERY basic client-server program on windows. I don't really wish to use a bloated TCP or even UDP, just something extremely basic and low latency. Would winsock be ideal for this? Or is winsock the same as the windows network functions, just all packaged up (and possibly slower)? Would I be better just using PInvoke on the native windows networking functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Winsock、TCP、UDP 以及任何建立在这些之上的广受好评的网络库都将在性能方面具有可比性。
使用最容易完成工作的一种。
Winsock, TCP, UDP, and any well received networking library built on top of these are all going to be comparable performance wise.
Use whichever one is easiest to get your work done.
首先,可以编写全新的协议而无需实现网络驱动程序。为此,您有原始套接字。在桌面 Windows 上,它们非常有限(找到“限制”)。
这是可能的,但不推荐。在您完全确定需要更复杂(但不是更简单)的东西之前,不要重新发明轮子并在 UDP 和 TCP 之间进行选择。
要通过网络发送数据(与两台计算机之间的直接电缆连接相反),您需要 IP 协议。要将数据发送到正确的应用程序,您需要传输协议(UDP、TCP 等)。 UDP 几乎是最简单的一种,因为这是它的主要设计目标。 UDP 提供附加寻址(除了 IP 地址之外的端口号,用于将数据传送到正确的套接字)、数据包边界(“长度”字段)和可选的校验和。这就是全部,这也是最少的功能列表。接受它并通过 UDP 实现您需要的一切。
接下来,如果您需要确保您的数据包已交付,而不是在途中默默地丢弃在某个地方(可靠性),以正确的顺序交付,如果您想知道有人仍在另一端监听您的声音(连接性)并且由最好的专家实现的其他东西,使用适应这种实现的硬件,数十年来经过数百万人的测试,有大量文档,几乎在所有可能的平台上可用 - 使用 TCP。
First of all, it's possible to write completely new protocol w/o implementing network driver. For this you have raw sockets. On desktop Windows they are very limited (find "limitations").
It's possible, but not recommended. Don't reinvent the wheel and choose between UDP and TCP until you're completely sure you need something more sophisticated (but not simpler).
To send data over network (as opposite to direct cable link between two computers) you need IP protocol. To dispatch your data to right application you need transport protocol (UDP, TCP and others). UDP is almost the simplest possible one because that's was its main design goal. UDP provides additional addressing (port number in addition to IP address to deliver your data to right socket), packet boundaries ("length" field) and optional checksum. That's all and that's the minimum feature list. Take it and implement everything you need over UDP.
Next, if you need to be sure that your packets are delivered instead of silently dropped somewhere on the way (reliability), delivered in right order, if you'd like to know that somebody still listen to you on opposite end (connectivity) and other things implemented by best specialists, with hardware adapted to this implementation, tested by millions during decades, with lots of documentation, available on almost all possible platforms - use TCP.