#ifndef BOOST_ASIO_H#define BOOST_ASIO_H#include <QObject>#include <QTimer>#include <QtNetwork/QUdpSocket>#include <QString>#include <QDataStream>#include <QDebug>#include <QByteArray>#include <iostream>#include <QThread>#include <QtConcurrent/QtConcurrentRun>#include <boost/ref.hpp>#include <boost/asio.hpp>#include <boost/bind.hpp>#include <boost/thread.hpp>#include <boost/format.hpp>#include <boost/optional.hpp>#include <boost/shared_ptr.hpp>#include <boost/make_shared.hpp>#include <boost/utility/in_place_factory.hpp>using namespace boost::asio;using namespace std;//void read_handler(// const boost::system::error_code& ec, std::size_t bytes_transferred);class boost_asio : public QObject{ Q_OBJECTpublic: boost_asio(QObject *parent); void Init(string address, int port); ip::address addr; int portInput; io_service service; ip::udp::socket *udpSocket; QByteArray arr; boost::asio::ip::udp::endpoint sender_ep; // void read_handler( // const boost::system::error_code& ec, // std::size_t bytes_transferred); // void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred); void read_handler(char *ptr, const boost::system::error_code& ec, std::size_t bytes_transferred); void newThread();private:public slots:};#endif // BOOST_ASIO_H
#include "boost_asio.h"boost_asio::boost_asio(QObject *parent) : QObject(parent){ arr.clear();}void boost_asio::Init(string address,int port){ addr = ip::address::from_string(address); udpSocket = new ip::udp::socket(service); sender_ep = ip::udp::endpoint(addr,port); ip::udp::endpoint res(addr,4810); qDebug()<< sender_ep.port();/*addr,port);*/ qDebug() << QString::fromStdString(sender_ep.address().to_string()); // ip::udp::socket sock(service); // udpSocket = sock; udpSocket->open(sender_ep.protocol()); udpSocket->bind(sender_ep); // qDebug() << udpSocket->send_to(buffer("testing\n"), ep); // udpSocket->bind(res); // udpSocket->async_connect(sender_ep, boost::bind(&boost_asio::read_handler, this // , boost::asio::placeholders::error // , boost::asio::placeholders::bytes_transferred)); qDebug() << udpSocket->is_open(); udpSocket->set_option(boost::asio::ip::udp::socket::reuse_address(true)); // udpSocket->async_receive_from(buffer(buff,512),sender_ep, ReadHendler); // char * buf = new char[1400]; // udpSocket->async_receive(buffer(buf,1400), boost::bind(&boost_asio::read_handler, this, buf // , boost::asio::placeholders::error // , boost::asio::placeholders::bytes_transferred())); // QtConcurrent::run(QThreadPool::globalInstance(),boost_asio::newThread,); // arr.push_back(buf); // qDebug()<<arr.size(); // ip::tcp::socket::send_buffer_size sbs; // udpSocket->get_option(sbs); // qDebug() << sbs.value(); char * buf = new char[1400]; qDebug()<< sizeof(buf); udpSocket->async_receive(buffer(buf,1400), boost::bind(&boost_asio::read_handler, this, buf , boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred())); QFuture<void> future = QtConcurrent::run(this,&boost_asio::newThread); // service.run();}void boost_asio::read_handler(char *ptr, const boost::system::error_code& ec, std::size_t bytes_transferred){ arr.push_back(ptr); qDebug()<<arr.size(); char * buf = new char[1400]; udpSocket->async_receive(buffer(buf,1400), boost::bind(&boost_asio::read_handler, this, buf , boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred));}void boost_asio::newThread(){ service.run();}
void boost_asio::read_handler(char *ptr, const boost::system::error_code& ec, std::size_t bytes_transferred){ arr.push_back(ptr); qDebug()<<arr.size(); char * buf = new char[1400]; udpSocket->async_receive(buffer(buf,1400), boost::bind(&boost_asio::read_handler, this, buf , boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred));}