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.
Loading...
Searching...
No Matches
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 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/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_available()) 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
// ...
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:20
Implements a xtd::io::text_writer for writing characters to a stream.
Definition: stream_writer.h:20
static ip_address any
Provides an IP address that indicates that the server must listen for client activity on all network ...
Definition: ip_address.h:34
static ip_address loopback
Provides the IP loopback address. This field is constant.
Definition: ip_address.h:50
Represents a network endpoint as an IP address and a port number.
Definition: ip_end_point.h:20
Provides the underlying stream of data for network access.
Definition: network_stream.h:29
xtd::net::sockets::socket socket() const
Gets the underlying xtd::net::sockets::socket.
Implements the Berkeley sockets interface.
Definition: socket.h:63
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition: ustring.h:689
@ 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: directory_not_found_exception.h:10
The xtd::net::sockets namespace provides a managed implementation of the Berkeley Sockets interface f...
Definition: address_family.h:17
The xtd::net namespace provides a simple programming interface for many of the protocols used on netw...
Definition: cookie_exception.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17

Inherits std::iostream.

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

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: