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. | |
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.
Socket errors.
ccuty::Socket::Socket | ( | int | type = SOCK_STREAM ) |
Creates a new Socket.
Creates a AF_INET socket using the IPv4 Internet protocol. Type can be:
|
virtual |
Assign the socket to a local address.
Typically used for UDP/Datagram sockets, see Unix 'bind' system call for details.
|
virtual |
Assign the socket to an address.
Typically used for UDP/Datagram sockets, see Unix 'bind' system call for details.
|
virtual |
Connect the socket to an address.
Typically used for TCP/IP sockets on the client side, see Unix 'connect' system call for details.
|
virtual |
Closes the socket.
|
inline |
Send data to a connected socket.
Sends len bytes to a TCP/IP socket using the Unix 'send' function.
|
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.
|
inline |
Send data to a datagram socket.
Sends len bytes to a datagram socket using the Unix 'sendto' function.
|
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.