udpserver.cpp




udpserver.cpp

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>

int main(int argc, char *argv[])
{
boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket(io_service);
boost::asio::ip::udp::endpoint remote_endpoint;
bool broadcast = true;

socket.open(boost::asio::ip::udp::v4());

// TO ENABLE BROADCAST
if(broadcast)
  {
  boost::asio::socket_base::broadcast option(true);
  socket.set_option(option);
  }
   
remote_endpoint = boost::asio::ip::udp::endpoint(
    boost::asio::ip::address::from_string(argv[1]),  boost::lexical_cast<int>(argv[2]));
    
boost::system::error_code ignored_error;

std::cout << "Send to " << remote_endpoint << std::endl;

socket.send_to(boost::asio::buffer("message", 7), remote_endpoint, 0, ignored_error);

return 0;
}

Read more here: Source link