xtd 0.2.0
Loading...
Searching...
No Matches
xtd::net::sockets::network_stream Class Reference
Inheritance diagram for xtd::net::sockets::network_stream:

Definition

Provides the underlying stream of data for network access.

class core_export_ network_stream : public std::iostream
Provides the underlying stream of data for network access.
Definition network_stream.h:37
#define core_export_
Define shared library export.
Definition core_export.h:13
Inheritance
std::iostream → xtd::net::sockets::network_stream
Header
#include <xtd/net/sockets/network_stream>
Namespace
xtd::net::sockets
Library
xtd.core
Remarks
The xtd::net::sockets::network_stream class provides methods for sending and receiving data over std::iostream sockets in blocking mode. You can use the xtd::net::sockets::network_stream class for both synchronous and asynchronous data transfer.
To create a xtd::net::sockets::network_stream, you must provide a connected xtd::net::sockets::socket. By default, closing the xtd::net::sockets::network_stream does not close the provided xtd::net::sockets::socket. If you want the xtd::net::sockets::network_stream to have permission to close the provided xtd::net::sockets::socket, you must specify true for the value of the owns_socket parameter.
Use the std::iostream::write and std::iostream::read methods for simple single thread synchronous blocking I/O.
The xtd::net::sockets::network_stream does not support random access to the network data stream.
std::iostream::read and std::iostream::write operations can be performed simultaneously on an instance of the xtd::net::sockets::network_stream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.
Examples
The following example shows how to use xtd::net::sockets::network_stream class with xtd::net::sockets::socket, xtd::io::stream_reader and xtd::io::stream_writer classes.
#include <xtd/io/stream_reader>
#include <xtd/io/stream_writer>
#include <xtd/net/sockets/socket>
#include <xtd/net/sockets/network_stream>
#include <xtd/net/ip_end_point>
#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 {[&] {
server_socket.bind(ip_end_point {ip_address::any, 9400});
server_socket.listen();
auto stream = network_stream {server_socket.accept()};
auto reader = stream_reader {stream};
while (!terminate_app)
if (stream.data_available()) console::write_line(reader.read_line());
}};
server.start();
auto client = thread {[&] {
stream.socket().connect(ip_address::loopback, 9400);
auto writer = stream_writer {stream};
auto counter = 0;
while (!terminate_app) {
writer.write_line(string::format("counter={}", ++counter));
thread::sleep(50_ms);
}
}};
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
// ...
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...
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.h:28
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.h:28
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.h:44
static const ip_address loopback
Provides the IP loopback address. This field is constant.
Definition ip_address.h:60
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
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:43
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
@ inter_network
Address for IP version 4.
@ tcp
Transmission Control Protocol.
@ stream
Supports reliable, two-way, connection-based byte streams without the duplication of data and without...
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: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
Examples
network_stream.cpp.

Public Constructors

 network_stream (const xtd::net::sockets::socket &socket)
 Creates a new instance of the network_stream class for the specified xtd::net::sockets::socket.
 
 network_stream (const xtd::net::sockets::socket &socket, bool owns_socket)
 Initializes a new instance of the xtd::net::sockets::network_stream class for the specified xtd::net::sockets::socket with the specified xtd::net::sockets::socket ownership.
 

Public Properties

virtual bool data_available () const
 Gets a value that indicates whether data is available on the xtd::net::sockets::network_stream to be read.
 
xtd::net::sockets::socket socket () const
 Gets the underlying xtd::net::sockets::socket.
 

Constructor & Destructor Documentation

◆ network_stream() [1/2]

xtd::net::sockets::network_stream::network_stream ( const xtd::net::sockets::socket socket)
explicit

Creates a new instance of the network_stream class for the specified xtd::net::sockets::socket.

Parameters
socketThe xtd::net::sockets::socket that the xtd::net::sockets::network_stream will use to send and receive data.
Remarks
The xtd::net::sockets::network_stream is created with read/write access to the specified xtd::net::sockets::socket. The xtd::net::sockets::network_stream does not own the underlying xtd::net::sockets::socket, so calling the close method does not close the xtd::net::sockets::socket.

◆ network_stream() [2/2]

xtd::net::sockets::network_stream::network_stream ( const xtd::net::sockets::socket socket,
bool  owns_socket 
)

Initializes a new instance of the xtd::net::sockets::network_stream class for the specified xtd::net::sockets::socket with the specified xtd::net::sockets::socket ownership.

Parameters
socketThe xtd::net::sockets::socket that the xtd::net::sockets::network_stream will use to send and receive data.
owns_socketSet to true to indicate that the xtd::net::sockets::network_stream will take ownership of the xtd::net::sockets::socket; otherwise, false.
Remarks
The xtd::net::sockets::network_stream is created with read/write access to the specified xtd::net::sockets::socket. If the value of owns_socket parameter is true, the xtd::net::sockets::network_stream takes ownership of the underlying xtd::net::sockets::socket, and calling the close method also closes the underlying xtd::net::sockets::socket.

Member Function Documentation

◆ data_available()

virtual bool xtd::net::sockets::network_stream::data_available ( ) const
virtual

Gets a value that indicates whether data is available on the xtd::net::sockets::network_stream to be read.

Returns
true if data is available on the stream to be read; otherwise, false.
Exceptions
xtd::net::sockets::socket_exceptionAn error occurred when attempting to access the socket.
xtd::object_closed_exceptionThe xtd::net::sockets::socket has been closed.
Remarks
Use the xtd::net::sockets::network_stream::data_available property to determine if data is ready to be read. If xtd::net::sockets::network_stream::data_available is true, a call to std::iostream::read returns immediately. If the remote host shuts down or closes the connection, xtd::net::sockets::network_stream::data_available may throw a xtd::net::sockets::socket_exception.

◆ socket()

xtd::net::sockets::socket xtd::net::sockets::network_stream::socket ( ) const

Gets the underlying xtd::net::sockets::socket.

Returns
A xtd::net::sockets::socket that represents the underlying network connection.
Remarks
Classes deriving from xtd::net::sockets::network_stream can use this property to get the underlying xtd::net::sockets::socket. Use the underlying xtd::net::sockets::socket returned from the xtd::net::sockets::socketproperty if you require access beyond that which xtd::net::sockets::network_stream provides.

The documentation for this class was generated from the following file: