QtCreator and boost::asio::ip::udp::socket vs QUdpSocket
-
Hi everybody,
I am writting a program with QtCreator which is intended to receive UDP data
from the source sending about 20 MBps (on the local machine for the time being).
At the begininig of my project I used QUdpSocket object but I realized
that it was to slow and can NOT keep up to receive so many data.
Some online forums I was confirmed that it is true - QUdpSocket is slow
and I was advised to use boost::asio::ip::udp::socket. I connected it to QtCreator
and wrote code on the tutorial
http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tutdaytime6.html basis.
Hovever I have some problems.
The structure of Qt project is not adequate to the code from the tutorial.My code:
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);
oscilloscope::Oscilloscope w;
w.show();return a.exec();
}
@oscilloscope.h:
@class Oscilloscope
{
...
boost::asio::io_service io_serv;
UdpReceiver *port1;
...
}
@oscilloscope.cpp
@oscilloscope::oscilloscope()
{
...........
try
{
port1 = new UdpReceiver(io_serv);
io_serv.run(); // <- it is very important
}
catch(std::exception& e)
{
std::cerr << e.what() << "\n";
}
...........
}
@udpreceiver.h
@class UdpReceiver
{
udp::socket sockPort;
udp::endpoint remote_endpoint;
boost::array<char, 1> rec_buffer;public:
UdpReceiver(boost::asio::io_service & io_serv);private:
void StartReceive();
void handle_receive(const boost::system::error_code& error,
std::size_t bytes_transferred);
};
@udpreceiver.cpp
@UdpReceiver::UdpReceiver(boost::asio::io_service & io_serv):
sockPort(io_serv, udp::endpoint(udp::v4(), 7755))
{
StartReceive();
}void UdpReceiver::StartReceive()
{
sockPort.async_receive_from(
boost::asio::buffer(rec_buffer), remote_endpoint,
boost::bind(&UdpReceiver::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));}
void UdpReceiver::handle_receive(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
if (!error || error == boost::asio::error::message_size)
{
std::string err = error.message();
StartReceive();
}
else
{
std::string ss = error.message();
std::cerr << ss << " " << error.value() << "\n";StartReceive();
}
}
@Application enter to function handle_receive properly when receiving data
but the first condition is always FALSE.
The second condition (else) is performed and prints
error 234 - More data is available.What does it mean? Where did I make an error?
Could someone help me?
Is it true that QUdpSocket is such slow?Thanks for your help in advance.
Best regards.
Milosz -
[quote author="Milosz" date="1421653198"]Is it true that QUdpSocket is such slow?[/quote]Try it :) Write two different programs, where one uses QUdpSocket and the other uses boost::asio::ip::udp::socket. See if you notice any difference.