Base Framework
Public Member Functions | List of all members
StreamSocket Class Reference

Stream socket. More...

#include <base/net/StreamSocket.h>

Inherits Socket, InputStream, and OutputStream.

Public Member Functions

 StreamSocket ()
 
 StreamSocket (const InetAddress &address, unsigned short port)
 
 StreamSocket (const InetEndPoint &endPoint)
 
 StreamSocket (const InetAddress &address, unsigned short port, const InetAddress &localAddress, unsigned short localPort)
 
 StreamSocket (ServerSocket &socket)
 
 StreamSocket (const StreamSocket &copy) noexcept
 
void bind (const InetAddress &address, unsigned short port)
 
void bind (const InetEndPoint &endPoint)
 
void close ()
 
void connect (const InetAddress &address, unsigned short port)
 
void connect (const InetEndPoint &endPoint)
 
void getName ()
 
const InetAddressgetAddress () const noexcept
 
unsigned short getPort () const noexcept
 
InetEndPoint getEndPoint () const noexcept
 
const InetAddressgetLocalAddress () const noexcept
 
unsigned short getLocalPort () const noexcept
 
InetEndPoint getLocalEndPoint () const noexcept
 
void shutdownInputStream ()
 
void shutdownOutputStream ()
 
int getErrorState () const
 
bool getReuseAddress () const
 
void setReuseAddress (bool value)
 
bool getKeepAlive () const
 
void setKeepAlive (bool value)
 
int getLinger () const
 
void setLinger (int seconds)
 
int getReceiveBufferSize () const
 
void setReceiveBufferSize (int size)
 
int getSendBufferSize () const
 
void setSendBufferSize (int size)
 
bool getTcpNoDelay () const
 
void setTcpNoDelay (bool value)
 
unsigned int getTimeToLive () const
 
void setTimeToLive (unsigned int value)
 
void setNonBlocking (bool value)
 
unsigned int available () const
 
unsigned int read (uint8 *buffer, unsigned int size, bool nonblocking=false)
 
unsigned int write (const uint8 *buffer, unsigned int size, bool nonblocking=false)
 
void wait () const
 
bool wait (unsigned int timeout) const
 
 operator bool () const noexcept
 
unsigned int skip (unsigned int count)
 
void flush ()
 
unsigned int read (uint8 *buffer, unsigned int size, bool nonblocking=false)
 
AsynchronousReadOperation read (uint8 *buffer, unsigned int bytesToRead, AsynchronousReadEventListener *listener)
 
unsigned int write (const uint8 *buffer, unsigned int size, bool nonblocking=false)
 
AsynchronousWriteOperation write (const uint8 *buffer, unsigned int bytesToWrite, AsynchronousWriteEventListener *listener)
 
- Public Member Functions inherited from Socket
 Socket () noexcept
 
Domain getDomain () const
 
Kind getKind () const
 
bool accept (Socket &socket)
 
void bind (const InetAddress &address, unsigned short port)
 
void bind (const InetEndPoint &endPoint)
 
void close ()
 
void connect (const InetAddress &address, unsigned short port)
 
void connect (const InetEndPoint &endPoint)
 
void create (Kind kind, Domain domain=DEFAULT_DOMAIN)
 
void getName () noexcept
 
const InetAddressgetAddress () const noexcept
 
unsigned short getPort () const noexcept
 
InetEndPoint getEndPoint () const noexcept
 
const InetAddressgetLocalAddress () const noexcept
 
unsigned short getLocalPort () const noexcept
 
InetEndPoint getLocalEndPoint () const noexcept
 
void listen (unsigned int backlog)
 
void shutdownInputStream ()
 
void shutdownOutputStream ()
 
int getErrorState () const
 
bool getReuseAddress () const
 
void setReuseAddress (bool value)
 
bool getKeepAlive () const
 
void setKeepAlive (bool value)
 
bool getBroadcast () const
 
void setBroadcast (bool value)
 
int getLinger () const
 
void setLinger (int seconds)
 
int getReceiveBufferSize () const
 
void setReceiveBufferSize (int size)
 
int getSendBufferSize () const
 
void setSendBufferSize (int size)
 
bool getDontRoute () const
 
void setDontRoute (bool value)
 
uint64 getReceiveTimeout () const
 
void setReceiveTimeout (uint64 nanoseconds)
 
uint64 getSendTimeout () const
 
void setSendTimeout (uint64 nanoseconds)
 
bool getTcpNoDelay () const
 
void setTcpNoDelay (bool value)
 
uint64 getTcpDeferAccept () const
 
void setTcpDeferAccept (uint64 nanoseconds)
 
unsigned int getTimeToLive () const
 
void setTimeToLive (unsigned int value)
 
uint8 getMulticastHops () const
 
void setMulticastHops (uint8 value)
 
bool getMulticastLoopback () const
 
void setMulticastLoopback (bool value)
 
InetAddress getMulticastInterface () const
 
void setMulticastInterface (const InetAddress &interface)
 
uint8 getUnicastHops () const
 
void setUnicastHops (uint8 value)
 
void joinGroup (const InetAddress &group)
 
void joinGroup (const InetAddress &interface, const InetAddress &group)
 
void leaveGroup (const InetAddress &interface, const InetAddress &group)
 
bool getIPv6Restriction () const
 
void setIPv6Restriction (bool value)
 
void setNonBlocking (bool value)
 
unsigned int available () const
 
void flush ()
 
unsigned int read (uint8 *buffer, unsigned int size, bool nonblocking=false)
 
unsigned int write (const uint8 *buffer, unsigned int size, bool nonblocking=false)
 
unsigned int sendTo (const uint8 *buffer, unsigned int size, const InetAddress &address, unsigned short port)
 
unsigned int receiveFrom (uint8 *buffer, unsigned int size, InetAddress &address, unsigned short &port)
 
void asyncCancel ()
 
AsynchronousReadOperation read (uint8 *buffer, unsigned int bytesToRead, AsynchronousReadEventListener *listener)
 
AsynchronousWriteOperation write (const uint8 *buffer, unsigned int bytesToWrite, AsynchronousWriteEventListener *listener)
 
void wait () const
 
bool wait (unsigned int microseconds) const
 
 ~Socket ()
 
- Public Member Functions inherited from Resource
String getDescription () const
 
void setDescription (const String &description)
 
unsigned int getResourceId () const
 
unsigned int getCreatedById () const
 
AnyReference getHandle () const noexcept
 
template<class TYPE >
TYPE & getInternalHandle () const
 
 operator bool () const noexcept
 
- Public Member Functions inherited from AsynchronousStream
virtual ~AsynchronousStream () noexcept(false)
 
- Public Member Functions inherited from Stream
virtual ~Stream () noexcept(false)
 
- Public Member Functions inherited from ReferenceCountedObject
 ReferenceCountedObject () noexcept
 
 ReferenceCountedObject (const ReferenceCountedObject &copy) noexcept
 
 ReferenceCountedObject (ReferenceCountedObject &&move) noexcept
 
ReferenceCountedObjectoperator= (const ReferenceCountedObject &copy) noexcept
 
ReferenceCountedObjectoperator= (ReferenceCountedObject &&move) noexcept
 
MemorySize getNumberOfReferences_INTERNAL () const noexcept
 
virtual bool useGarbageCollector () const noexcept
 
- Public Member Functions inherited from DynamicObject
 DynamicObject () noexcept
 
bool isValidObject () const noexcept
 
virtual ~DynamicObject () noexcept(false)
 
 _COM_AZURE_DEV__BASE__OVERRIDE_ALLOC ()
 

Additional Inherited Members

- Public Types inherited from Socket
enum  Domain { IPV4, IPV6, DEFAULT_DOMAIN = IPV4 }
 
enum  Kind { STREAM, DATAGRAM, RAW }
 
- Static Public Member Functions inherited from Socket
static const char * toString (Domain domain) noexcept
 
static const char * toString (Kind kind) noexcept
 
- Static Public Member Functions inherited from AsynchronousStream
static bool asyncTest () noexcept
 
- Protected Types inherited from Socket
enum  { OPTION_DONT_ROUTE, OPTION_TCP_NO_DELAY, OPTION_TCP_DEFER_ACCEPT }
 
- Protected Member Functions inherited from Socket
OperatingSystem::Handle getHandle () const noexcept
 
bool getBooleanOption (int option) const
 
void setBooleanOption (int option, bool value)
 
- Protected Attributes inherited from Resource
AnyReference handle
 

Detailed Description

Stream socket.

This class implements a stream socket. A socket is an endpoint for communication between two hosts.

Version
1.0
Examples
testsuite/client.cpp, testsuite/echod.cpp, testsuite/ftp.cpp, testsuite/http.cpp, testsuite/ping.cpp, and testsuite/server.cpp.

Constructor & Destructor Documentation

◆ StreamSocket() [1/6]

StreamSocket::StreamSocket ( )

Creates an unconnected stream socket.

◆ StreamSocket() [2/6]

StreamSocket::StreamSocket ( const InetAddress address,
unsigned short  port 
)

Creates a stream socket and connects it to the specified port at the specified IP address.

Parameters
addressThe IP address of the host to connect to.
portThe port to connect to on the host.

◆ StreamSocket() [3/6]

StreamSocket::StreamSocket ( const InetEndPoint endPoint)

Creates a stream socket and connects it to the specified port at the specified IP address.

Parameters
endPointThe end point to connect to.

◆ StreamSocket() [4/6]

StreamSocket::StreamSocket ( const InetAddress address,
unsigned short  port,
const InetAddress localAddress,
unsigned short  localPort 
)

Creates a stream socket and connects it to the specified port at the specified IP address and also binds the socket to the local address and local port.

Parameters
addressThe IP address of the host to connect to.
portThe port to connect to on the host.
localAddressThe local IP address the socket should be bound to.
localPortThe local port the socket should be bound to.

◆ StreamSocket() [5/6]

StreamSocket::StreamSocket ( ServerSocket socket)

Accept connect from server socket.

◆ StreamSocket() [6/6]

StreamSocket::StreamSocket ( const StreamSocket copy)
inlinenoexcept

Initialization of socket from other socket.

Member Function Documentation

◆ available()

unsigned int StreamSocket::available ( ) const
inlinevirtual

Returns the number of bytes that can be read or skipped over without blocking.

Returns
Available number of bytes in stream.

Implements InputStream.

Examples
testsuite/http.cpp, and testsuite/ping.cpp.

◆ bind() [1/2]

void StreamSocket::bind ( const InetAddress address,
unsigned short  port 
)
inline

Associates a local name (address and port) with this socket.

Parameters
addressThe IP address the socket should be bound to.
portThe port the socket should be bound to.

◆ bind() [2/2]

void StreamSocket::bind ( const InetEndPoint endPoint)
inline

Associates a local name (address and port) with this socket.

Parameters
endPointThe end point the socket should be bound to.

◆ close()

void StreamSocket::close ( )
inlinevirtual

◆ connect() [1/2]

void StreamSocket::connect ( const InetAddress address,
unsigned short  port 
)
inline

Connects this socket to the specified address and port.

Parameters
addressThe IP address to connect to.
portThe port to connect to.
Examples
testsuite/client.cpp, testsuite/ftp.cpp, testsuite/http.cpp, and testsuite/ping.cpp.

◆ connect() [2/2]

void StreamSocket::connect ( const InetEndPoint endPoint)
inline

Connects this socket to the specified address and port.

Parameters
endPointThe end-point to connect to.

◆ flush()

void StreamSocket::flush ( )
inlinevirtual

Forces any buffered bytes to be written out.

Implements OutputStream.

◆ getAddress()

const InetAddress& StreamSocket::getAddress ( ) const
inlinenoexcept

Returns the IP address to which the socket is connected.

Examples
testsuite/client.cpp, testsuite/echod.cpp, testsuite/ftp.cpp, and testsuite/server.cpp.

◆ getEndPoint()

InetEndPoint StreamSocket::getEndPoint ( ) const
inlinenoexcept

Returns the remote end-point to which the socket is connected.

◆ getErrorState()

int StreamSocket::getErrorState ( ) const
inline

Returns the error state of the socket.

◆ getKeepAlive()

bool StreamSocket::getKeepAlive ( ) const
inline

Returns true if connection is kept alive.

◆ getLinger()

int StreamSocket::getLinger ( ) const
inline

Gets the linger interval.

Returns
-1 if linger is disabled.

◆ getLocalAddress()

const InetAddress& StreamSocket::getLocalAddress ( ) const
inlinenoexcept

Returns the local IP address to which the socket is bound.

Examples
testsuite/ftp.cpp.

◆ getLocalEndPoint()

InetEndPoint StreamSocket::getLocalEndPoint ( ) const
inlinenoexcept

Returns the local end-point to which the socket is connected.

◆ getLocalPort()

unsigned short StreamSocket::getLocalPort ( ) const
inlinenoexcept

Returns the local port to which the socket is bound.

◆ getName()

void StreamSocket::getName ( )
inline

Caches the locally assigned address and port of the socket. This member function can be used after a succesful connect to determine the locally assigned address and port if unspecified.

Examples
testsuite/ftp.cpp, and testsuite/http.cpp.

◆ getPort()

unsigned short StreamSocket::getPort ( ) const
inlinenoexcept

Returns the remote port to which the socket is connected.

Examples
testsuite/client.cpp, testsuite/echod.cpp, and testsuite/server.cpp.

◆ getReceiveBufferSize()

int StreamSocket::getReceiveBufferSize ( ) const
inline

Gets the size of the receive buffer.

◆ getReuseAddress()

bool StreamSocket::getReuseAddress ( ) const
inline

Returns true if 'bind' allows local addresses to be reused.

◆ getSendBufferSize()

int StreamSocket::getSendBufferSize ( ) const
inline

Gets the size of the send buffer.

◆ getTcpNoDelay()

bool StreamSocket::getTcpNoDelay ( ) const
inline

Returns true of the Nagle's algorithm is disabled.

◆ getTimeToLive()

unsigned int StreamSocket::getTimeToLive ( ) const
inline

Returns the current time to live (TTL) value.

◆ operator bool()

StreamSocket::operator bool ( ) const
inlinenoexcept

Returns true if the socket is valid.

◆ read() [1/2]

unsigned int Socket::read

Fills the buffer with bytes from the socket input stream. Blocks if asked to read more bytes than available. Raises EndOfFile if end of stream has been reached.

Parameters
bufferThe buffer.
sizeThe size of the buffer.
nonblockingSelect nonblocking mode.
Returns
The actual number of bytes read.

◆ read() [2/2]

unsigned int StreamSocket::read ( uint8 *  buffer,
unsigned int  size,
bool  nonblocking = false 
)
inlinevirtual

Fills the buffer with bytes from the socket input stream. Blocks if asked to read more bytes than available.

Parameters
bufferThe buffer.
sizeThe size of the buffer.
Returns
The actual number of bytes read.

Implements InputStream.

Examples
testsuite/echod.cpp, testsuite/ftp.cpp, and testsuite/ping.cpp.

◆ setKeepAlive()

void StreamSocket::setKeepAlive ( bool  value)
inline

Sets the keep alive flag of this socket.

◆ setLinger()

void StreamSocket::setLinger ( int  seconds)
inline

Sets the linger interval. Negative time disables the linger.

◆ setNonBlocking()

void StreamSocket::setNonBlocking ( bool  value)
inline

Sets the blocking mode of the socket.

◆ setReceiveBufferSize()

void StreamSocket::setReceiveBufferSize ( int  size)
inline

Sets the size of the receive buffer.

Examples
testsuite/ping.cpp.

◆ setReuseAddress()

void StreamSocket::setReuseAddress ( bool  value)
inline

Sets the local address reuse flag of this socket.

◆ setSendBufferSize()

void StreamSocket::setSendBufferSize ( int  size)
inline

Sets the size of the send buffer.

Examples
testsuite/ping.cpp.

◆ setTcpNoDelay()

void StreamSocket::setTcpNoDelay ( bool  value)
inline

Disables/enables the Nagle's algorithm.

Examples
testsuite/ping.cpp.

◆ setTimeToLive()

void StreamSocket::setTimeToLive ( unsigned int  value)
inline

Sets the time to live (TTL) value.

◆ shutdownInputStream()

void StreamSocket::shutdownInputStream ( )
inline

Disables the input stream for this socket.

◆ shutdownOutputStream()

void StreamSocket::shutdownOutputStream ( )
inline

Disables the output stream for this socket.

Examples
testsuite/echod.cpp, and testsuite/http.cpp.

◆ skip()

unsigned int StreamSocket::skip ( unsigned int  count)
inlinevirtual

Skips the specified number of bytes.

Implements InputStream.

◆ wait() [1/2]

void StreamSocket::wait ( ) const
inlinevirtual

Blocking wait for input to become available.

Implements InputStream.

Examples
testsuite/echod.cpp, testsuite/http.cpp, and testsuite/ping.cpp.

◆ wait() [2/2]

bool StreamSocket::wait ( unsigned int  timeout) const
inlinevirtual

Waits for input to become available.

Parameters
timeoutThe timeout periode in microseconds.
Returns
True, if data is available. False, if the timeout periode expired.

Implements InputStream.

◆ write() [1/2]

unsigned int Socket::write

Writes bytes in buffer to stream.

Parameters
bufferThe buffer containing the bytes to be written.
sizeThe number of bytes to be written.
nonblockingSelect nonblocking mode.
Returns
The actual number of bytes written.

◆ write() [2/2]

unsigned int StreamSocket::write ( const uint8 *  buffer,
unsigned int  size,
bool  nonblocking = false 
)
inlinevirtual

Writes bytes in buffer to stream.

Parameters
bufferThe buffer containing the bytes to be written.
sizeThe number of bytes to be written.
Returns
The actual number of bytes written.

Implements OutputStream.

Examples
testsuite/echod.cpp, and testsuite/ping.cpp.