xtd 0.2.0
Loading...
Searching...
No Matches
socket_tcp_ip_v4_without_thread.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/as>
#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 on_server_accept = [&](async_result ar) {
auto socket = as<xtd::net::sockets::socket>(ar->async_state()).end_accept(ar);
while (!terminate_app) {
auto buffer = list<unsigned char>(256);
auto number_of_byte_received = socket.receive(buffer);
if (number_of_byte_received) console::write_line(string {buffer.begin(), buffer.begin() + number_of_byte_received});
}
};
auto on_client_connect = [&](async_result ar) {
auto socket = as<xtd::net::sockets::socket>(ar->async_state());
auto counter = 0;
while (!terminate_app) {
auto str = string::format("socket={}, counter={}", socket.handle(), ++counter);
socket.send(list<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.bind(ip_end_point {ip_address::any, 9400});
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) {
auto socket = xtd::net::sockets::socket(address_family::inter_network, socket_type::stream, protocol_type::tcp);
socket.begin_connect(ip_address::loopback, 9400, async_callback(on_client_connect), socket);
}
console::read_key();
terminate_app = true;
}
// This code can produce the following output :
//
// socket=4, counter=1
// socket=5, counter=1
// socket=6, counter=1
// socket=9, counter=1
// socket=10, counter=1
// socket=12, counter=1
// socket=15, counter=1
// socket=17, counter=1
// socket=18, counter=1
// socket=20, counter=1
// socket=5, counter=2
// socket=4, counter=2
// socket=6, counter=2
// socket=9, counter=2
// socket=10, counter=2
// socket=12, counter=2
// socket=17, counter=2
// socket=18, counter=2
// socket=15, counter=2
// socket=20, counter=2
// socket=10, counter=3
// socket=12, counter=3
// socket=15, counter=3
// socket=17, counter=3
// socket=4, counter=3
// socket=6, counter=3
// socket=9, counter=3
// socket=5, counter=3
// socket=20, counter=3
// socket=18, counter=3
// socket=17, counter=4
// socket=9, counter=4
// socket=15, counter=4
// socket=4, counter=4
// socket=12, counter=4
// socket=6, counter=4
// socket=5, counter=4
// socket=18, counter=4
// socket=10, counter=4
// socket=20, counter=4
// socket=6, counter=5
// socket=18, counter=5
// socket=4, counter=5
// socket=12, counter=5
// socket=9, counter=5
// socket=15, counter=5
// socket=17, counter=5
// socket=5, counter=5
// socket=10, counter=5
// socket=20, 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
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.
void end_connect(xtd::sptr< xtd::iasync_result > async_result)
Ends a pending asynchronous connection request.
size_t receive(std::vector< xtd::byte > &buffer)
Receives data from a bound xtd::net::sockets::socket into a receive buffer.
socket end_accept(xtd::sptr< xtd::iasync_result > async_result)
Asynchronously accepts an incoming connection attempt and creates a new xtd::net::sockets::socket::so...
xtd::sptr< 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:581
xtd::sptr< xtd::iasync_result > async_result
Represents the status of an asynchronous operation.
Definition async_result.h:19
delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition delegate.h:36
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