Skip to content
Snippets Groups Projects
Commit 1eeaae27 authored by Simon Oehrl's avatar Simon Oehrl
Browse files

chore: setup project

parents
No related branches found
No related tags found
No related merge requests found
cmake_minimum_required(VERSION 3.20)
project(network-speedtest)
set(CMAKE_CXX_STANDARD 20)
set(CXX_STANDARD_REQUIRED ON)
include(CPM.cmake)
CPMAddPackage("gh:OlivierLDff/asio.cmake@1.1.3")
add_executable(sender sender.cpp)
target_link_libraries(sender PRIVATE asio::asio)
add_executable(receiver receiver.cpp)
target_link_libraries(receiver PRIVATE asio::asio)
This diff is collapsed.
#include <asio.hpp>
#include <asio/buffer.hpp>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <optional>
using udp = asio::ip::udp;
using clk = std::chrono::steady_clock;
class Receiver {
public:
Receiver(std::uint16_t port)
: socket(context, udp::endpoint(udp::v4(), port)) {
receive();
}
void run() { context.run(); }
private:
asio::io_context context;
udp::socket socket;
std::array<std::byte, 1024> data;
std::optional<clk::time_point> first_receive;
std::uint64_t bytes_received = 0;
clk::time_point last_print;
void receive() {
socket.async_receive(asio::buffer(data), [this](std::error_code ec,
std::size_t bytes_recvd) {
if (ec) {
std::cerr << ec.category().name() << ": " << ec.message() << std::endl;
} else {
bytes_received += bytes_recvd;
if (!first_receive.has_value()) {
first_receive = last_print = clk::now();
} else {
const auto dt = clk::now() - *first_receive;
const auto seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(dt)
.count();
if (clk::now() - last_print > std::chrono::seconds(2)) {
std::cout << bytes_received * 8 / seconds / 1000.0 / 1000.0 << " Mbit/s" << std::endl;
last_print = clk::now();
}
}
this->receive();
}
});
}
};
int main(int argc, char *argv[]) {
Receiver r(std::stoi(argv[1]));
r.run();
}
#include <asio.hpp>
#include <asio/buffer.hpp>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <iostream>
using udp = asio::ip::udp;
using clk = std::chrono::steady_clock;
int main(int argc, char *argv[]) {
asio::io_context context;
udp::resolver resolver(context);
udp::endpoint endpoint =
*resolver.resolve(udp::v4(), argv[1], argv[2]).begin();
udp::socket s(context, udp::endpoint(udp::v4(), 0));
// udp::socket s(context, endpoint);
int packet_size = argc >= 4 ? std::stoi(argv[3]) : 1024;
double time_between_packets = argc >= 5 ? std::stod(argv[4]) : 100;
std::cout << "packet size: " << packet_size << " byte" << std::endl;
std::cout << "send interval: " << time_between_packets << " ms" << std::endl;
const auto dt = std::chrono::duration_cast<clk::duration>(
std::chrono::duration<double, std::milli>(time_between_packets));
std::array<std::byte, 1024> data;
auto last = clk::now();
while (true) {
auto now = clk::now();
if (now - last > dt) {
s.send_to(asio::buffer(data, packet_size), endpoint);
last += dt;
if (clk::now() - now > dt) {
std::putchar('.');
std::cout.flush();
last += dt;
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment