向套接字发送数据导致异常

发布于 2024-11-25 07:37:52 字数 1236 浏览 0 评论 0原文

当我在下面的代码中调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-12-02 07:37:52

我不确定是什么原因导致了这个问题

抛出 system_error 异常是因为您的 send() 方法有 选择使用会引发异常不处理。处理 trycatch 块内的异常,或者使用 重载不会抛出。 send 出现任何错误情况都有多种原因,请参阅文档以获取更多信息。

任何帮助都会很棒

我最好的猜测是您正在未连接的 UDP 套接字上调用 send() 方法。也许您打算调用 send_to()< /a> 因为您使用 receive_from() 而不是 receive() 接收数据?

I'm not sure what's causing the issue

The system_error exception is being thrown because the send() method you have chosen to use throws an exception that you are not handling. Either handle the exception inside a try and catch block, or use one of the overloads that do not throw. There's a variety of reasons for any of the error conditions from send, consult the documentation for more information.

any help would be great

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 using receive_from() and not receive()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文