ccuty  1.0
 All Classes Namespaces Files Functions Enumerations Macros Pages
Public Types | Public Member Functions | List of all members
ccuty::Socket Class Reference

TCP/IP or UDP/Datagram socket. More...

Public Types

enum  Errors
 Socket errors. More...
 

Public Member Functions

 Socket (int type=SOCK_STREAM)
 Creates a new Socket. More...
 
 Socket (int type, int sockfd)
 Creates a Socket object from an existing socket file descriptor.
 
virtual ~Socket ()
 Destructor (closes the socket).
 
virtual int bind (int port)
 Assign the socket to a local address. More...
 
virtual int bind (const std::string &host, int port)
 Assign the socket to an address. More...
 
virtual int connect (const std::string &host, int port)
 Connect the socket to an address. More...
 
virtual int close ()
 Closes the socket. More...
 
bool isClosed () const
 Return true if the socket has been closed.
 
int descriptor ()
 Return the Unix descriptor of the socket.
 
ssize_t send (const void *buf, size_t len, int flags=0)
 Send data to a connected socket. More...
 
ssize_t receive (void *buf, size_t len, int flags=0)
 Receive data from a connected socket. More...
 
ssize_t sendTo (const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
 Send data to a datagram socket. More...
 
ssize_t receiveFrom (void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
 Receive data from datagram socket. More...
 
virtual void shutdownInput ()
 Disable further receive operations.
 
virtual void shutdownOutput ()
 Disable further send operations.
 
int setReceiveBufferSize (int size)
 Set the size of the TCP/IP input buffer.
 
int setReuseAddress (bool)
 Enable/disable the SO_REUSEADDR socket option.
 
int setSendBufferSize (int size)
 Set the size of the TCP/IP output buffer.
 
int setSoLinger (bool, int linger)
 Enable/disable SO_LINGER with the specified linger time in seconds.
 
int setSoTimeout (int timeout)
 Enable/disable SO_TIMEOUT with the specified timeout (in milliseconds).
 
int setTcpNoDelay (bool)
 Enable/disable TCP_NODELAY (turns on/off TCP coalescence).
 
int getReceiveBufferSize () const
 Return the size of the TCP/IP input buffer.
 
bool getReuseAddress () const
 Return SO_REUSEADDR state.
 
int getSendBufferSize () const
 Return the size of the TCP/IP output buffer.
 
bool getSoLinger (int &linger) const
 Return SO_LINGER state and the specified linger time in seconds.
 
int getSoTimeout () const
 Return SO_TIMEOUT value.
 
bool getTcpNoDelay () const
 Return TCP_NODELAY state.
 
virtual int setLocalAddress (struct sockaddr_in &addr, int port)
 Initialize a local INET4 address, returns 0 on success, -1 otherwise.
 
virtual int setAddress (struct sockaddr_in &addr, const std::string &host, int port)
 Initialize a remote INET4 address, returns 0 on success, -1 otherwise.
 

Detailed Description

TCP/IP or UDP/Datagram socket.

This class encapsulates a TCP/IP or UDP/Datagram socket. AF_INET connections following the IPv4 Internet protocol are supported.

Note
  • The ServerSocket class should be used on the server side.
  • SIGPIPE signals are ignored when using Linux, BSD or MACOSX.
  • TCP/IP sockets do not preserve record boundaries but class SocketBuffer solves this problem.

Member Enumeration Documentation

Socket errors.

  • Socket::Failed (-1): connection error (could not connect, could not bind, etc.)
  • Socket::InvalidSocket (-2): invalid socket or wrong socket type
  • Socket::UnknownHost (-3): could not reach host

Constructor & Destructor Documentation

ccuty::Socket::Socket ( int  type = SOCK_STREAM)

Creates a new Socket.

Creates a AF_INET socket using the IPv4 Internet protocol. Type can be:

  • SOCK_STREAM (the default) for TCP/IP connected stream sockets
  • SOCK_DGRAM for UDP/datagram sockets

Member Function Documentation

int ccuty::Socket::bind ( int  port)
virtual

Assign the socket to a local address.

Typically used for UDP/Datagram sockets, see Unix 'bind' system call for details.

Returns
0 on success or a negative value on error, which is one of Socket::Errors
int ccuty::Socket::bind ( const std::string &  host,
int  port 
)
virtual

Assign the socket to an address.

Typically used for UDP/Datagram sockets, see Unix 'bind' system call for details.

Returns
0 on success or a negative value on error, which is one of Socket::Errors
int ccuty::Socket::connect ( const std::string &  host,
int  port 
)
virtual

Connect the socket to an address.

Typically used for TCP/IP sockets on the client side, see Unix 'connect' system call for details.

Returns
0 on success or a negative value on error which is one of Socket::Errors
int ccuty::Socket::close ( )
virtual

Closes the socket.

Returns
0 on success and -1 on error.
ssize_t ccuty::Socket::send ( const void *  buf,
size_t  len,
int  flags = 0 
)
inline

Send data to a connected socket.

Sends len bytes to a TCP/IP socket using the Unix 'send' function.

Returns
the number of bytes that were sent or:
  • len is 0 or shutdownInput() was called on the other side,
  • Socket::Failed (-1): a connection error occured.
Note
TCP/IP sockets do not preserve record boundaries but SocketBuffer solves this problem.
ssize_t ccuty::Socket::receive ( void *  buf,
size_t  len,
int  flags = 0 
)
inline

Receive data from a connected socket.

Reads at most len bytes from a TCP/IP socket using the Unix 'recv' function. By default, this function blocks the caller until data is present.

Returns
the number of bytes that were received or:
  • 0: len is 0 or shutdownOutput() was called on the other side,
  • Socket::Failed (-1): a connection error occured.
Note
TCP/IP sockets do not preserve record boundaries but SocketBuffer solves this problem.
ssize_t ccuty::Socket::sendTo ( const void *  buf,
size_t  len,
int  flags,
const struct sockaddr *  dest_addr,
socklen_t  addrlen 
)
inline

Send data to a datagram socket.

Sends len bytes to a datagram socket using the Unix 'sendto' function.

Returns
the number of bytes that were sent or Socket::Failed (-1) if an error occurred.
ssize_t ccuty::Socket::receiveFrom ( void *  buf,
size_t  len,
int  flags,
struct sockaddr *  src_addr,
socklen_t *  addrlen 
)
inline

Receive data from datagram socket.

Reads at most len bytes from a datagram socket using the Unix 'recvfrom' function. By default, this function blocks the caller until data is present.

Returns
the number of bytes which was received or Socket::Failed (-1) if an error occurred.

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