Last active
December 21, 2015 19:58
-
-
Save nickveys/6357523 to your computer and use it in GitHub Desktop.
Code for SO question: http://stackoverflow.com/questions/18474737/boostasio-udp-send-receive-in-the-same-process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/asio.hpp> | |
#include <boost/assign.hpp> | |
#include <boost/bind.hpp> | |
#include <boost/filesystem.hpp> | |
#include <boost/smart_ptr.hpp> | |
#include <boost/thread.hpp> | |
#include <algorithm> | |
#include <cstdio> | |
#include <iterator> | |
#include <numeric> | |
#include <vector> | |
#include <stdint.h> | |
namespace ba = boost::asio; | |
#ifdef WIN32 | |
# include "windows.h" | |
# define wait(milliseconds) Sleep(milliseconds) | |
#else | |
# include <unistd.h> | |
# define wait(milliseconds) usleep(milliseconds * 1000) | |
#endif | |
void receive(const boost::system::error_code& error, size_t bytes_recvd); | |
void send(ba::ip::udp::socket& socket, char val); | |
/* buffer for data reception */ | |
#define BUF_SZ 8192 | |
uint8_t buffer[8192]; | |
#define PACKET_SIZE ((2 * 1024) - 42) | |
/* storage for chunks of received data */ | |
std::vector< std::vector<uint8_t> > buffers; | |
ba::ip::udp::endpoint endpoint; | |
boost::scoped_ptr< ba::ip::udp::socket > rxsock; | |
void go(); | |
int main() | |
{ | |
go(); | |
go(); | |
go(); | |
go(); | |
go(); | |
go(); | |
go(); | |
} | |
void go() | |
{ | |
ba::ip::address address(ba::ip::address::from_string("127.0.0.1")); | |
endpoint = ba::ip::udp::endpoint(address, 54321); | |
ba::io_service service; | |
ba::ip::udp::socket txsock(service); | |
txsock.open(ba::ip::udp::v4()); | |
txsock.set_option(ba::ip::udp::socket::reuse_address(true)); | |
txsock.set_option(ba::socket_base::broadcast(true)); | |
rxsock.reset(new ba::ip::udp::socket(service)); | |
rxsock->open(ba::ip::udp::v4()); | |
rxsock->bind(endpoint); | |
rxsock->async_receive_from(ba::buffer(buffer, BUF_SZ), endpoint, | |
boost::bind(receive, ba::placeholders::error, ba::placeholders::bytes_transferred)); | |
boost::thread thread(boost::bind(&ba::io_service::run, &service)); | |
static char i = 0; | |
send(txsock, i++); | |
send(txsock, i++); | |
send(txsock, i++); | |
send(txsock, i++); | |
send(txsock, i++); | |
send(txsock, i++); | |
send(txsock, i++); | |
send(txsock, i++); | |
wait(500); | |
printf("received %lu packet(s) total\n", buffers.size()); | |
/* clean up */ | |
rxsock->close(); | |
service.stop(); | |
thread.join(); | |
rxsock.reset(); | |
txsock.close(); | |
buffers.clear(); | |
} | |
void send(ba::ip::udp::socket& socket, char val) | |
{ | |
ba::streambuf b; | |
std::ostream stream(&b); | |
std::vector<char> data(576, val); | |
stream.write(&(data[0]), data.size()); | |
std::size_t sz = socket.send_to(b.data(), endpoint); | |
printf("send(%lu)\n", sz); | |
boost::this_thread::yield(); | |
} | |
void receive(const boost::system::error_code& error, size_t bytes_recvd) | |
{ | |
/* single error occurs when service is stopped, ignore */ | |
if (error) | |
{ | |
printf("receive error: %s\n", error.message().c_str()); | |
return; | |
} | |
/* store relevant data */ | |
std::vector<uint8_t> recv(buffer, buffer + bytes_recvd); | |
buffers.push_back(recv); | |
printf("receive(%lu) index: %d\n", bytes_recvd, (int) recv.front()); | |
/* get back in line to listen */ | |
rxsock->async_receive_from(ba::buffer(buffer, BUF_SZ), endpoint, | |
boost::bind(receive, ba::placeholders::error, ba::placeholders::bytes_transferred)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ asio-test.cpp -o asio-test -g -lboost_system-mt -lboost_thread-mt -pthread |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment