xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Classes | Public Member Functions | List of all members
xtd::net::sockets::network_stream Class Reference

#include <network_stream.h>

Definition

Provides the underlying stream of data for network access.

Namespace
xtd::net::sockets
Library
xtd.core
Remarks
The xtd::net::sockets::network_stream class provides methods for sending and receiving data over sttd::iostrream 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/xtd>
using namespace std;
using namespace std::literals;
using namespace xtd;
using namespace xtd::io;
using namespace xtd::net;
using namespace xtd::net::sockets;
int main() {
auto terminate_app = false;
thread server([&] {
server_socket.bind(ip_end_point(ip_address::any, 9400));
server_socket.listen();
network_stream stream(server_socket.accept());
while (!terminate_app)
if (stream.data_availlable()) console::write_line(reader.read_line());
});
thread client([&] {
stream.socket().connect(ip_address::loopback, 9400);
auto counter = 1;
while (!terminate_app) {
writer.write_line(ustring::format("counter={}", counter++));
this_thread::sleep_for(50ms);
}
});
terminate_app = true;
server.join();
client.join();
}
// This code produces the following output:
//
// counter=1
// counter=2
// counter=3
// counter=4
// counter=5
// ...

Public Member Functions

 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...
 
virtual bool data_availlable () 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)

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_availlable()

virtual bool xtd::net::sockets::network_stream::data_availlable ( ) 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: