Library: Net
Package: Sockets
Header: Poco/Net/Socket.h
Socket is the common base class for StreamSocket, ServerSocket, DatagramSocket and other socket classes.
It provides operations common to all socket types.
Known Derived Classes: DatagramSocket, DialogSocket, ICMPSocket, MulticastSocket, RawSocket, ServerSocket, StreamSocket, SecureServerSocket, SecureStreamSocket
Member Functions: address, available, close, getBlocking, getKeepAlive, getLinger, getNoDelay, getOOBInline, getOption, getReceiveBufferSize, getReceiveTimeout, getReuseAddress, getReusePort, getSendBufferSize, getSendTimeout, impl, operator !=, operator <, operator <=, operator =, operator ==, operator >, operator >=, peerAddress, poll, secure, select, setBlocking, setKeepAlive, setLinger, setNoDelay, setOOBInline, setOption, setReceiveBufferSize, setReceiveTimeout, setReuseAddress, setReusePort, setSendBufferSize, setSendTimeout, sockfd, supportsIPv4, supportsIPv6
typedef std::vector < Socket > SocketList;
SELECT_READ = 1
SELECT_WRITE = 2
SELECT_ERROR = 4
Socket();
Creates an uninitialized socket.
Socket(
const Socket & socket
);
Copy constructor.
Attaches the SocketImpl from the other socket and increments the reference count of the SocketImpl.
Socket(
SocketImpl * pImpl
);
Creates the Socket and attaches the given SocketImpl. The socket takes owership of the SocketImpl.
virtual ~Socket();
Destroys the Socket and releases the SocketImpl.
SocketAddress address() const;
Returns the IP address and port number of the socket.
int available() const;
Returns the number of bytes available that can be read without causing the socket to block.
void close();
Closes the socket.
bool getBlocking() const;
Returns the blocking mode of the socket. This method will only work if the blocking modes of the socket are changed via the setBlocking method!
bool getKeepAlive() const;
Returns the value of the SO_KEEPALIVE socket option.
void getLinger(
bool & on,
int & seconds
) const;
Returns the value of the SO_LINGER socket option.
bool getNoDelay() const;
Returns the value of the TCP_NODELAY socket option.
bool getOOBInline() const;
Returns the value of the SO_OOBINLINE socket option.
void getOption(
int level,
int option,
int & value
) const;
Returns the value of the socket option specified by level and option.
void getOption(
int level,
int option,
unsigned & value
) const;
Returns the value of the socket option specified by level and option.
void getOption(
int level,
int option,
unsigned char & value
) const;
Returns the value of the socket option specified by level and option.
void getOption(
int level,
int option,
Poco::Timespan & value
) const;
Returns the value of the socket option specified by level and option.
void getOption(
int level,
int option,
IPAddress & value
) const;
Returns the value of the socket option specified by level and option.
int getReceiveBufferSize() const;
Returns the size of the receive buffer.
The returned value may be different than the value previously set with setReceiveBufferSize(), as the system is free to adjust the value.
Poco::Timespan getReceiveTimeout() const;
Returns the receive timeout for the socket.
The returned timeout may be different than the timeout previously set with getReceiveTimeout(), as the system is free to adjust the value.
bool getReuseAddress() const;
Returns the value of the SO_REUSEADDR socket option.
bool getReusePort() const;
Returns the value of the SO_REUSEPORT socket option.
Returns false if the socket implementation does not support SO_REUSEPORT.
int getSendBufferSize() const;
Returns the size of the send buffer.
The returned value may be different than the value previously set with setSendBufferSize(), as the system is free to adjust the value.
Poco::Timespan getSendTimeout() const;
Returns the send timeout for the socket.
The returned timeout may be different than the timeout previously set with setSendTimeout(), as the system is free to adjust the value.
SocketImpl * impl() const;
Returns the SocketImpl for this socket.
bool operator != (
const Socket & socket
) const;
Returns false if both sockets share the same SocketImpl, true otherwise.
bool operator < (
const Socket & socket
) const;
Compares the SocketImpl pointers.
bool operator <= (
const Socket & socket
) const;
Compares the SocketImpl pointers.
Socket & operator = (
const Socket & socket
);
Assignment operator.
Releases the socket's SocketImpl and attaches the SocketImpl from the other socket and increments the reference count of the SocketImpl.
bool operator == (
const Socket & socket
) const;
Returns true if both sockets share the same SocketImpl, false otherwise.
bool operator > (
const Socket & socket
) const;
Compares the SocketImpl pointers.
bool operator >= (
const Socket & socket
) const;
Compares the SocketImpl pointers.
SocketAddress peerAddress() const;
Returns the IP address and port number of the peer socket.
bool poll(
const Poco::Timespan & timeout,
int mode
) const;
Determines the status of the socket, using a call to select().
The mode argument is constructed by combining the values of the SelectMode enumeration.
Returns true if the next operation corresponding to mode will not block, false otherwise.
bool secure() const;
Returns true if and only if the socket's connection is secure (using SSL or TLS).
static int select(
SocketList & readList,
SocketList & writeList,
SocketList & exceptList,
const Poco::Timespan & timeout
);
Determines the status of one or more sockets, using a call to select().
ReadList contains the list of sockets which should be checked for readability.
WriteList contains the list of sockets which should be checked for writeability.
ExceptList contains a list of sockets which should be checked for a pending error.
Returns the number of sockets ready.
After return,
If the total number of sockets passed in readList, writeList and exceptList is zero, select() will return immediately and the return value will be 0.
If one of the sockets passed to select() is closed while select() runs, select will return immediately. However, the closed socket will not be included in any list. In this case, the return value may be greater than the sum of all sockets in all list.
void setBlocking(
bool flag
);
Sets the socket in blocking mode if flag is true, disables blocking mode if flag is false.
void setKeepAlive(
bool flag
);
Sets the value of the SO_KEEPALIVE socket option.
void setLinger(
bool on,
int seconds
);
Sets the value of the SO_LINGER socket option.
void setNoDelay(
bool flag
);
Sets the value of the TCP_NODELAY socket option.
void setOOBInline(
bool flag
);
Sets the value of the SO_OOBINLINE socket option.
void setOption(
int level,
int option,
int value
);
Sets the socket option specified by level and option to the given integer value.
void setOption(
int level,
int option,
unsigned value
);
Sets the socket option specified by level and option to the given integer value.
void setOption(
int level,
int option,
unsigned char value
);
Sets the socket option specified by level and option to the given integer value.
void setOption(
int level,
int option,
const Poco::Timespan & value
);
Sets the socket option specified by level and option to the given time value.
void setOption(
int level,
int option,
const IPAddress & value
);
Sets the socket option specified by level and option to the given time value.
void setReceiveBufferSize(
int size
);
Sets the size of the receive buffer.
void setReceiveTimeout(
const Poco::Timespan & timeout
);
Sets the send timeout for the socket.
On systems that do not support SO_RCVTIMEO, a workaround using poll() is provided.
void setReuseAddress(
bool flag
);
Sets the value of the SO_REUSEADDR socket option.
void setReusePort(
bool flag
);
Sets the value of the SO_REUSEPORT socket option. Does nothing if the socket implementation does not support SO_REUSEPORT.
void setSendBufferSize(
int size
);
Sets the size of the send buffer.
void setSendTimeout(
const Poco::Timespan & timeout
);
Sets the send timeout for the socket.
static bool supportsIPv4();
Returns true if the system supports IPv4.
static bool supportsIPv6();
Returns true if the system supports IPv6.
int sockfd() const;
Returns the socket descriptor for this socket.