xtd 0.2.0
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::net;
using namespace xtd::net::sockets;
using namespace xtd::threading;
auto main() -> int {
auto terminate_app = false;
auto server = thread {[&] {
server_socket.bind(ip_end_point {ip_address::any, 9400});
while (!terminate_app) {
auto buffer = array<byte>(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 counter = 0;
while (!terminate_app) {
auto str = string::format("counter={}", ++counter);
client_socket.send_to(array<byte> {str.begin(), str.end()}, ip_end_point {ip_address::loopback, 9400});
thread::sleep(50_ms);
}
client_socket.send_to(array<byte> {0xFF}, ip_end_point {ip_address::loopback, 9400});
}};
server.start();
client.start();
terminate_app = true;
server.join();
client.join();
}
// This code produces the following output :
//
// counter=1
// counter=2
// counter=3
// counter=4
// counter=5
// ...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:61
static console_key_info read_key()
Obtains the next character or function key pressed by the user. The pressed key is displayed in the c...
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static const ip_address any
Provides an IP address that indicates that the server must listen for client activity on all network ...
Definition ip_address.hpp:49
static const ip_address loopback
Provides the IP loopback address. This field is constant.
Definition ip_address.hpp:65
Represents a network endpoint as an IP address and a port number.
Definition ip_end_point.hpp:28
Implements the Berkeley sockets interface.
Definition socket.hpp:77
void bind(const end_point_t &local_end_point)
Associates a xtd::net::sockets::socket with a local endpoint.
Definition socket.hpp:801
size_t send_to(const xtd::array< xtd::byte > &buffer, const xtd::net::end_point &remote_end_point)
Sends data to the specified endpoint.
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:45
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
void start()
Causes the operating system to change the state of the current instance to xtd::threading::thread_sta...
@ inter_network
Address for IP version 4.
Definition address_family.hpp:38
@ udp
User Datagram Protocol.
Definition protocol_type.hpp:52
@ dgram
Supports datagrams, which are connectionless, unreliable messages of a fixed (typically small) maximu...
Definition socket_type.hpp:38
The xtd::net::sockets namespace provides a managed implementation of the Berkeley Sockets interface f...
Definition address_family.hpp:16
The xtd::net namespace provides a simple programming interface for many of the protocols used on netw...
Definition cookie_exception.hpp:10
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8