Boost logo

Boost Users :

Subject: Re: [Boost-users] ASIO UDP Communication stops working when entering a thread
From: Ulf Samuelsson (boost-user_at_[hidden])
Date: 2013-09-25 16:30:25


On 2013-09-25 21:58, Ulf Samuelsson wrote:
> Have created a class to allow UDP communication using ASIO
>
Think I found the problem.

Added a method, to allow me to do "io_service.run()" on the endpoint in use,
and then it works.

void UdpClient::Run (void)
{
     io_service.run()
}

which is called by main:

endpoint.Run()

and then I get output.

Reading up a bit more, I realize I have to be careful,
if I try to use UDP on this port in more than one thread.

BR
Ulf

> // Header
> class UdpClient
> {
> public:
> UdpClient();
> void Open(string ipaddr, string port);
> void Send(string packet);
> void Close();
> private:
> bool init;
> string ipAddress;
> string Port;
> udp::endpoint Endpoint;
> udp::socket *pSocket;
> udp::socket *s;
> };
>
>
>
> // Part of code
> void UdpClient::Open(string ipaddr, string port)
> {
> ipAddress = ipaddr;
> Port = port;
>
> boost::asio::io_service io_service;
>
> udp::resolver resolver(io_service);
> udp::resolver::query query(udp::v4(), ipaddr,port);
> Endpoint = *resolver.resolve(query);
>
> pSocket = new udp::socket(io_service);
> pSocket->open(udp::v4());
> pSocket->send_to(boost::asio::buffer(first.c_str(), first.size()),
> Endpoint);
>
> std::cout << "Opening port [" + Port + "] on " + ipAddress + "\n";
> init = true;
> }
>
> void UdpClient::Send(string packet)
> {
> if (init) {
> pSocket->send_to(boost::asio::buffer(packet.c_str(),
> packet.size()), Endpoint);
> }
> }
>
>
> ---------------
> Another class implements more advanced communication.
>
> // Extracts
> extern UdpClient endpoint;
>
> void UdpDebug::WriteLine(string line)
> {
> line = "[" + Clock.Now() + "]" + line;
> endpoint.Send(line);
> };
>
> ------------------------
>
>
> In my main program i declare two global variables.
>
> UdpDebug DebugPort;
> UdpClient endpoint;
>
>
> The I init the UDP communication by calling
>
> endpoint.Open(IPAddress, UDPPort);
>
> after that I can write to the UDP enpoint by:
>
> DebugPort.WriteLine("Debug working!");
>
>
> Have declared a thread:
>
> boost::thread t1(Initialize);
>
> void Initialize()
> {
> DebugPort.WriteLine("Debug working in the thread");
> for (int i = 0; i < MAX; ++i) {
> wait(1);
> std::cout << "Thread 1: " << i << std::endl;
> }
> }
>
>
> When I do
>
> t1.join();
>
> The thread starts printing out "Thread1: <number>" on the terminal
> but no UDP output is received on the other end.
>
> Any clues why this won't work?
>
> Are starting two threads, but only one of them is attempting UDP
> communication.
>
> If I put a wait statement inside the main program, and attempt UDP
> communication
> after the threads have executed a few seconds, this will still work.
>
> Also tried creating a new instance of UdpClient inside the thread.
> Opened it, and tried to send data, to no avail.
>
> Any clues?
>
>
> BR
> Ulf Samuelsson
>
>
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net