Shows how to use xtd::net::sockets::socket class.
#include <xtd/xtd>
using namespace std;
using namespace std::literals;
auto main()->int {
auto terminate_app = false;
thread server([&] {
socket server_socket(address_family::inter_network, socket_type::dgram, protocol_type::udp);
while (!terminate_app) {
vector<unsigned char> buffer(256);
size_t number_of_byte_received = server_socket.
receive_from(buffer, incoming_end_point);
if (!(number_of_byte_received == 1 && buffer[0] == 0xFF))
console::write_line(
ustring(buffer.begin(), buffer.begin() + number_of_byte_received));
}
});
thread client([&] {
socket client_socket(address_family::inter_network, socket_type::dgram, protocol_type::udp);
auto counter = 1;
while (!terminate_app) {
auto str = ustring::format("counter={}", counter++);
client_socket.
send_to(vector<unsigned char>(str.begin(), str.end()),
ip_end_point(ip_address::loopback, 9400));
this_thread::sleep_for(50ms);
}
});
console::read_key();
terminate_app = true;
server.join();
client.join();
}