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

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

#include <xtd/io/stream_reader>
#include <xtd/net/sockets/tcp_client>
#include <xtd/net/sockets/tcp_listener>
#include <xtd/threading/thread>
#include <xtd/console>
using namespace xtd;
using namespace xtd::io;
using namespace xtd::net;
using namespace xtd::net::sockets;
using namespace xtd::threading;
auto main()->int {
auto terminate_app = false;
auto server = thread {[&] {
auto listener = tcp_listener::create(9400);
listener.start();
auto stream = listener.accept_tcp_client().get_stream();
auto reader = stream_reader {stream};
while (!terminate_app)
if (stream.data_available()) console::write_line(reader.read_line());
}};
auto client = thread {[&] {
auto client = tcp_client {};
client.connect(ip_address::loopback, 9400);
auto stream = client.get_stream();
auto writer = stream_writer {stream};
auto counter = 0;
while (!terminate_app) {
writer.write_line(ustring::format("counter={}", ++counter));
thread::sleep(50_ms);
}
}};
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
// ...
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.h:26
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.h:26
Provides client connections for TCP network services.
Definition tcp_client.h:46
void connect(const xtd::net::ip_end_point &remote_end_point)
Connects the client to a remote TCP host using the specified remote network endpoint.
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:41
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
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