This is automatic generated document for things in Swow\Socket class
Class Socket
Constants
UNIX datagram socket type, this constant is only avaliable at unix-like os.
UNIX domain socket type, this constant is only available at unix-like os.
Methods
Static method getGlobalAcceptTimeout
Static method getGlobalConnectTimeout
Static method getGlobalDnsTimeout
Static method getGlobalHandshakeTimeout
Static method getGlobalReadTimeout
Static method getGlobalWriteTimeout
Static method setGlobalAcceptTimeout
Static method setGlobalConnectTimeout
Static method setGlobalDnsTimeout
Static method setGlobalHandshakeTimeout
Static method setGlobalReadTimeout
Static method setGlobalTimeout
Static method setGlobalWriteTimeout
Static method typeName
Static method typeSimplify
Method __construct
Method __debugInfo
Returns array<string, mixed>
debug information for var_dump
Method accept
Param int
$timeout [optional] = $this->getAcceptTimeout()
Returns static
Notice: it returns a new un-constructed connection instead of returning itself
Method acceptTo
Template T of static
Param int
$timeout [optional] = $this->getAcceptTimeout()
Returns T
Notice: it does not return the connection, but returns itself
Method bind
Param int
$flags self::BIND_FLAG_* constants combinations
Method checkLiveness
Throws SocketException
when the socket connection is broken
Method close
Method connect
Param int
$timeout [optional] = $this->getConnectTimeout()
Method enableCrypto
Method getAcceptTimeout
Method getConnectTimeout
Method getConnectionError
Returns int
return Errno constants if the socket is broken, zero otherwise, it’s a silent version of {@see Socket::checkLiveness()}
Method getDnsTimeout
Method getFd
Returns int
real file descriptor number
Method getHandshakeTimeout
Method getId
Fixme may cause error on 32-bit env
Returns int
64-bit auto-incrementing ID, never repeat in the same process
Method getIoState
Returns int
self::IO_FLAG_* constants combinations
Method getIoStateName
Method getIoStateNaming
Method getPeerAddress
Method getPeerPort
Method getReadTimeout
Method getRecvBufferSize
Method getSendBufferSize
Method getSimpleType
Method getSimpleTypeName
Method getSockAddress
Method getSockPort
Method getType
Method getTypeName
Method getWriteTimeout
Method isAvailable
Returns bool
Whether the socket has been constructed and has not been closed
Method isClient
Method isEstablished
this method may return true even if the connection is broken, use {@see Socket::checkLiveness()} to check if the connection is broken
Returns bool
Whether the socket has connected (including SSL handshake if it’s on SSL)
Method isOpen
Returns bool
Whether the socket has been opened with exact type and file descriptor is valid
Method isServer
Method isServerConnection
Method listen
Method open
Method peek
read at max $size
bytes data into buffer from socket without removing the read data from socket, only works on some type of socket, returns immediately.
Param Buffer
$buffer buffer to write in, data will be written from offset to offset + length Param int
$offset offset is the start position to write in buffer Param int
$size -1 meaning not limited, otherwise buffer size in bytes.
Returns int
bytes received
Throws SocketException
when socket read failed
Method peekFrom
read at max $size
bytes data into buffer from socket without removing the read data from socket, only works on some type of socket, peer info is stored into $address
and $port
if applicable, returns immediately.
Returns int
bytes received
Throws SocketException
when socket read failed
Method peekString
read at max $size
bytes data from socket as string without removing the read data from socket, only works on some type of socket, returns immediately.
Param int
$size -1 meaning not limited, otherwise buffer size in bytes.
Returns string
data received in string
Throws SocketException
when socket read failed
Method peekStringFrom
read at max $size
bytes data from socket as string without removing the read data from socket, only works on some type of socket, peer info is stored into $address
and $port
if applicable, returns immediately.
Returns string
data received in string
Throws SocketException
when socket read failed
Method read
read $length
bytes data into buffer from socket, keep reading until $length
bytes data received.
context switching may happen here
Param Buffer
$buffer buffer to write in, data will be written from offset to offset + length Param int
$offset offset is the start position to write in buffer Param int
$length -1 meaning not limited, receive until eof, otherwise length in bytes Param (int | null)
$timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value
Returns int
bytes received
Throws SocketException
when got eof without enough data received Throws SocketException
when timed out Throws SocketException
when socket read failed
Method readString
read $length
bytes data from socket as string, keep reading until $length
bytes data received.
context switching may happen here
Param int
$length -1 meaning not limited, receive until eof, otherwise length in bytes Param (int | null)
$timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value
Returns string
data received in string
Throws SocketException
when got eof without enough data received Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recv
read at max $size
bytes data into buffer from socket, returns when any data received or eof.
context switching may happen here
Param Buffer
$buffer buffer to write in, data will be written from offset to offset + length Param int
$offset offset is the start position to write in buffer Param int
$size -1 meaning not limited, otherwise buffer size in bytes. only $size
bytes data will be read from socket if there are more data than $size
bytes, extra data will be kept in socket for further reading options Param (int | null)
$timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value
Returns int
bytes received
Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recvData
read at max $size
bytes data into buffer from socket, return when any data received.
context switching may happen here
Param Buffer
$buffer buffer to write in, data will be written from offset to offset + length Param int
$offset offset is the start position to write in buffer Param int
$size -1 meaning not limited, otherwise buffer size in bytes. only $size
bytes data will be read from socket if there are more data than $size
bytes, extra data will be kept in socket for further reading options Param (int | null)
$timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value
Throws SocketException
when no data received Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recvDataFrom
read at max $size
bytes data into buffer from socket, peer info is stored into $address
and $port
if applicable, return when any data received.
context switching may happen here
Returns int
bytes received
Throws SocketException
when no data received Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recvFrom
read at max $size
bytes data into buffer from socket, peer info is stored into $address
and $port
if applicable, return when any data received or eof.
context switching may happen here
Returns int
bytes received
Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recvString
read at max $size
bytes data from socket as string, returns when any data received or eof.
context switching may happen here
Param int
$size -1 meaning not limited, otherwise buffer size in bytes. only $size
bytes data will be read from socket if there are more data than $size
bytes, extra data will be kept in socket for further reading options Param (int | null)
$timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value
Returns string
data received in string
Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recvStringData
read at max $size
bytes data from socket as string, return when any data received.
context switching may happen here
Param int
$size -1 meaning not limited, otherwise buffer size in bytes. only $size
bytes data will be read from socket if there are more data than $size
bytes, extra data will be kept in socket for further reading options Param (int | null)
$timeout timeout in microseconds or null for using {@see Socket::getReadTimeout()} value
Returns string
data received in string
Throws SocketException
when no data received Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recvStringDataFrom
read at max $size
bytes data from socket as string, peer info is stored into $address
and $port
if applicable, return when any data received.
context switching may happen here
Returns string
data received in string
Throws SocketException
when no data received Throws SocketException
when timed out Throws SocketException
when socket read failed
Method recvStringFrom
read at max $size
bytes data from socket as string, peer info is stored into $address
and $port
if applicable, return when any data received or eof.
context switching may happen here
Returns string
data received in string
Throws SocketException
when timed out Throws SocketException
when socket read faile
Method send
write data into socket from $start
of data with $length
bytes
Throws SocketException
when timed out Throws SocketException
when write failed
Method sendFile
Send file without memory allocation or memory copy
Param int
$offset [optional] = 0 Param int
$timeout [optional] = $this->getWriteTimeout()
Method sendHandle
Send a socket handle to peer via pipe socket
Param int
$timeout [optional] = $this->getWriteTimeout()
Method sendTo
write data to specified address (and port if protocol needs port) from $start
of data with $length
bytes
Throws SocketException
when timed out Throws SocketException
when write failed
Method setAcceptTimeout
Method setConnectTimeout
Method setDnsTimeout
Method setHandshakeTimeout
Method setReadTimeout
Method setRecvBufferSize
Method setSendBufferSize
Method setTcpKeepAlive
Method setTcpNodelay
Method setTimeout
Method setWriteTimeout
Method write
write io vector to socket
vector is an array for all contents to write, elements in it maybe
Buffer
: write full text from start$start
string|Stringable
: write full text from start$start
array[Buffer $content, int $start, int $length]
: write it from start$start
, with length$length
array[string|Stringable $content, int $start, int $offset, ?int $length]
: write the string from start$start
, with length$length
, and offset$offset
in the string
for code
$s = new Socket(Socket::TYPE_STDOUT);
$s->write([
(new Buffer(0))->write('1234567890', start: 6),
'abc',
['1234567890', 1, 2],
[(new Buffer(0))->write('1234567890', start: 4), 2],
]);
string "7890abc2356"
will be written to the socket.
context switching may happen here
Throws SocketException
when timed out Throws SocketException
when write failed
Method writeTo
write io vector to socket with peer information
See Socket::write() for io vector format
context switching may happen here
Throws SocketException
when timed out Throws SocketException
when write failed