Home | C-Bits Package Documentation | Project Page |
#include <Socket.h>
Inheritance diagram for cbits::Socket:
Public Methods | |
Socket (const std::string &host, const int port, const bool now=true) throw (SocketException) | |
Construct a socket that may be used to connect to the application executing on the specified host, bound to the specified port. More... | |
Socket (const char *host, const int port, const bool now=true) throw (SocketException) | |
Socket (const int port, const bool now=true) throw (SocketException) | |
virtual | ~Socket () |
Drops the network connection and destroys the Socket instance. More... | |
virtual void | connect () throw (SocketException) |
Complete (establish) the network connection. More... | |
virtual const ssize_t | readLine (std::string &buffer, const std::string &delim, int *const error=0, const long int msec=0) throw ( SocketException ) |
Read from the network connection until a specified delimiter string is encountered. More... | |
virtual const ssize_t | read (char *const buffer, const int buflen, int *const error=0, const long msec=0) throw ( SocketException ) |
Read data from the network connection into a user-supplied buffer. More... | |
virtual const ssize_t | receive (char *const buffer, const int buflen, const long int msec=0, int *const error=0) throw ( SocketException ) |
Synonym for cbits::Socket::read with the error and msec argument positions reversed. More... | |
virtual const ssize_t | write (const char *buffer, const long buflen, int *const error=0, const long int msec=0) throw ( SocketException ) |
Write a buffer of data to the network connection. More... | |
virtual const ssize_t | send (const char *buffer, const long buflen, const long int msec=0, int *const error=0) throw ( SocketException ) |
Synonym for cbits::Socket::write with the error and msec argument positions reversed. More... | |
const socket_t | getHandle () const |
Get a handle to the underlying BSD socket connection. More... | |
std::iostream & | get_iostream () throw (SocketException) |
Get a std::iostream instance that can be used to write to or read from the socket. More... | |
const struct sockaddr_in & | get_addr () const |
Access the socket address that this cbits::Socket is bound to. More... | |
virtual const bool | isError () const |
Check the error status of thenetwork connection. More... | |
const std::string & | getError () const |
Access a human readable description of the socket error condition, if any. More... | |
virtual const bool | isReadable (long int *msec=0, int *const error=0) const |
Determine if the socket has data. More... | |
virtual const bool | isWritable (long int *msec=0, int *const error=0) const |
Determine if the socket can accept data. More... | |
operator std::ostream & () | |
Type conversion operator for when cbits::Socket needs to be used as a std::ostream instance. More... | |
operator std::istream & () | |
Type conversion operator for when cbits::Socket needs to be used as a std::istream instance. More... | |
virtual const bool | set_blocking_on () |
Configure the network connection such that calls to read data will block if none is available and calls to write data will block if the network connection can not immediately accept the data. More... | |
virtual const bool | set_blocking_off () |
set_blocking_off. More... | |
virtual const int | getSockOpt (const int level, const int op_name, char *buffer, int *buflen) const |
Access the value of a socket option. More... | |
std::ostream * | getLogger () const |
Access this Socket's debug logging stream. More... | |
std::ostream * | getLocalLogger () const |
Access this Sockets individual logging stream, set via cbits::setLocalLogger, if any. More... | |
void | setLocalLogger (std::ostream *os) const |
Set a instance debug logging stream for this Socket. More... | |
void | closeLocalLogger () const |
Close this Socket's individually set debug log stream, set via cbits::Socket::setLocalLogger, if any. More... | |
Static Public Methods | |
Socket * | create (const socket_t s) |
Wrap a pre-existing low-level BSD socket in a cbits::Socket. More... | |
std::ostream * | getGlobalLogger () |
Access the global debug logging stream for all Socket instances that don't have an individual logging stream. More... | |
void | setGlobalLogger (std::ostream *os) |
Set a global debug logging stream to be used by all Socket instances. More... | |
void | closeGlobalLogger () |
Close the globally set debug log stream, set via cbits::Socket::setGlobalLogger, if any. More... | |
Protected Methods | |
Socket () | |
Protected default constructor. More... | |
const bool | isAvailable (fd_set *rd, fd_set *wr, long int *msec=0, int *error=0) const |
Use the 'select' system call to determine if any of a set of file descriptors are available for reading or writing. More... | |
Protected Attributes | |
socket_t | _sockfd |
The BSD socket descriptor. More... | |
sockaddr_in | _host |
Address of remote host. More... | |
int | _port |
Port of remote host application. More... | |
std::iostream * | _iostream |
<iostream> rep of this Socket. More... | |
bool | _error |
Last error that occurred. More... | |
std::string | _errstr |
Human readable descripton of _error. More... | |
bool | _is_connected |
true if socket is connected. More... | |
bool | _is_bound |
std::ostream * | _llogr |
instance-scoped logging stream. More... | |
Static Protected Attributes | |
std::ostream * | _glogr = 0 |
class-scoped logging stream. More... | |
Private Methods | |
void | set_bound (const bool val) |
void | invalid_host (const char *) const throw ( SocketException ) |
Convenicne method to throws an exception if an invalid host is specified in a cbits::Socket constructor. More... | |
socket_t | make_socket (const char *name, const int port, const bool now) throw ( SocketException ) |
Convenience method to instantiate a BSD socket. More... | |
unsigned long | resolve_name (const char *) |
Convenience method to convert a host name into an IP address. More... | |
void | bind_address (struct sockaddr_in *inout_address, unsigned long in_IP, const int in_port) |
Convenience method to bind an IP address to a BSD socket endpoint. More... | |
void | ignore_epipe (const bool) |
Convenience method to install or de-install signal handler for SIGPIPE signal.. More... | |
Socket (const Socket &) | |
Socket & | operator= (const Socket &) |
Private Attributes | |
CriticalSection | _write_csect |
Critical section used to disallow more than one thread from writing to the socket at a time. More... | |
CriticalSection | _read_csect |
Critical section used to disallow more than one thread from reading data from the socket at a time. More... | |
CriticalSection | _csect |
Critical section used so synchronize non-read/write specific sections of code. More... | |
Friends | |
class | ServerSocket |
Must be able to set _is_bound state. More... |
It may be directly instantiated in order to establish a network connection to a sever application running on the same or a different host.
Instances of this class are also manufactured by a cbits::ServerSocket as it accepts network connections remote clients, each produced cbits::Socket instance representing a socket connection to a specific client.
Sockets are byte-stream oriented. As a byte is written to a Socket endpoint, it becomes available to be read on the mate Socket endpoint in the remote process. Structured data has to be serialized into a sequence of bytes by the application code as it is written to the Socket and reconstructed by the application reading the data from the other Socket endpoint.
Sockets are full duplex network connections. Data may be transmitted in both directions simultaneously and independently; both Socket endpoints may be used for reading and writing.
The primary Socket methods for reading and writing byte data are cbits:Socket::read and cbits::Socket::write. However, as a convenience, cbits::Socket supports std::iostream semantics via type conversion operators that allow a Socket instance to be used wherever a std::ostream& or std:istream& is used.
|
Construct a socket that may be used to connect to the application executing on the specified host, bound to the specified port.
|
|
|
|
|
|
Drops the network connection and destroys the Socket instance.
|
|
Protected default constructor.
|
|
|
|
Convenience method to bind an IP address to a BSD socket endpoint.
|
|
Close the globally set debug log stream, set via cbits::Socket::setGlobalLogger, if any. Since the output stream supplied to cbits::Socket::setGlobalLogger is externally created, the output stream itself is not closed by this action. |
|
Close this Socket's individually set debug log stream, set via cbits::Socket::setLocalLogger, if any. Since the output stream supplied to cbits::Socket::setLocalLogger is externally created, the output stream itself is not closed by this action. |
|
Complete (establish) the network connection.
Reimplemented in cbits::SSLSocket. |
|
Wrap a pre-existing low-level BSD socket in a cbits::Socket.
|
|
Access the socket address that this cbits::Socket is bound to.
|
|
Get a std::iostream instance that can be used to write to or read from the socket. All of the rules related to use of a std::iostream instance apply, including the need to check the stream flags for errors.
|
|
Access a human readable description of the socket error condition, if any. @Returns A non-empty string if an error exists on the socket; otherwise an empty string is returned. |
|
Access the global debug logging stream for all Socket instances that don't have an individual logging stream. This only accesses the logging stream that is used by Socket instances that haven't been individually configured with their own logging stream via cbits::Socket::setLocalLogger.
|
|
Get a handle to the underlying BSD socket connection.
|
|
Access this Sockets individual logging stream, set via cbits::setLocalLogger, if any.
|
|
Access this Socket's debug logging stream. A pointer to a stream is always returned. Logging statements that use this method should perform runtime checks to determine if application debug logging is enabled.
|
|
Access the value of a socket option.
|
|
Convenience method to install or de-install signal handler for SIGPIPE signal.. A SIGPIPE is generated when data is written to a socket that has been disconnected at the remote endpoint.
|
|
Convenicne method to throws an exception if an invalid host is specified in a cbits::Socket constructor.
|
|
Use the 'select' system call to determine if any of a set of file descriptors are available for reading or writing.
|
|
Check the error status of thenetwork connection. Generally, any error on a cbits::Socket should invalidate it.
|
|
Determine if the socket has data. Arguments: ----------- error - optional pointer to an error flag that is updated to a non-zero value if an error occurs during the call. Returns: ------------ true if the socket is readble (as determined by 'select'); otherwise, false. Exceptions: ----------- none.
|
|
Determine if the socket can accept data.
|
|
Convenience method to instantiate a BSD socket.
|
|
Type conversion operator for when cbits::Socket needs to be used as a std::istream instance.
|
|
Type conversion operator for when cbits::Socket needs to be used as a std::ostream instance.
|
|
|
|
Read data from the network connection into a user-supplied buffer.
Reimplemented in cbits::SSLSocket. |
|
Read from the network connection until a specified delimiter string is encountered.
For instance, readLine("\r set to the specific 'errno' value returned by the OS.
Reimplemented in cbits::SSLSocket. |
|
Synonym for cbits::Socket::read with the error and msec argument positions reversed.
Reimplemented in cbits::SSLSocket. |
|
Convenience method to convert a host name into an IP address.
|
|
Synonym for cbits::Socket::write with the error and msec argument positions reversed.
|
|
set_blocking_off. Configure the network connection such that calls to read data will not block if none is available and calls to write data will not block if the network connection can not immediately accept the data.
|
|
Configure the network connection such that calls to read data will block if none is available and calls to write data will block if the network connection can not immediately accept the data.
|
|
|
|
Set a global debug logging stream to be used by all Socket instances. This does not override a Socket instance's specific logging stream if any. This method is a useful way to provide a default logging stream to all Socket instances, without having to configure each instance separately, but still be able to set the logging stream for specific Socket instances. |
|
Set a instance debug logging stream for this Socket.
|
|
Write a buffer of data to the network connection.
Reimplemented in cbits::SSLSocket. |
|
Must be able to set _is_bound state.
|
|
Critical section used so synchronize non-read/write specific sections of code.
Reimplemented in cbits::SSLSocket. |
|
Last error that occurred.
|
|
Human readable descripton of _error.
|
|
class-scoped logging stream.
|
|
Address of remote host.
|
|
<iostream> rep of this Socket.
|
|
|
|
true if socket is connected.
|
|
instance-scoped logging stream.
|
|
Port of remote host application.
|
|
Critical section used to disallow more than one thread from reading data from the socket at a time.
Reimplemented in cbits::SSLSocket. |
|
The BSD socket descriptor.
|
|
Critical section used to disallow more than one thread from writing to the socket at a time.
Reimplemented in cbits::SSLSocket. |
|
|