[英]Boost.Asio datagram (UDP) socket that is both bound and connected
I have problems understanding the concept behind Boost.Asio's (using v1.49.0) boost::asio::ip::udp::socket
sockets. 我在理解Boost.Asio(使用v1.49.0)
boost::asio::ip::udp::socket
套接字背后的概念时遇到了问题。
First I am gonna to to explain what I want to achieve: 首先,我要解释一下我想要实现的目标:
socket.receive
(alternatively boost::asio::read
) and socket.send
(alternatively boost::asio::write
) member functions instead of the socket.receive_from
and socket.send_to
member functions. socket.receive
(或者boost::asio::read
)和socket.send
(或者boost::asio::write
)成员函数,而不是socket.receive_from
和socket.send_to
成员函数。 socket.send
and socket.receive
with a boost::asio::ip::udp::socket
seems to connect the socket. boost::asio::ip::udp::socket
使用socket.send
和socket.receive
的唯一方法似乎是连接套接字。 A UDP socket can both be bound and connected: UDP套接字可以绑定和连接:
socket.bind
member function. socket.bind
成员函数实现的。 socket.connect
member function. socket.connect
成员函数实现连接。 The problem is, that even though I am able to 问题是,即使我能够
and to be able to send data via the socket, I can not receive data from the socket. 并且为了能够通过套接字发送数据,我无法从套接字接收数据。 If I don't connect the socket, I can receive data via the bound local endpoint, but I am unable to send data with the approached described.
如果不连接套接字,则可以通过绑定的本地端点接收数据,但是无法使用上述方法发送数据。
bind
or connect
with one socket instance? bind
或与一个套接字实例connect
吗? I know that UDP is in fact connectionless, therefore the text uses Boost.Asio terminology. 我知道UDP实际上是无连接的,因此文本使用Boost.Asio术语。 I have also read connect on "connection less" boost::asio::ip::udp::socket which seems to indicate that it is impossible what I am trying.
我还阅读了“少连接” boost :: asio :: ip :: udp :: socket上的connect,这似乎表明我尝试的是不可能的。
You are missing one point from man page of connect
: 您在
connect
手册页中遗漏了一点:
If the socket sockfd is of type SOCK_DGRAM, then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received .
如果套接字sockfd的类型为SOCK_DGRAM,则addr是默认情况下向其发送数据报的地址,并且是唯一从其接收数据报的地址 。
This means, that if you want to connect
the socket, then it will be able to receive datagrams only from remote endpoint (the connected one), ie peer will have to bind own socket before sending datagram to your socket waiting for data. 这意味着,如果您想
connect
套接字,那么它将只能从远程端点(已连接的端点)接收数据报,即对等方将必须绑定自己的套接字,然后才能将数据报发送到套接字以等待数据。
If you need to received data from more than one peer, you can connect udp socket to "any" address (ie 0.0.0.0 - udp::v4()) and some port. 如果需要从多个对等方接收数据,则可以将udp套接字连接到“任意”地址(即0.0.0.0-udp :: v4())和某个端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.