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

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

#include <xtd/collections/generic/list>
#include <xtd/net/sockets/socket>
#include <xtd/net/ip_end_point>
#include <xtd/threading/thread>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::net;
using namespace xtd::net::sockets;
using namespace xtd::threading;
auto main() -> int {
auto terminate_app = false;
auto server = thread {[&] {
auto server_socket = socket {address_family::inter_network, socket_type::dgram, protocol_type::udp};
server_socket.bind(ip_end_point {ip_address::any, 9400});
while (!terminate_app) {
auto buffer = list<unsigned char>(256);
auto incoming_end_point = ip_end_point {};
auto number_of_byte_received = server_socket.receive_from(buffer, incoming_end_point);
if (!(number_of_byte_received == 1 && buffer[0] == 0xFF))
console::write_line(string(buffer.begin(), buffer.begin() + number_of_byte_received));
}
}};
auto client = thread{[&] {
auto client_socket = socket {address_family::inter_network, socket_type::dgram, protocol_type::udp};
auto counter = 0;
while (!terminate_app) {
auto str = string::format("counter={}", ++counter);
client_socket.send_to(list<unsigned char> {str.begin(), str.end()}, ip_end_point {ip_address::loopback, 9400});
thread::sleep(50_ms);
}
client_socket.send_to(list<unsigned char> {0xFF}, 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 strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
const_iterator begin() const noexcept override
Returns an iterator to the first element of the enumarable.
Definition list.h:204
Represents a network endpoint as an IP address and a port number.
Definition ip_end_point.h:23
Implements the Berkeley sockets interface.
Definition socket.h:74
void bind(const end_point_t &local_end_point)
Associates a xtd::net::sockets::socket with a local endpoint.
Definition socket.h:798
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:43
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
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:11
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10