简体   繁体   English

绑定和连接的Boost.Asio数据报(UDP)套接字

[英]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: 首先,我要解释一下我想要实现的目标:

  • I hide the Boost.Asio sockets behind a very simple interface ( Pure Abstract Base Class ), so I have two wrapper classes that allow to access either a stream socket or a datagram socket. 我将Boost.Asio套接字隐藏在一个非常简单的接口( Pure Abstract Base Class )后面,因此我有两个包装器类,它们可以访问流套接字或数据报套接字。
  • I want to configure both the local endpoint and the remote endpoint before passing the Boost.Asio socket to the constructor of my wrapper class. 在将Boost.Asio套接字传递给包装类的构造函数之前,我想同时配置本地端点远程端点。
  • I want to use the 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_fromsocket.send_to成员函数。
  • The only way to use socket.send and socket.receive with a boost::asio::ip::udp::socket seems to connect the socket. 通过boost::asio::ip::udp::socket使用socket.sendsocket.receive的唯一方法似乎是连接套接字。

A UDP socket can both be bound and connected: UDP套接字可以绑定和连接:

The problem is, that even though I am able to 问题是,即使我能够

  1. Open the socket, 打开插座
  2. Set socket options, 设置套接字选项,
  3. Bind the socket, 绑定套接字,
  4. Connect the socket, 连接插座,

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. 如果不连接套接字,则可以通过绑定的本地端点接收数据,但是无法使用上述方法发送数据。

  1. So my central question is: Am I trying something that is impossible to achieve? 所以我的中心问题是:我是否正在尝试一些无法实现的事情?
  2. Can I only use bind or connect with one socket instance? 我只能使用bind 与一个套接字实例connect吗?
  3. If the answer to the two previous questions is no: What do I have to do, to be able to receive and send data via a bound and connected Boost.Asio UDP socket. 如果对前两个问题的回答是否定的:我必须做什么,才能通过绑定并连接的Boost.Asio UDP套接字接收和发送数据。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM