activemq-cpp-3.6.0
decaf::net::Socket Class Reference

#include <src/main/decaf/net/Socket.h>

Inheritance diagram for decaf::net::Socket:

Public Member Functions

 Socket ()
 Creates an unconnected Socket using the set SocketImplFactory or if non is set than the default SockImpl type is created.
 Socket (SocketImpl *impl)
 Creates a Socket wrapping the provided SocketImpl instance, this Socket is considered unconnected.
 Socket (const InetAddress *address, int port)
 Creates a new Socket instance and connects it to the given address and port.
 Socket (const InetAddress *address, int port, const InetAddress *localAddress, int localPort)
 Creates a new Socket instance and connects it to the given address and port.
 Socket (const std::string &host, int port)
 Creates a new Socket instance and connects it to the given host and port.
 Socket (const std::string &host, int port, const InetAddress *localAddress, int localPort)
 Creates a new Socket instance and connects it to the given host and port.
virtual ~Socket ()
virtual void bind (const std::string &ipaddress, int port)
 Binds this Socket to the given local address and port.
virtual void close ()
 Closes the Socket.
virtual void connect (const std::string &host, int port)
 Connects to the specified destination.
virtual void connect (const std::string &host, int port, int timeout)
 Connects to the specified destination, with a specified timeout value.
bool isConnected () const
 Indicates whether or not this socket is connected to am end point.
bool isClosed () const
bool isBound () const
bool isInputShutdown () const
bool isOutputShutdown () const
virtual decaf::io::InputStreamgetInputStream ()
 Gets the InputStream for this socket if its connected.
virtual decaf::io::OutputStreamgetOutputStream ()
 Gets the OutputStream for this socket if it is connected.
int getPort () const
 Gets the on the remote host this Socket is connected to.
int getLocalPort () const
 Gets the local port the socket is bound to.
std::string getInetAddress () const
 Returns the address to which the socket is connected.
std::string getLocalAddress () const
 Gets the local address to which the socket is bound.
virtual void shutdownInput ()
 Shuts down the InputStream for this socket essentially marking it as EOF.
virtual void shutdownOutput ()
 Shuts down the OutputStream for this socket, any data already written to the socket will be sent, any further calls to OuputStream::write will throw an IOException.
virtual int getSoLinger () const
 Gets the linger time for the socket, SO_LINGER.
virtual void setSoLinger (bool state, int timeout)
 Sets the linger time (SO_LINGER) using a specified time value, this limits of this value are platform specific.
virtual bool getKeepAlive () const
 Gets the keep alive flag for this socket, SO_KEEPALIVE.
virtual void setKeepAlive (bool keepAlive)
 Enables/disables the keep alive flag for this socket, SO_KEEPALIVE.
virtual int getReceiveBufferSize () const
 Gets the receive buffer size for this socket, SO_RCVBUF.
virtual void setReceiveBufferSize (int size)
 Sets the receive buffer size for this socket, SO_RCVBUF.
virtual bool getReuseAddress () const
 Gets the reuse address flag, SO_REUSEADDR.
virtual void setReuseAddress (bool reuse)
 Sets the reuse address flag, SO_REUSEADDR.
virtual int getSendBufferSize () const
 Gets the send buffer size for this socket, SO_SNDBUF, this value is used by the platform socket to buffer data written to the socket.
virtual void setSendBufferSize (int size)
 Gets the send buffer size for this socket, SO_SNDBUF, this value is used by the platform socket to buffer data written to the socket.
virtual int getSoTimeout () const
 Gets the timeout for socket operations, SO_TIMEOUT.
virtual void setSoTimeout (int timeout)
 Sets the timeout for socket operations, SO_TIMEOUT.
virtual bool getTcpNoDelay () const
 Gets the Status of the TCP_NODELAY setting for this socket.
virtual void setTcpNoDelay (bool value)
 Sets the Status of the TCP_NODELAY param for this socket., this setting is used to disable or enable Nagle's algorithm on the Socket.
virtual int getTrafficClass () const
 Gets the Traffic Class setting for this Socket, sometimes referred to as Type of Service setting.
virtual void setTrafficClass (int value)
 Gets the Traffic Class setting for this Socket, sometimes referred to as Type of Service setting.
virtual bool getOOBInline () const
 Gets the value of the OOBINLINE for this socket.
virtual void setOOBInline (bool value)
 Sets the value of the OOBINLINE for this socket, by default this option is disabled.
virtual void sendUrgentData (int data)
 Sends on byte of urgent data to the Socket.
virtual std::string toString () const
- Public Member Functions inherited from decaf::io::Closeable
virtual ~Closeable ()

Static Public Member Functions

static void setSocketImplFactory (SocketImplFactory *factory)
 Sets the instance of a SocketImplFactory that the Socket class should use when new instances of this class are created.

Protected Member Functions

void accepted ()
void initSocketImpl (const std::string &address, int port, const InetAddress *localAddress, int localPort)
void checkClosed () const
void ensureCreated () const

Protected Attributes

SocketImplimpl

Friends

class ServerSocket

Detailed Description

Since
1.0

Constructor & Destructor Documentation

decaf::net::Socket::Socket ( )

Creates an unconnected Socket using the set SocketImplFactory or if non is set than the default SockImpl type is created.

decaf::net::Socket::Socket ( SocketImpl impl)

Creates a Socket wrapping the provided SocketImpl instance, this Socket is considered unconnected.

The Socket class takes ownership of this SocketImpl pointer and will delete it when the Socket class is destroyed.

Parameters
implThe SocketImpl instance to wrap.
Exceptions
NullPointerExceptionif the passed SocketImpl is Null.
decaf::net::Socket::Socket ( const InetAddress address,
int  port 
)

Creates a new Socket instance and connects it to the given address and port.

If there is a SocketImplFactory set then the SokcetImpl is created using the factory otherwise the default Socket implementation is used.

If the host parameter is empty then the loop back address is used.

Parameters
addressThe address to connect to.
portThe port number to connect to [0...65535]
Exceptions
UnknownHostExceptionif the host cannot be resolved.
IOExceptionif an I/O error occurs while connecting the Socket.
NullPointerExceptionif the InetAddress instance in NULL.
IllegalArgumentExceptionif the port if not in range [0...65535]
decaf::net::Socket::Socket ( const InetAddress address,
int  port,
const InetAddress localAddress,
int  localPort 
)

Creates a new Socket instance and connects it to the given address and port.

If there is a SocketImplFactory set then the SokcetImpl is created using the factory otherwise the default Socket implementation is used. The Socket will also bind to the local address and port specified.

Parameters
addressThe address to connect to.
portThe port number to connect to [0...65535]
localAddressThe IP address on the local machine to bind to.
localPortThe port on the local machine to bind to.
Exceptions
UnknownHostExceptionif the host cannot be resolved.
IOExceptionif an I/O error occurs while connecting the Socket.
NullPointerExceptionif the InetAddress instance in NULL.
IllegalArgumentExceptionif the port if not in range [0...65535]
decaf::net::Socket::Socket ( const std::string &  host,
int  port 
)

Creates a new Socket instance and connects it to the given host and port.

If there is a SocketImplFactory set then the SokcetImpl is created using the factory otherwise the default Socket implementation is used.

If the host parameter is empty then the loop back address is used.

Parameters
hostThe host name or IP address to connect to, empty string means loopback.
portThe port number to connect to [0...65535]
Exceptions
UnknownHostExceptionif the host cannot be resolved.
IOExceptionif an I/O error occurs while connecting the Socket.
IllegalArgumentExceptionif the port if not in range [0...65535]
decaf::net::Socket::Socket ( const std::string &  host,
int  port,
const InetAddress localAddress,
int  localPort 
)

Creates a new Socket instance and connects it to the given host and port.

If there is a SocketImplFactory set then the SokcetImpl is created using the factory otherwise the default Socket implementation is used.

If the host parameter is empty then the loop back address is used.

Parameters
hostThe host name or IP address to connect to, empty string means loopback.
portThe port number to connect to [0...65535]
localAddressThe IP address on the local machine to bind to.
localPortThe port on the local machine to bind to.
Exceptions
UnknownHostExceptionif the host cannot be resolved.
IOExceptionif an I/O error occurs while connecting the Socket.
IllegalArgumentExceptionif the port if not in range [0...65535]
virtual decaf::net::Socket::~Socket ( )
virtual

Member Function Documentation

void decaf::net::Socket::accepted ( )
protected
virtual void decaf::net::Socket::bind ( const std::string &  ipaddress,
int  port 
)
virtual

Binds this Socket to the given local address and port.

If the SocketAddress value is NULL then the Socket will be bound to an available local address and port.

Parameters
ipaddressThe local address and port to bind the socket to.
portThe port on the local machine to bind to.
Exceptions
IOExceptionif an error occurs during the bind operation.
IllegalArgumentExceptionif the Socket can't process the subclass of SocketAddress that has been provided.
void decaf::net::Socket::checkClosed ( ) const
protected
virtual void decaf::net::Socket::close ( )
virtual

Closes the Socket.

Once closed a Socket cannot be connected or otherwise operated upon, a new Socket instance must be created.

Exceptions
IOExceptionif an I/O error occurs while closing the Socket.

Implements decaf::io::Closeable.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

virtual void decaf::net::Socket::connect ( const std::string &  host,
int  port 
)
virtual

Connects to the specified destination.

Parameters
hostThe host name or IP address of the remote host to connect to.
portThe port on the remote host to connect to.
Exceptions
IOExceptionThrown if a failure occurred in the connect.
IllegalArguementExceptionif the timeout value is negative or the endpoint is invalid.
virtual void decaf::net::Socket::connect ( const std::string &  host,
int  port,
int  timeout 
)
virtual

Connects to the specified destination, with a specified timeout value.

If a connection to the remote host is not established within the specified timeout interval than an SocketTimeoutException is thrown. A timeout value of zero is treated as an inifinite timeout.

Parameters
hostThe host name or IP address of the remote host to connect to.
portThe port on the remote host to connect to.
timeoutThe number of Milliseconds to wait before treating the connection as failed.
Exceptions
IOExceptionThrown if a failure occurred in the connect.
SocketTimeoutExceptionif the timeout for connection is exceeded.
IllegalArguementExceptionif the timeout value is negative or the endpoint is invalid.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

void decaf::net::Socket::ensureCreated ( ) const
protected
std::string decaf::net::Socket::getInetAddress ( ) const

Returns the address to which the socket is connected.

Returns
the remote IP address to which this socket is connected, or null if the socket is not connected.
virtual decaf::io::InputStream* decaf::net::Socket::getInputStream ( )
virtual

Gets the InputStream for this socket if its connected.

The pointer returned is the property of the associated Socket and should not be deleted by the caller.

When the returned InputStream is performing a blocking operation and the underlying connection is closed or otherwise broker the read calls will normally throw an exception to indicate the failure.

Closing the InputStream will also close the underlying Socket.

Returns
The InputStream for this socket.
Exceptions
IOExceptionif an error occurs during creation of the InputStream, also if the Socket is not connected or the input has been shutdown previously.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

virtual bool decaf::net::Socket::getKeepAlive ( ) const
virtual

Gets the keep alive flag for this socket, SO_KEEPALIVE.

Returns
true if keep alive is enabled for this socket.
Exceptions
SocketExceptionif the operation fails.
std::string decaf::net::Socket::getLocalAddress ( ) const

Gets the local address to which the socket is bound.

Returns
the local address to which the socket is bound or InetAddress.anyLocalAddress() if the socket is not bound yet.
int decaf::net::Socket::getLocalPort ( ) const

Gets the local port the socket is bound to.

Returns
the local port the socket was bound to, or -1 if the socket is not bound.
virtual bool decaf::net::Socket::getOOBInline ( ) const
virtual

Gets the value of the OOBINLINE for this socket.

Returns
true if OOBINLINE is enabled, false otherwise.
Exceptions
SocketExceptionif an error is encountered while performing this operation.
virtual decaf::io::OutputStream* decaf::net::Socket::getOutputStream ( )
virtual

Gets the OutputStream for this socket if it is connected.

The pointer returned is the property of the Socket instance and should not be deleted by the caller.

Closing the returned Socket will also close the underlying Socket.

Returns
the OutputStream for this socket.
Exceptions
IOExceptionif an error occurs during the creation of this OutputStream, or if the Socket is closed or the output has been shutdown previously.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

int decaf::net::Socket::getPort ( ) const

Gets the on the remote host this Socket is connected to.

Returns
the port on the remote host the socket is connected to, or 0 if not connected.
virtual int decaf::net::Socket::getReceiveBufferSize ( ) const
virtual

Gets the receive buffer size for this socket, SO_RCVBUF.

This is the buffer used by the underlying platform socket to buffer received data.

Returns
the receive buffer size in bytes.
Exceptions
SocketExceptionif the operation fails.
virtual bool decaf::net::Socket::getReuseAddress ( ) const
virtual

Gets the reuse address flag, SO_REUSEADDR.

Returns
True if the address can be reused.
Exceptions
SocketExceptionif the operation fails.
virtual int decaf::net::Socket::getSendBufferSize ( ) const
virtual

Gets the send buffer size for this socket, SO_SNDBUF, this value is used by the platform socket to buffer data written to the socket.

Returns
the size in bytes of the send buffer.
Exceptions
SocketExceptionif the operation fails.
virtual int decaf::net::Socket::getSoLinger ( ) const
virtual

Gets the linger time for the socket, SO_LINGER.

A return value of -1 indicates that the option is disabled.

Returns
The linger time in seconds.
Exceptions
SocketExceptionif the operation fails.
virtual int decaf::net::Socket::getSoTimeout ( ) const
virtual

Gets the timeout for socket operations, SO_TIMEOUT.

Returns
The timeout in milliseconds for socket operations.
Exceptions
SocketExceptionThrown if unable to retrieve the information.
virtual bool decaf::net::Socket::getTcpNoDelay ( ) const
virtual

Gets the Status of the TCP_NODELAY setting for this socket.

Returns
true if TCP_NODELAY is enabled for the socket.
Exceptions
SocketExceptionThrown if unable to set the information.
virtual int decaf::net::Socket::getTrafficClass ( ) const
virtual

Gets the Traffic Class setting for this Socket, sometimes referred to as Type of Service setting.

This setting is dependent on the underlying network implementation for the platform this Socket runs on and is not guaranteed to have any effect.

Refer to your platforms network documentation regarding support for this setting.

Returns
the bitset result of querying the traffic class setting.
Exceptions
SocketExceptionif an error is encountered while performing this operation.
void decaf::net::Socket::initSocketImpl ( const std::string &  address,
int  port,
const InetAddress localAddress,
int  localPort 
)
protected
bool decaf::net::Socket::isBound ( ) const
inline
Returns
true if this Socket has been bound to a Local address.
bool decaf::net::Socket::isClosed ( ) const
inline
Returns
true if the Socket has been closed.
bool decaf::net::Socket::isConnected ( ) const
inline

Indicates whether or not this socket is connected to am end point.

Returns
true if connected, false otherwise.
bool decaf::net::Socket::isInputShutdown ( ) const
inline
Returns
true if input on this Socket has been shutdown.
bool decaf::net::Socket::isOutputShutdown ( ) const
inline
Returns
true if output on this Socket has been shutdown.
virtual void decaf::net::Socket::sendUrgentData ( int  data)
virtual

Sends on byte of urgent data to the Socket.

Parameters
dataThe value to write as urgent data, only the lower eight bits are sent.
Exceptions
IOExceptionif an I/O error occurs while performing this operation.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

virtual void decaf::net::Socket::setKeepAlive ( bool  keepAlive)
virtual

Enables/disables the keep alive flag for this socket, SO_KEEPALIVE.

Parameters
keepAliveIf true, enables the flag.
Exceptions
SocketExceptionif the operation fails.
virtual void decaf::net::Socket::setOOBInline ( bool  value)
virtual

Sets the value of the OOBINLINE for this socket, by default this option is disabled.

If enabled the urgent data is read inline on the Socket's InputStream, no notification is give.

Returns
true if OOBINLINE is enabled, false otherwise.
Exceptions
SocketExceptionif an error is encountered while performing this operation.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

virtual void decaf::net::Socket::setReceiveBufferSize ( int  size)
virtual

Sets the receive buffer size for this socket, SO_RCVBUF.

Parameters
sizeNumber of bytes to set the receive buffer to.
Exceptions
SocketExceptionif the operation fails.
IllegalArgumentExceptionif the value is zero or negative.
virtual void decaf::net::Socket::setReuseAddress ( bool  reuse)
virtual

Sets the reuse address flag, SO_REUSEADDR.

Parameters
reuseIf true, sets the flag.
Exceptions
SocketExceptionif the operation fails.
virtual void decaf::net::Socket::setSendBufferSize ( int  size)
virtual

Gets the send buffer size for this socket, SO_SNDBUF, this value is used by the platform socket to buffer data written to the socket.

Parameters
sizeThe number of bytes to set the send buffer to, must be larger than zero.
Exceptions
SocketExceptionif the operation fails.
IllegalArgumentExceptionif the value is zero or negative.
static void decaf::net::Socket::setSocketImplFactory ( SocketImplFactory factory)
static

Sets the instance of a SocketImplFactory that the Socket class should use when new instances of this class are created.

This method is only allowed to be used once during the lifetime of the application.

Parameters
factoryThe instance of a SocketImplFactory to use when new Socket objects are created.
Exceptions
IOExceptionif an I/O error occurs while performing this operation.
SocketExceptionif this method has already been called with a valid factory.
virtual void decaf::net::Socket::setSoLinger ( bool  state,
int  timeout 
)
virtual

Sets the linger time (SO_LINGER) using a specified time value, this limits of this value are platform specific.

Parameters
stateThe state of SO_LINGER, true is on.
timeoutThe linger time in seconds, must be non-negative.
Exceptions
SocketExceptionif the operation fails.
IllegalArgumentExceptionif state is true and timeout is negative.
virtual void decaf::net::Socket::setSoTimeout ( int  timeout)
virtual

Sets the timeout for socket operations, SO_TIMEOUT.

A value of zero indicates that timeout is infinite for operations on this socket.

Parameters
timeoutThe timeout in milliseconds for socket operations.
Exceptions
SocketExceptionThrown if unable to set the information.
IllegalArgumentExceptionif the timeout value is negative.
virtual void decaf::net::Socket::setTcpNoDelay ( bool  value)
virtual

Sets the Status of the TCP_NODELAY param for this socket., this setting is used to disable or enable Nagle's algorithm on the Socket.

Parameters
valueThe setting for the socket's TCP_NODELAY option, true to enable.
Exceptions
SocketExceptionThrown if unable to set the information.
virtual void decaf::net::Socket::setTrafficClass ( int  value)
virtual

Gets the Traffic Class setting for this Socket, sometimes referred to as Type of Service setting.

This setting is dependent on the underlying network implementation for the platform this Socket runs on and is not guaranteed to have any effect.

Refer to your platforms network documentation regarding support for this setting.

Parameters
valueThe integer value representing the traffic class setting bitset.
Exceptions
SocketExceptionif an error is encountered while performing this operation.
IllegalArgumentExceptionif the value is not in the range [0..255].
virtual void decaf::net::Socket::shutdownInput ( )
virtual

Shuts down the InputStream for this socket essentially marking it as EOF.

The stream returns EOF for any calls to read after this method has been called.

Exceptions
IOExceptionif an I/O error occurs while performing this operation.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

virtual void decaf::net::Socket::shutdownOutput ( )
virtual

Shuts down the OutputStream for this socket, any data already written to the socket will be sent, any further calls to OuputStream::write will throw an IOException.

Exceptions
IOExceptionif an I/O error occurs while performing this operation.

Reimplemented in decaf::internal::net::ssl::openssl::OpenSSLSocket.

virtual std::string decaf::net::Socket::toString ( ) const
virtual
Returns
a string representing this Socket.

Friends And Related Function Documentation

friend class ServerSocket
friend

Field Documentation

SocketImpl* decaf::net::Socket::impl
mutableprotected

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