xtd 0.2.0
Loading...
Searching...
No Matches
udp_client_ip_v4.cpp

Shows how to use xtd::net::sockets::udp_client class.

#include <xtd/io/stream_reader>
#include <xtd/net/sockets/udp_client>
#include <xtd/net/ip_end_point>
#include <xtd/threading/thread>
#include <xtd/console>
using namespace xtd;
using namespace xtd::net;
using namespace xtd::net::sockets;
using namespace xtd::threading;
auto main()->int {
auto terminate_app = false;
auto server = thread {[&] {
auto udp = udp_client {ip_end_point {ip_address::any, 9400}};
while (!terminate_app) {
ip_end_point incoming_end_point;
auto buffer = udp.receive(incoming_end_point);
if (buffer.size() && buffer[0] != 0xFF)
console::write_line(ustring(buffer.begin(), buffer.end()));
}
}};
auto client = thread {[&] {
auto udp = udp_client {};
auto counter = 0;
while (!terminate_app) {
auto str = ustring::format("counter={}", ++counter);
udp.send(std::vector<unsigned char>(str.begin(), str.end()), str.size(), ip_end_point(ip_address::loopback, 9400));
thread::sleep(50_ms);
}
udp.send(std::vector<unsigned char> {0xFF}, 1, ip_end_point(ip_address::loopback, 9400));
}};
server.start();
client.start();
console::read_key();
terminate_app = true;
server.join();
client.join();
}
// This code produces the following output:
//
// counter=1
// counter=2
// counter=3
// counter=4
// counter=5
// ...
Represents a network endpoint as an IP address and a port number.
Definition ip_end_point.h:21
Provides User Datagram Protocol (UDP) network services.
Definition udp_client.h:43
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:41
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
The xtd::net::sockets namespace provides a managed implementation of the Berkeley Sockets interface f...
Definition address_family.h:16
The xtd::net namespace provides a simple programming interface for many of the protocols used on netw...
Definition cookie_exception.h:10
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10