xtd 0.2.0
xtd::net::sockets::network_stream Class Reference

Definition

Provides the underlying stream of data for network access.

class core_export_ network_stream : public std::iostream
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(ustring::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
// ...
Examples:
network_stream.cpp.

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. More...
 
 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. More...
 

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. More...
 
xtd::net::sockets::socket socket () const
 Gets the underlying xtd::net::sockets::socket. More...
 

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: