在“少连接"上连接boost::asio::ip::udp::socketasio、boost、quot、socket

2023-09-07 12:56:45 作者:错误。

我最近通过浏览网络了解了 UDP 套接字,所有解释它的页面都提到 UDP 套接字连接较少".如果我理解正确,这意味着一个在两个套接字之间没有连接",而是在不知道另一端是否正在侦听的情况下将数据报包发送到指定的端点.

I've been learning about UDP socket lately by browsing the net and all the pages that were explaining it were mentioning that UDP sockets are "connection less". This, if I understand it correctly means that one does not have a "connection" between two sockets but instead shoots datagram packets to specified endpoints without knowing whether the other end is listening.

然后我开始阅读 boost::asio::ip::udp::socket docs 并发现它提到的 API 如下:

Then I go and start reading the boost::asio::ip::udp::socket docs and find that it mentions API like:

async_connect:启动异步connect.async_receive:在连接的套接字上启动异步接收.async_send:在连接的套接字上启动异步发送. async_connect: Start an asynchronous connect. async_receive: Start an asynchronous receive on a connected socket. async_send: Start an asynchronous send on a connected socket.

现在这对于新手来说有点混乱.我可以找到 3 个可能导致我困惑的原因(按相似度排序:))

Now this is a bit confusing for a novice. I can find 3 possible causes for my confusion (in order of likehood :) )

我错过了什么asio 实现在幕后做一些事情来虚拟化连接.文档有误

当您打开 basic_datagram_socket::async_connect 中的示例是实例化 TCP 套接字(而不是 UDP 套接字).

There is also a slight glitch in the docs, when you open the page for basic_datagram_socket::async_connect the example in there is instantiating TCP sockets (instead of UDP ones).

有人请教我吗?

推荐答案

Single UNIX 规范对 connect 适用于无连接套接字:

The Single UNIX specification has a better explanation of what connect does for connection-less sockets:

如果启动套接字不是连接模式,则 connect() 设置套接字的对等地址,但不建立连接.对于 SOCK_DGRAM 套接字,对等地址识别所有数据报在后续 send() 调用中的发送位置,并限制远程发送方以进行后续 recv() 调用.

If the initiating socket is not connection-mode, then connect() sets the socket's peer address, but no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send() calls, and limits the remote sender for subsequent recv() calls.