Skip to content

Instantly share code, notes, and snippets.

@o-zombi-o
Created November 28, 2016 00:38
Show Gist options
  • Save o-zombi-o/6474ca62dcb171fcd760077fd56b1d48 to your computer and use it in GitHub Desktop.
Save o-zombi-o/6474ca62dcb171fcd760077fd56b1d48 to your computer and use it in GitHub Desktop.
#include <thread>
#include <chrono>
#include <vector>
#include <signal.h>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
namespace
{
bool keepGoing = true;
void shutdown(int) { keepGoing = false; }
std::size_t megs10 = 1048576; // 10 megs
}
int main(int, char **)
{
signal(SIGINT, shutdown);
boost::asio::io_service io;
boost::asio::io_service::work work(io);
boost::thread t1(boost::bind(&boost::asio::io_service::run, &io));
boost::thread t2(boost::bind(&boost::asio::io_service::run, &io));
boost::thread t3(boost::bind(&boost::asio::io_service::run, &io));
boost::thread t4(boost::bind(&boost::asio::io_service::run, &io));
boost::asio::ip::tcp::socket socket(io);
auto endpoint = boost::asio::ip::tcp::resolver(io).resolve({
"127.0.0.1", "1234" });
boost::asio::connect(socket, endpoint);
std::vector<unsigned char> buffer(megs10, 0);
std::chrono::time_point<std::chrono::system_clock> last =
std::chrono::system_clock::now();
std::chrono::duration<double> delta = std::chrono::seconds(0);
std::size_t bytesSent = 0;
while (keepGoing)
{
// blocks during send
boost::asio::write(socket, boost::asio::buffer(buffer));
// accumulate bytes sent
bytesSent += buffer.size();
// accumulate time spent sending
delta += std::chrono::system_clock::now() - last;
last = std::chrono::system_clock::now();
// print information periodically
if (delta.count() >= 5.0)
{
std::printf("Gbits/sec: %f\n", 8 * bytesSent / 1.0e9 / delta.count());
// reset accumulators
bytesSent = 0;
delta = std::chrono::seconds(0);
}
}
io.stop();
t1.join();
t2.join();
t3.join();
t4.join();
std::printf("client: goodbyte\n");
}
#include <thread>
#include <chrono>
#include <vector>
#include <signal.h>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
namespace
{
bool keepGoing = true;
void shutdown(int)
{
keepGoing = false;
}
void justReceive(boost::system::error_code ec, std::size_t bytesReceived,
boost::asio::ip::tcp::socket &socket, std::vector<unsigned char> &buffer)
{
socket.async_receive(
boost::asio::buffer(buffer, 2048),
0,
boost::bind(justReceive, _1, _2, boost::ref(socket),
boost::ref(buffer)));
}
std::size_t megs10 = 1048576; // 10 megs
}
int main(int, char **)
{
signal(SIGINT, shutdown);
boost::asio::io_service io;
boost::asio::io_service::work work(io);
boost::thread t1(boost::bind(&boost::asio::io_service::run, &io));
boost::thread t2(boost::bind(&boost::asio::io_service::run, &io));
boost::thread t3(boost::bind(&boost::asio::io_service::run, &io));
boost::thread t4(boost::bind(&boost::asio::io_service::run, &io));
boost::asio::ip::tcp::acceptor acceptor(io,
boost::asio::ip::tcp::endpoint(
boost::asio::ip::address::from_string("127.0.0.1"), 1234));
boost::asio::ip::tcp::socket socket(io);
// accept 1 client
std::vector<unsigned char> buffer(megs10);
acceptor.async_accept(socket, [&socket, &buffer](boost::system::error_code ec)
{
socket.async_receive(
boost::asio::buffer(buffer),
0,
boost::bind(justReceive, _1, _2, boost::ref(socket),
boost::ref(buffer)));
});
while (keepGoing)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
io.stop();
t1.join();
t2.join();
t3.join();
t4.join();
std::printf("server: goodbye\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment