#include <xtd/net/sockets/socket>
#include <xtd/net/ip_end_point>
#include <xtd/threading/thread>
#include <xtd/as>
#include <xtd/console>
auto main()->int {
auto terminate_app = false;
while (!terminate_app) {
auto buffer = std::vector<unsigned char>(256);
if (number_of_byte_received) console::write_line(
ustring {buffer.begin(), buffer.begin() + number_of_byte_received});
}
};
auto socket = as<xtd::net::sockets::socket>(ar->async_state());
auto counter = 0;
while (!terminate_app) {
auto str = ustring::format(
"socket={}, counter={}",
socket.
handle(), ++counter);
socket.
send(std::vector<unsigned char> {str.begin(), str.end()});
thread::sleep(50_ms);
}
};
const size_t client_count = 10;
auto server_socket =
socket {address_family::inter_network, socket_type::stream, protocol_type::tcp};
server_socket.listen();
for (auto index = 0_z; index < client_count; ++index)
server_socket.begin_accept(
async_callback(on_server_accept), server_socket);
for (auto index = 0_z; index < client_count; ++index) {
}
console::read_key();
terminate_app = true;
}
Represents a network endpoint as an IP address and a port number.
Definition ip_end_point.h:21
Implements the Berkeley sockets interface.
Definition socket.h:71
void bind(const end_point_t &local_end_point)
Associates a xtd::net::sockets::socket with a local endpoint.
Definition socket.h:795
size_t send(const std::vector< xtd::byte > &buffer)
Sends data to a connected xtd::net::sockets::socket.
intptr handle() const noexcept
Gets the operating system handle for the xtd::net::sockets::socket.
socket end_accept(std::shared_ptr< xtd::iasync_result > async_result)
Asynchronously accepts an incoming connection attempt and creates a new xtd::net::sockets::socket::so...
size_t receive(std::vector< xtd::byte > &buffer)
Receives data from a bound xtd::net::sockets::socket into a receive buffer.
std::shared_ptr< xtd::iasync_result > begin_connect(const end_point_t &remote_end_point, xtd::async_callback callback, const std::any &state)
Begins an asynchronous request for a remote host connection.
Definition socket.h:578
void end_connect(std::shared_ptr< xtd::iasync_result > async_result)
Ends a pending asynchronous connection request.
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
std::shared_ptr< xtd::iasync_result > async_result
Represents the status of an asynchronous operation.
Definition async_result.h:16
delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition delegate.h:34
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