开发者

Sending Data To Socket is Causing an Exception

When I call the start_receive() method in the code belo开发者_Go百科w 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));

}


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()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜