Library: Net
Package: Sockets
Header: Poco/Net/SocketImpl.h
Description
This class encapsulates the Berkeley sockets API.
Subclasses implement specific socket types like stream or datagram sockets.
You should not create any instances of this class.
Inheritance
Direct Base Classes: Poco::RefCountedObject
All Base Classes: Poco::RefCountedObject
Known Derived Classes: DatagramSocketImpl, ICMPSocketImpl, RawSocketImpl, ServerSocketImpl, StreamSocketImpl, WebSocketImpl
Member Summary
Member Functions: acceptConnection, address, available, bind, bind6, checkBrokenTimeout, close, connect, connectNB, error, fcntl, getBlocking, getBroadcast, getError, getKeepAlive, getLinger, getNoDelay, getOOBInline, getOption, getRawOption, getReceiveBufferSize, getReceiveTimeout, getReuseAddress, getReusePort, getSendBufferSize, getSendTimeout, init, initSocket, initialized, ioctl, lastError, listen, peerAddress, poll, receiveBytes, receiveFrom, reset, secure, sendBytes, sendTo, sendUrgent, setBlocking, setBroadcast, setKeepAlive, setLinger, setNoDelay, setOOBInline, setOption, setRawOption, setReceiveBufferSize, setReceiveTimeout, setReuseAddress, setReusePort, setSendBufferSize, setSendTimeout, shutdown, shutdownReceive, shutdownSend, socketError, sockfd, type, useFileDescriptor
Inherited Functions: duplicate, referenceCount, release
Enumerations
SelectMode
SELECT_READ = 1
SELECT_WRITE = 2
SELECT_ERROR = 4
Type
SOCKET_TYPE_STREAM = SOCK_STREAM
SOCKET_TYPE_DATAGRAM = SOCK_DGRAM
SOCKET_TYPE_RAW = SOCK_RAW
Constructors
SocketImpl
SocketImpl();
Creates a SocketImpl.
SocketImpl
SocketImpl(
int sockfd
);
Creates a SocketImpl using the given native socket.
Destructor
~SocketImpl
virtual ~SocketImpl();
Destroys the SocketImpl. Closes the socket if it is still open.
Member Functions
acceptConnection
virtual SocketImpl * acceptConnection(
SocketAddress & clientAddr
);
Get the next completed connection from the socket's completed connection queue.
If the queue is empty, waits until a connection request completes.
Returns a new TCP socket for the connection with the client.
The client socket's address is returned in clientAddr.
address
virtual SocketAddress address();
Returns the IP address and port number of the socket.
available
virtual int available();
Returns the number of bytes available that can be read without causing the socket to block.
bind
virtual void bind(
const SocketAddress & address,
bool reuseAddress = false
);
Bind a local address to the socket.
This is usually only done when establishing a server socket. TCP clients should not bind a socket to a specific address.
If reuseAddress is true, sets the SO_REUSEADDR socket option.
bind
virtual void bind(
const SocketAddress & address,
bool reuseAddress,
bool reusePort
);
Bind a local address to the socket.
This is usually only done when establishing a server socket. TCP clients should not bind a socket to a specific address.
If reuseAddress is true, sets the SO_REUSEADDR socket option.
If reusePort is true, sets the SO_REUSEPORT socket option.
bind6
virtual void bind6(
const SocketAddress & address,
bool reuseAddress = false,
bool ipV6Only = false
);
Bind a local IPv6 address to the socket.
This is usually only done when establishing a server socket. TCP clients should not bind a socket to a specific address.
If reuseAddress is true, sets the SO_REUSEADDR socket option.
The given address must be an IPv6 address. The IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket according to the ipV6Only parameter.
If the library has not been built with IPv6 support, a Poco::NotImplementedException will be thrown.
bind6
virtual void bind6(
const SocketAddress & address,
bool reuseAddress,
bool reusePort,
bool ipV6Only
);
Bind a local IPv6 address to the socket.
This is usually only done when establishing a server socket. TCP clients should not bind a socket to a specific address.
If reuseAddress is true, sets the SO_REUSEADDR socket option.
If reusePort is true, sets the SO_REUSEPORT socket option.
The given address must be an IPv6 address. The IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket according to the ipV6Only parameter.
If the library has not been built with IPv6 support, a Poco::NotImplementedException will be thrown.
close
virtual void close();
Close the socket.
connect
virtual void connect(
const SocketAddress & address
);
Initializes the socket and establishes a connection to the TCP server at the given address.
Can also be used for UDP sockets. In this case, no connection is established. Instead, incoming and outgoing packets are restricted to the specified address.
connect
virtual void connect(
const SocketAddress & address,
const Poco::Timespan & timeout
);
Initializes the socket, sets the socket timeout and establishes a connection to the TCP server at the given address.
connectNB
virtual void connectNB(
const SocketAddress & address
);
Initializes the socket and establishes a connection to the TCP server at the given address. Prior to opening the connection the socket is set to nonblocking mode.
fcntl
int fcntl(
int request
);
A wrapper for the fcntl system call.
fcntl
int fcntl(
int request,
long arg
);
A wrapper for the fcntl system call.
getBlocking
virtual 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!
getBroadcast
bool getBroadcast();
Returns the value of the SO_BROADCAST socket option.
getError
virtual int getError();
Returns the socket error.
getKeepAlive
bool getKeepAlive();
Returns the value of the SO_KEEPALIVE socket option.
getLinger
void getLinger(
bool & on,
int & seconds
);
Returns the value of the SO_LINGER socket option.
getNoDelay
bool getNoDelay();
Returns the value of the TCP_NODELAY socket option.
getOOBInline
bool getOOBInline();
Returns the value of the SO_OOBINLINE socket option.
getOption
void getOption(
int level,
int option,
int & value
);
Returns the value of the socket option specified by level and option.
getOption
void getOption(
int level,
int option,
unsigned & value
);
Returns the value of the socket option specified by level and option.
getOption
void getOption(
int level,
int option,
unsigned char & value
);
Returns the value of the socket option specified by level and option.
getOption
void getOption(
int level,
int option,
Poco::Timespan & value
);
Returns the value of the socket option specified by level and option.
getOption
void getOption(
int level,
int option,
IPAddress & value
);
Returns the value of the socket option specified by level and option.
getRawOption
virtual void getRawOption(
int level,
int option,
void * value,
socklen_t & length
);
Returns the value of the socket option specified by level and option.
getReceiveBufferSize
virtual int getReceiveBufferSize();
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.
getReceiveTimeout
virtual Poco::Timespan getReceiveTimeout();
Returns the receive timeout for the socket.
The returned timeout may be different than the timeout previously set with setReceiveTimeout(), as the system is free to adjust the value.
getReuseAddress
bool getReuseAddress();
Returns the value of the SO_REUSEADDR socket option.
getReusePort
bool getReusePort();
Returns the value of the SO_REUSEPORT socket option.
Returns false if the socket implementation does not support SO_REUSEPORT.
getSendBufferSize
virtual int getSendBufferSize();
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.
getSendTimeout
virtual Poco::Timespan getSendTimeout();
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.
initialized
bool initialized() const;
Returns true iff the underlying socket is initialized.
ioctl
void ioctl(
int request,
int & arg
);
A wrapper for the ioctl system call.
ioctl
void ioctl(
int request,
void * arg
);
A wrapper for the ioctl system call.
listen
virtual void listen(
int backlog = 64
);
Puts the socket into listening state.
The socket becomes a passive socket that can accept incoming connection requests.
The backlog argument specifies the maximum number of connections that can be queued for this socket.
peerAddress
virtual SocketAddress peerAddress();
Returns the IP address and port number of the peer socket.
poll
virtual bool poll(
const Poco::Timespan & timeout,
int mode
);
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.
receiveBytes
virtual int receiveBytes(
void * buffer,
int length,
int flags = 0
);
Receives data from the socket and stores it in buffer. Up to length bytes are received.
Returns the number of bytes received.
Certain socket implementations may also return a negative value denoting a certain condition.
receiveBytes
virtual int receiveBytes(
SocketBufVec & buffers,
int flags = 0
);
Receives data from the socket and stores it in buffers.
Returns the number of bytes received.
Always returns zero for platforms where not implemented.
receiveBytes
virtual int receiveBytes(
Poco::Buffer < char > & buffer,
int flags = 0,
const Poco::Timespan & timeout = 100000
);
Receives data from the socket and stores it in the buffer. If needed, the buffer will be resized to accomodate the data. Note that this function may impose additional performance penalties due to the check for the available amount of data.
Returns the number of bytes received.
receiveFrom
int receiveFrom(
void * buffer,
int length,
struct sockaddr * * ppSA,
socklen_t * * ppSALen,
int flags = 0
);
Receives data from the socket and stores it in buffer. Up to length bytes are received. Stores the native address of the sender in ppSA, and the length of native address in ppSALen.
Returns the number of bytes received.
receiveFrom
virtual int receiveFrom(
void * buffer,
int length,
SocketAddress & address,
int flags = 0
);
Receives data from the socket and stores it in buffer. Up to length bytes are received. Stores the address of the sender in address.
Returns the number of bytes received.
receiveFrom
virtual int receiveFrom(
SocketBufVec & buffers,
SocketAddress & address,
int flags = 0
);
Receives data from the socket and stores it in buffers. Stores the address of the sender in address.
Returns the number of bytes received.
Always returns zero for platforms where not implemented.
receiveFrom
int receiveFrom(
SocketBufVec & buffers,
struct sockaddr * * ppSA,
socklen_t * * ppSALen,
int flags
);
Receives data from the socket and stores it in buffers. Stores the native address of the sender in ppSA, and the length of native address in ppSALen.
Returns the number of bytes received.
secure
virtual bool secure() const;
Returns true iff the socket's connection is secure (using SSL or TLS).
sendBytes
virtual int sendBytes(
const void * buffer,
int length,
int flags = 0
);
Sends the contents of the given buffer through the socket.
Returns the number of bytes sent, which may be less than the number of bytes specified.
Certain socket implementations may also return a negative value denoting a certain condition.
sendBytes
virtual int sendBytes(
const SocketBufVec & buffers,
int flags = 0
);
Sends the contents of the given buffers through the socket.
Returns the number of bytes received.
Always returns zero for platforms where not implemented.
sendTo
virtual int sendTo(
const void * buffer,
int length,
const SocketAddress & address,
int flags = 0
);
Sends the contents of the given buffer through the socket to the given address.
Returns the number of bytes sent, which may be less than the number of bytes specified.
sendTo
virtual int sendTo(
const SocketBufVec & buffers,
const SocketAddress & address,
int flags = 0
);
Sends the contents of the buffers through the socket to the given address.
Returns the number of bytes sent, which may be less than the number of bytes specified.
Always returns zero for platforms where not implemented.
sendUrgent
virtual void sendUrgent(
unsigned char data
);
Sends one byte of urgent data through the socket.
The data is sent with the MSG_OOB flag.
The preferred way for a socket to receive urgent data is by enabling the SO_OOBINLINE option.
setBlocking
virtual void setBlocking(
bool flag
);
Sets the socket in blocking mode if flag is true, disables blocking mode if flag is false.
setBroadcast
void setBroadcast(
bool flag
);
Sets the value of the SO_BROADCAST socket option.
setKeepAlive
void setKeepAlive(
bool flag
);
Sets the value of the SO_KEEPALIVE socket option.
setLinger
void setLinger(
bool on,
int seconds
);
Sets the value of the SO_LINGER socket option.
setNoDelay
void setNoDelay(
bool flag
);
Sets the value of the TCP_NODELAY socket option.
setOOBInline
void setOOBInline(
bool flag
);
Sets the value of the SO_OOBINLINE socket option.
setOption
void setOption(
int level,
int option,
int value
);
Sets the socket option specified by level and option to the given integer value.
setOption
void setOption(
int level,
int option,
unsigned value
);
Sets the socket option specified by level and option to the given integer value.
setOption
void setOption(
int level,
int option,
unsigned char value
);
Sets the socket option specified by level and option to the given integer value.
setOption
void setOption(
int level,
int option,
const Poco::Timespan & value
);
Sets the socket option specified by level and option to the given time value.
setOption
void setOption(
int level,
int option,
const IPAddress & value
);
Sets the socket option specified by level and option to the given time value.
setRawOption
virtual void setRawOption(
int level,
int option,
const void * value,
socklen_t length
);
Sets the socket option specified by level and option to the given time value.
setReceiveBufferSize
virtual void setReceiveBufferSize(
int size
);
Sets the size of the receive buffer.
setReceiveTimeout
virtual void setReceiveTimeout(
const Poco::Timespan & timeout
);
Sets the receive timeout for the socket.
On systems that do not support SO_RCVTIMEO, a workaround using poll() is provided.
setReuseAddress
void setReuseAddress(
bool flag
);
Sets the value of the SO_REUSEADDR socket option.
setReusePort
void setReusePort(
bool flag
);
Sets the value of the SO_REUSEPORT socket option. Does nothing if the socket implementation does not support SO_REUSEPORT.
setSendBufferSize
virtual void setSendBufferSize(
int size
);
Sets the size of the send buffer.
setSendTimeout
virtual void setSendTimeout(
const Poco::Timespan & timeout
);
Sets the send timeout for the socket.
shutdown
virtual int shutdown();
Shuts down both the receiving and the sending part of the socket connection.
Returns 0 for a non-blocking socket. May return a negative value for a non-blocking socket in case of a TLS connection. In that case, the operation should be retried once the underlying socket becomes writable.
shutdownReceive
virtual void shutdownReceive();
Shuts down the receiving part of the socket connection.
shutdownSend
virtual int shutdownSend();
Shuts down the sending part of the socket connection.
Returns 0 for a non-blocking socket. May return a negative value for a non-blocking socket in case of a TLS connection. In that case, the operation should be retried once the underlying socket becomes writable.
socketError
int socketError();
Returns the value of the SO_ERROR socket option.
sockfd
int sockfd() const;
Returns the socket descriptor for the underlying native socket.
type
Type type();
Returns the socket type.
useFileDescriptor
void useFileDescriptor(
int fd
);
Use a external file descriptor for the socket. Required to be careful about what kind of file descriptor you're passing to make sure it's compatible with how you plan on using it. These specifics are platform-specific. Not valid to call this if the internal socket is already initialized. Poco takes ownership of the file descriptor, closing it when this socket is closed.
checkBrokenTimeout
void checkBrokenTimeout(
SelectMode mode
);
error
static void error();
Throws an appropriate exception for the last error.
error
static void error(
const std::string & arg
);
Throws an appropriate exception for the last error.
error
static void error(
int code
);
Throws an appropriate exception for the given error code.
error
static void error(
int code,
const std::string & arg
);
Throws an appropriate exception for the given error code.
init
virtual void init(
int af
);
Creates the underlying native socket.
Subclasses must implement this method so that it calls initSocket() with the appropriate arguments.
The default implementation creates a stream socket.
initSocket
void initSocket(
int af,
int type,
int proto = 0
);
Creates the underlying native socket.
The first argument, af, specifies the address family used by the socket, which should be either AF_INET or AF_INET6.
The second argument, type, specifies the type of the socket, which can be one of SOCK_STREAM, SOCK_DGRAM or SOCK_RAW.
The third argument, proto, is normally set to 0, except for raw sockets.
lastError
static int lastError();
Returns the last error code.
reset
void reset(
int fd = - 1
);
Allows subclasses to set the socket manually, iff no valid socket is set yet.