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 supportsTcpReusePortLoadBalancing
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
enable crypto (TLS) on socket the options is a little different from PHP options, PHP uses cafile, local_cert, local_pk while we use ca_file, certificate, certificate_key. most other options is the same as PHP options.
context switching may happen here
Param array{verify_peer?: bool, verify_peer_name?: bool, allow_self_signed?: bool, verify_depth?: int, ca_file?: string, ca_path?: string, security_level?: int, alpn_protocols?: string, passphrase?: string, certificate?: string, certificate_key?: string, no_ticket?: bool, no_compression?: bool, peer_name?: string, peer_fingerprint?: (string | array<string, string>), SNI_server_certs?: (array<string, string> | array<string, array{certificate?: callable, certificate_key?: callable}>)} $options [optional] The method ignores unknown options. verify_peer=true requires peer_name.
Returns static it returns itself
Throws SocketException when options are invalid Throws SocketException when IO error
Method getAcceptTimeout
Method getConnectTimeout
Method getConnectionError
Get the connection error.
Returns int 0 when the connection is healthy, an Errno value when it is broken. A socket without a descriptor gives EBADF (-9), an open socket without a connection gives ENOTCONN (-107).
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
Use an existing socket stream.
Use a socket of type TYPE_TCP. It has no native socket before bind or connect.
Param resource $socket the stream to use
Throws SocketException when the stream is not a socket Throws \Error when the socket already has a native socket
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, 0 when no data is ready at the timeout
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.
The current length of the buffer must be at least the offset. A new buffer starts at 0.
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 milliseconds 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 number of bytes, must be larger than 0 Param (int | null) $timeout timeout in milliseconds 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 Throws \ValueError when the length is not larger than 0
Method recreate
Recreate the native socket handle on this Socket object.
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 milliseconds 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 milliseconds 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 milliseconds 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 milliseconds 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
A write to a closed peer can succeed. Check getConnectionError() after the write.
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 the peer.
The socket must be a named pipe with IPC enabled, type TYPE_IPCC. The API has no method to receive the handle.
Param int $timeout [optional] = $this->getWriteTimeout()
Throws SocketException when the socket is not an IPC pipe
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
Set the timeouts.
The method does not change the accept timeout. Use setAcceptTimeout() for it. A negative value becomes -1.
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$startstring|Stringable: write full text from start$startarray[Buffer $content, int $start, int $length]: write it from start$start, with length$lengtharray[string|Stringable $content, int $start, int $length]: write it from start$start, with length$length
A null element ends the vector. A write to a closed peer can succeed. Check getConnectionError() after the write.
for code
$s = new Socket(Socket::TYPE_STDOUT);
$buffer = new Buffer(0);
$buffer->append('xyz');
$s->write([
'abcd',
['abcdef', 2],
['abcdef', 1, 3],
[$buffer, 1],
]);
string "abcdcdefbcdyz" will be written to the socket.
context switching may happen here
Throws SocketException when timed out Throws SocketException when write failed Throws \ValueError when an array element has more than 3 items
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