Code Network Collection modifies a small problem with a compilation
Client
Client.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <boost/asio.hpp>int main () {namespace IP = Boost::asio:: Ip;boost::asio::io_service io_service;//Client binds to any address in port 8888 (the same port on which//broadcast data is sent from server). Ip::udp::socket socket (Io_service,ip::udp::endpoint (IP::UDP::V4 (), 8888)); Ip::udp::endpoint sender_endpoint;//Receive Data.//boost::array<char, 4> buffer;char buf[500] = {0};std::size_t bytes_transferred =socket.receive_from (Boost::asio::buffer (BUF), sender_endpoint) std::cout << "got" << bytes_transferred << "bytes" << buf <<std::endl;}
Service side
Server.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <boost/asio.hpp>int main () {namespace IP = Boost::asio::ip;boost::asio::io_ Service io_service;//Server binds to any address and any Port.ip::udp::socket sockets (Io_service,ip::udp::endpoint (IP:: UDP::V4 (), 0)); Socket.set_option (Boost::asio::socket_base::broadcast (True));//broadcast'll go to Port 8888.ip::udp :: Endpoint Broadcast_endpoint (Ip::address_v4::broadcast (), 8888);//broadcast Data.//boost::array<char, 4> buffer;char* buf = "Test Code"; socket.send_to (Boost::asio::buffer (Buf,strlen (BUF) +1), broadcast_endpoint);}
ASIO broadcast code example