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

Definition

Provides the underlying stream of data for network access.

Provides a generic view of a sequence of bytes. This is an abstract class.
Definition stream.hpp:40
Provides the underlying stream of data for network access.
Definition network_stream.hpp:40
#define core_export_
Define shared library export.
Definition core_export.hpp:13
Inheritance
xtd::objectxtd::abstract_objectxtd::io::streamxtd::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(), file_access::read};
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("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.hpp:28
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.hpp: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.hpp:49
static const ip_address loopback
Provides the IP loopback address. This field is constant.
Definition ip_address.hpp:65
Represents a network endpoint as an IP address and a port number.
Definition ip_end_point.hpp:28
Implements the Berkeley sockets interface.
Definition socket.hpp:76
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:45
@ 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.hpp:16
The xtd::net::sockets namespace provides a managed implementation of the Berkeley Sockets interface f...
Definition address_family.hpp:16
The xtd::net namespace provides a simple programming interface for many of the protocols used on netw...
Definition cookie_exception.hpp:11
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp: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.
 
 network_stream (const xtd::net::sockets::socket &socket, xtd::io::file_access access)
 Initializes a new instance of the network_stream class for the specified xtd::net::sockets::socket with the specified access rights.
 
 network_stream (const xtd::net::sockets::socket &socket, xtd::io::file_access access, bool owns_socket)
 Initializes a new instance of the network_stream class for the specified xtd::net::sockets::socket with the specified access rights and the specified xtd::net::sockets::socket ownership.
 

Public Properties

bool can_read () const noexcept override
 Gets a value indicating whether the current stream supports reading.
 
bool can_seek () const noexcept override
 Gets a value indicating whether the current stream supports seeking.
 
bool can_timeout () const noexcept override
 Indicates whether timeout properties are usable for xtd::net::sockets::network_stream.
 
bool can_write () const noexcept override
 Gets a value indicating whether the current stream supports writing.
 
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::size length () const override
 Gets the length of the stream in bytes.
 
xtd::size position () const override
 Gets the current position within the stream.
 
void position (xtd::size value) override
 Sets the current position within the stream.
 
int32 read_timeout () const override
 Gets the amount of time that a read operation blocks waiting for data.
 
void read_timeout (int32 value) override
 Sets the amount of time that a read operation blocks waiting for data.
 
int32 write_timeout () const override
 Gets the amount of time that a write operation blocks waiting for data.
 
void write_timeout (int32 value) override
 Sets the amount of time that a write operation blocks waiting for data.
 
xtd::net::sockets::socket socket () const
 Gets the underlying xtd::net::sockets::socket.
 

Public Methods

void flush () override
 Flushes data from the stream. This method is reserved for future use.
 
xtd::size read (xtd::array< xtd::byte > &buffer, size offset, size count) override
 Reads a block of bytes from the current stream and writes the data to a buffer.
 
void set_length (xtd::size value) override
 Sets the length of the current stream to the specified value.
 
void write (const xtd::array< xtd::byte > &buffer, size offset, size count) override
 Writes a block of bytes to the current stream using data read from a buffer.
 

Additional Inherited Members

- Static Public Attributes inherited from xtd::io::stream
static xtd::io::null_stream null_stream
 A Stream with no backing store.
 
static constexpr int32 eof
 Represnets an eof value.
 
- Public Member Functions inherited from xtd::io::stream
virtual bool is_closed () const noexcept
 Indicates if the stream is closed.
 
void copy_to (std::ostream &destination)
 Reads the bytes from the current memory stream and writes them to another stream.
 
void copy_to (std::ostream &destination, xtd::size buffer_size)
 Reads the bytes from the current memory stream and writes them to another stream, using a specified buffer size.
 
virtual xtd::size read (xtd::span< xtd::byte > &buffer)
 When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
 
xtd::size read_at_least (xtd::array< xtd::byte > &buffer, xtd::size minimum_bytes, bool throw_on_end_of_stream=true)
 Reads at least a minimum number of bytes from the current stream and advances the position within the stream by the number of bytes read.
 
virtual int32 read_byte ()
 
void read_exactly (xtd::array< xtd::byte > &buffer)
 Reads bytes from the current stream and advances the position within the stream until the buffer is filled.
 
void read_exactly (xtd::array< xtd::byte > &buffer, xtd::size offset, xtd::size count)
 Reads count number of bytes from the current stream and advances the position within the stream.
 
virtual void write (xtd::span< const xtd::byte > buffer)
 
virtual void write_byte (xtd::byte value)
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual xtd::size get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<class object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const noexcept
 Returns a xtd::string that represents the current object.
 
- Static Public Member Functions inherited from xtd::object
template<class object_a_t , class object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<class object_a_t , class object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 
- Protected Member Functions inherited from xtd::io::stream
 stream ()
 Initializes a new instance of the xtd::io::stream class.
 
- Protected Member Functions inherited from xtd::abstract_object
 abstract_object ()=default
 Initializes a new instance of the xtd::abstract_object class.
 

Constructor & Destructor Documentation

◆ network_stream() [1/4]

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/4]

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.

◆ network_stream() [3/4]

xtd::net::sockets::network_stream::network_stream ( const xtd::net::sockets::socket socket,
xtd::io::file_access  access 
)

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

Parameters
socketThe xtd::net::sockets::socket that the xtd::net::sockets::network_stream will use to send and receive data.
accessA bitwise combination of the xtd::io::file_access values that specify the type of access given to the xtd::net::sockets::network_stream over the provided xtd::net::sockets::socket.

◆ network_stream() [4/4]

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

Initializes a new instance of the network_stream class for the specified xtd::net::sockets::socket with the specified access rights and 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.
accessA bitwise combination of the xtd::io::file_access values that specify the type of access given to the xtd::net::sockets::network_stream over the provided xtd::net::sockets::socket.
owns_socketSet to true to indicate that the xtd::net::sockets::network_stream will take ownership of the xtd::net::sockets::socket; otherwise, false.

Member Function Documentation

◆ can_read()

bool xtd::net::sockets::network_stream::can_read ( ) const
overridevirtualnoexcept

Gets a value indicating whether the current stream supports reading.

Returns
true if the stream supports reading; otherwise, false.

Implements xtd::io::stream.

◆ can_seek()

bool xtd::net::sockets::network_stream::can_seek ( ) const
overridevirtualnoexcept

Gets a value indicating whether the current stream supports seeking.

Returns
true if the stream supports seeking; otherwise, false.

Implements xtd::io::stream.

◆ can_timeout()

bool xtd::net::sockets::network_stream::can_timeout ( ) const
overridevirtualnoexcept

Indicates whether timeout properties are usable for xtd::net::sockets::network_stream.

Returns
true in all cases.

Reimplemented from xtd::io::stream.

◆ can_write()

bool xtd::net::sockets::network_stream::can_write ( ) const
overridevirtualnoexcept

Gets a value indicating whether the current stream supports writing.

Returns
true if the stream supports writing; otherwise, false.

Implements xtd::io::stream.

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

◆ length()

xtd::size xtd::net::sockets::network_stream::length ( ) const
overridevirtual

Gets the length of the stream in bytes.

Returns
The length of the stream in bytes.

Implements xtd::io::stream.

◆ position() [1/2]

xtd::size xtd::net::sockets::network_stream::position ( ) const
overridevirtual

Gets the current position within the stream.

Returns
The current position within the stream.

Implements xtd::io::stream.

◆ position() [2/2]

void xtd::net::sockets::network_stream::position ( xtd::size  value)
overridevirtual

Sets the current position within the stream.

Parameters
valueThe current position within the stream.

Implements xtd::io::stream.

◆ read_timeout() [1/2]

int32 xtd::net::sockets::network_stream::read_timeout ( ) const
overridevirtual

Gets the amount of time that a read operation blocks waiting for data.

Returns
A xtd::int32 that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, xtd::threading::timeout::infinite, specifies that the read operation does not time out.

Reimplemented from xtd::io::stream.

◆ read_timeout() [2/2]

void xtd::net::sockets::network_stream::read_timeout ( int32  value)
overridevirtual

Sets the amount of time that a read operation blocks waiting for data.

Parameters
valueA xtd::int32 that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, xtd::threading::timeout::infinite, specifies that the read operation does not time out.

Reimplemented from xtd::io::stream.

◆ write_timeout() [1/2]

int32 xtd::net::sockets::network_stream::write_timeout ( ) const
overridevirtual

Gets the amount of time that a write operation blocks waiting for data.

Returns
A xtd::int32 that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, xtd::threading::timeout::infinite, specifies that the read operation does not time out.

Reimplemented from xtd::io::stream.

◆ write_timeout() [2/2]

void xtd::net::sockets::network_stream::write_timeout ( int32  value)
overridevirtual

Sets the amount of time that a write operation blocks waiting for data.

Parameters
valueA xtd::int32 that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, xtd::threading::timeout::infinite, specifies that the read operation does not time out.

Reimplemented from xtd::io::stream.

◆ 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::socket property if you require access beyond that which xtd::net::sockets::network_stream provides.

◆ flush()

void xtd::net::sockets::network_stream::flush ( )
overridevirtual

Flushes data from the stream. This method is reserved for future use.

Remarks
The Flush method implements the xtd::io::stream::flush method; however, because xtd::net::sockets::network_stream is not buffered, it has no effect on network streams. Calling the xtd::net::sockets::network_stream::flush method does not throw an exception.

Implements xtd::io::stream.

◆ read()

xtd::size xtd::net::sockets::network_stream::read ( xtd::array< xtd::byte > &  buffer,
size  offset,
size  count 
)
overridevirtual

Reads a block of bytes from the current stream and writes the data to a buffer.

Parameters
bufferWhen this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.
offsetThe zero-based byte offset in buffer at which to begin storing data from the current stream.
countThe maximum number of bytes to read.
Returns
The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.
Remarks
The offset parameter gives the offset of the first byte in buffer to which data from the current stream is written. The count parameter gives the maximum number of bytes to read from the current stream. The returned value is the actual number of bytes read, or zero if the end of the stream is reached.
If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.
The read method will return zero only if the end of the stream is reached. In all other cases, read always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to read, the read method returns zero (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.
Use xtd::io::binary_reader for reading primitive data types.

Implements xtd::io::stream.

◆ set_length()

void xtd::net::sockets::network_stream::set_length ( xtd::size  value)
overridevirtual

Sets the length of the current stream to the specified value.

Parameters
valueThe value at which to set the length.
Remarks
If the specified value is less than the current length of the stream, the stream is truncated. If after the truncation the current position within the stream is past the end of the stream, the xtd::io::memery_stream::read_byte method returns -1, the xtd::io::memery_stream::read method reads zero bytes into the provided byte array, and xtd::io::memery_stream::write and xtd::io::memery_stream::write_byte methods append specified bytes at the end of the stream, increasing its length. If the specified value is larger than the current capacity and the stream is resizable, the capacity is increased, and the current position within the stream is unchanged. If the length is increased, the contents of the stream between the old and the new length are initialized to zeros.

Implements xtd::io::stream.

◆ write()

void xtd::net::sockets::network_stream::write ( const xtd::array< xtd::byte > &  buffer,
size  offset,
size  count 
)
overridevirtual

Writes a block of bytes to the current stream using data read from a buffer.

Parameters
bufferThe buffer to write data from.
offsetThe zero-based byte offset in buffer at which to begin copying bytes to the current stream.
countThe maximum number of bytes to write.
Examples
This code example is part of a larger example provided for the xtd::io::memory_stream class.
// Write the first string to the stream.
mem_stream.write(first_string, 0 , first_string.length());
Remarks
The offset parameter gives the offset of the first byte in buffer to write from, and the count parameter gives the number of bytes to write. If the write operation is successful, the current position within the stream is advanced by the number of bytes written. If an exception occurs, the current position within the stream is unchanged.
Except for a memory_stream constructed with a byte[] parameter, write operations at the end of a memory_stream expand the memory_stream.

Implements xtd::io::stream.


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