向套接字发送数据导致异常
当我在下面的代码中调用 start_receive() 方法而不调用 _outSocket.send() 时,该方法从套接字接收数据没有问题,但是当我尝试在另一个套接字上发送数据时,会引发 boost 异常。我不确定是什么导致了这个问题,所以任何帮助都会很棒。
#include "dataproxy.h"
#include <boost/bind.hpp>
#include <cstdlib>
using namespace boost::asio;
DataProxy::DataProxy(boost::asio::io_service& ioserv)
: _inSocket(ioserv, ip::udp::endpoint(ip::udp::v4(), 5004))
,_outSocket(ioserv, ip::udp::endpoint(ip::udp::v4(), 5005))
{
}
DataProxy::~DataProxy()
{
}
void DataProxy::start(){
QThread::start();
}
void DataProxy::run()
{
while(true)
{
start_receive();
}
}
void DataProxy::start_receive()
{
/*_inSocket.async_receive_from(
boost::asio::buffer(_inBuffer), _videoEndpoint,
boost::bind(&DataProxy::handleIncomingData, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
*/
size_t reply_length = _inSocket.receive_from(
boost::asio::buffer(_inBuffer), _dataSourceEndpoint);
std::cout << "Received " << reply_length << " bytes" << std::endl;
//This is the line that causes the exception
size_t send_length = _outSocket.send(boost::asio::buffer(_inBuffer));
}
When I call the start_receive() method in the code below without the _outSocket.send() call, the method receives data from the socket with no issues, but when I try to send the data out on another socket a boost exception is thrown. I'm not sure what's causing the issue, so any help would be great.
#include "dataproxy.h"
#include <boost/bind.hpp>
#include <cstdlib>
using namespace boost::asio;
DataProxy::DataProxy(boost::asio::io_service& ioserv)
: _inSocket(ioserv, ip::udp::endpoint(ip::udp::v4(), 5004))
,_outSocket(ioserv, ip::udp::endpoint(ip::udp::v4(), 5005))
{
}
DataProxy::~DataProxy()
{
}
void DataProxy::start(){
QThread::start();
}
void DataProxy::run()
{
while(true)
{
start_receive();
}
}
void DataProxy::start_receive()
{
/*_inSocket.async_receive_from(
boost::asio::buffer(_inBuffer), _videoEndpoint,
boost::bind(&DataProxy::handleIncomingData, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
*/
size_t reply_length = _inSocket.receive_from(
boost::asio::buffer(_inBuffer), _dataSourceEndpoint);
std::cout << "Received " << reply_length << " bytes" << std::endl;
//This is the line that causes the exception
size_t send_length = _outSocket.send(boost::asio::buffer(_inBuffer));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抛出
system_error
异常是因为您的send()
方法有 选择使用会引发异常不处理。处理try
和catch
块内的异常,或者使用 重载不会抛出。send
出现任何错误情况都有多种原因,请参阅文档以获取更多信息。我最好的猜测是您正在未连接的 UDP 套接字上调用
send()
方法。也许您打算调用 send_to()< /a> 因为您使用receive_from()
而不是receive()
接收数据?The
system_error
exception is being thrown because thesend()
method you have chosen to use throws an exception that you are not handling. Either handle the exception inside atry
andcatch
block, or use one of the overloads that do not throw. There's a variety of reasons for any of the error conditions fromsend
, consult the documentation for more information.My best guess is that you are invoking the
send()
method on an unconnected UDP socket. Perhaps you intended to invoke send_to() since you received the data usingreceive_from()
and notreceive()
?