Note: This class is not available with the PO_NO_NETWORK parse option.
Socket objects allow Qore programs safe access to network sockets. Non-blocking socket I/O can be performed by appending a timeout value in milliseconds to all Socket::recv*() methods, or by using the Socket::isDataAvailable() method with a timeout value in milliseconds (1000 ms = 1 second). Note that as with all Qore functions and methods accepting a timeout value, relative date/time values can be given instead of integers to make the source more readable, for example:
my bool $rc = $socket.isDataAvailable(1250ms); # times out in 1.25 secondsSocket objects can automatically convert character encodings if desired when sending string data with Socket::send(). Use the Socket::setCharset() method to set the character encoding for the socket. If a character encoding is set, and string data is read with the Socket::recv() method, then it will be tagged with the encoding of the socket as well.
Client applications should call Socket::connect() to connect to a remote port or a UNIX domain socket (socket file on the local server). However, if the remote end is expecting a TLS/SSL connection, use Socket::connectSSL() instead.
Server applications should call Socket::bind(), Socket::listen(), and Socket::accept() in this order to accept incoming connections. Normally a new thread should be started after the Socket::accept() call to handle the new connection in a separate thread (Socket::accept() returns a new Socket object for the accepted connection).
To support TLS/SSL server connections, first set the certificate and private key with the Socket::setCertificate() and Socket::setPrivateKey() methods (see the SSLCertificate Class and the SSLPrivateKey Class for more information on the parameters required for these methods). Then Socket::acceptSSL() should be called after the socket is in a listening state to accept client connections and negotiate a TLS/SSL connection.
This class supports posting events to a Queue. See Event Handling for more information.
The events raised by this object are listed in the following table:
Table 4.341. Socket Events
Name | Description |
|---|---|
Raised when a network packet is received. | |
Raised when a network packet is sent. | |
Raised when a socket is closed. | |
Raised when the object being monitored is deleted. | |
Raised when a hostname lookup is attempted. | |
Raised when a hostname lookup is resolved. | |
Raised when an HTTP message is sent. | |
Raised when an HTTP message is received. | |
Raised right before a socket connection attempt is made. | |
Raised when the socket connection has been established. | |
Raised when socket SSL negotiation starts. | |
Raised when SSL communication has been negotiated and established. |
Table 4.342. Socket Method Overview
Method | Except? | Description |
|---|---|---|
N | Creates the socket object | |
N | Closes the socket if it's open and destroys the object. | |
N | Creates a new Socket object, not based on the parent. | |
| Y | Connects to a remote port (if the string has a format "host:port") or UNIX domain socket file with an optional timeout value with a millisecond resolution. |
| Y | Connects to the given host and port with an optional timeout value with a millisecond resolution. |
Y | Connects to the UNIX domain socket file. | |
| Y | Connects to a remote socket and attempts to establish a TLS/SSL connection; accepts an optional timeout value with a millisecond resolution. |
| Y | Connects to the given host and port and attempts to establish a TLS/SSL connection; accepts an optional timeout value with a millisecond resolution. |
Y | Connects to the UNIX domain socket file and attempts to establish a TLS/SSL connection. | |
| N | Opens and binds the socket to a port, interface and port (if the |
| Y | Opens and binds the socket to the given IPv4 or IPv6 interface (or if no interface is given, then to all interfaces on the local system) and port (the port number will be derived from the service name if a numeric port number is not given). If the third parameter is |
| Y | Opens and binds the socket to the given UNIX domain socket file as given by the filename argument. If any errors occur, an exception is thrown. |
| Y | Accepts connections on a listening socket with a variant accepting a timeout value with a millisecond resolution. If the variant with a timeout is used, then if no connection is accepted within the timeout period, then NOTHING is returned, otherwise a Socket object for the new connection is returned. Otherwise, if any errors occur, an exception is thrown. |
| Y | Accepts a remote connection and attempts to negotiate a TLS/SSL connection. If the variant with a timeout is used, then if no connection is accepted within the timeout period, then NOTHING is returned, otherwise a Socket object for the new connection is returned. Otherwise, if any errors occur, an exception is thrown. |
Y | Shuts down the SSL connection on a secure connection. | |
Y | Listens for connections on the socket; sets the socket in a listening state. | |
| N | Returns |
| N | Returns |
| Y | Sends string or binary data over the socket; string data is converted to the socket's encoding if necessary. |
| Y | Sends string or binary data over the socket; string data is not converted to the socket's encoding, but sent exactly as-is. |
| Y | Sends a 1-byte integer over the socket. |
| Y | Sends a 2-byte (16-bit) integer in big-endian format (network byte order) over the socket. |
| Y | Sends a 4-byte (32-bit) integer in big-endian format (network byte order) over the socket. |
| Y | Sends an 8-byte (64-bit) integer in big-endian format (network byte order) over the socket. |
| Y | Sends a 2-byte (16-bit) integer in little-endian format over the socket. |
| Y | Sends a 4-byte (32-bit) integer in little-endian format over the socket. |
| Y | Sends an 8-byte (64-bit) integer in little-endian format over the socket. |
| Y | Sends an HTTP message with a method and user-defined headers given as a hash and an optional message body. |
| Y | Sends an HTTP response with user-defined headers given as a hash and an optional message body. |
| Y | Receives data from the socket and returns a string. |
| Y | Receives a 1-byte signed integer from the socket. |
| Y | Receives a 2-byte (16-bit) signed integer in big-endian format (network byte order) from the socket. |
| Y | Receives a 4-byte (32-bit) signed integer in big-endian format (network byte order) from the socket. |
| Y | Receives an 8-byte (64-bit) signed integer in big-endian format (network byte order) from the socket. |
| Y | Receives a 2-byte (16-bit) signed integer in little-endian format from the socket. |
| Y | Receives a 4-byte (32-bit) signed integer in little-endian format from the socket. |
| Y | Receives an 8-byte (64-bit) signed integer in little-endian format from the socket. |
| Y | Receives a 1-byte unsigned integer from the socket. |
| Y | Receives a 2-byte (16-bit) unsigned integer in big-endian format (network byte order) from the socket. |
| Y | Receives a 4-byte (32-bit) unsigned integer in big-endian format (network byte order) from the socket. |
| Y | Receives a 2-byte (16-bit) unsigned integer in little-endian format from the socket. |
| Y | Receives a 4-byte (32-bit) unsigned integer in little-endian format from the socket. |
| Y | Receives data on a socket and returns a binary object |
| Y | Retuns a hash representing the data in the HTTP header read, or, if the data cannot be parsed as an HTTP header, the data read is returned as a string. |
Y | Returns information about the remote end for connected sockets as a hash. | |
N | Returns the port number of the socket for INET sockets. | |
Y | Returns information about the local socket as a hash; the socket must be open. | |
N | Ensures that a socket will be closed even if shared with other processes. | |
N | Closes the socket. | |
N | Returns the character encoding for the socket. | |
| N | Sets the character encoding for the socket. |
N | Returns the | |
| N | Sets the |
N | Returns the socket file descriptor number. | |
N | Returns the name of the cipher for an encrypted connection or NOTHING is a secure connection has not been established. | |
N | Returns the version string of the cipher for encrypted connections or NOTHING is a secure connection has not been established. | |
N | Returns | |
N | Returns | |
| N | Sets the X.509 certificate to use for negotiating encrypted connections. |
| N | Sets the private key to use for negotiating encrypted connections along with the X.509 certificate. |
N | Returns a string code giving the result of verifying the remote certificate or NOTHING if an encrypted connection is not currently established. | |
N | Sets a Queue object to receive socket events, or clears the queue when called with no argument.. |
Creates the socket object.
my Socket $sock();Closes the socket if it's open and destroys the object. If the socket was a UNIX domain socket, and was created with Socket::bind() or Socket::bindUNIX(), then the socket file will be deleted as well.
delete $sock;Creates a new Socket object, not based on the original.
my Socket $new_sock = $sock.copy();Binds the socket to a port, interface and port (if the $bind_to string has a format "host:port"), or UNIX domain socket file (if no port or service name appears in the bind string). If the second parameter is True, then the socket will set the SO_REUSEADDR option, which will allow the socket to be bound to a port that is not yet closed (for example, in a TIME_WAIT state).
This method tries to automatically pick the appropriate address family from the arguments; note that a hostname or address in square brackets (ex: [localhost]) will be looked up and bound as an ipv6 address, additionally, the method recognizes ipv6 addresses by embedded colons (:) in the address string and binds them as such.
Internally, the getaddrinfo() function is used to look up bind addresses; internal bind() operations are tried in sequence for each address returned; as soon as a bind operation is successful, the method returns. If none of the addresses can be bound, then an error code is returned.
See also Socket::bindINET() and Socket::bindUNIX().
UNIX domain sockets are not available on native Windows ports.
intSocket::bind(string$bind_to, softbool$reuseaddr = False)
intSocket::bind(int$port, softbool$reuseaddr = False)
# bind to port 80 on all interfaces on the local system and reuse the address, check return code
if ($sock.bind(80, True))
throw "BIND-ERROR", strerror(errno());# bind to interface 192.168.2.23 port 8080 and do not reuse the address, check return code
if ($sock.bind("192.168.2.23:8080"))
throw "BIND-ERROR", strerror(errno());# bind to localhost port 8080 with ipv6 and do not reuse the address, check return code
if ($sock.bind("[localhost]:8080"))
throw "BIND-ERROR", strerror(errno());# bind to ipv6 host address fe80::21c:42ff:fe00:8, port 1001, reuse the address, check return code
if ($sock.bind("[fe80::21c:42ff:fe00:8]:1001", True))
throw "BIND-ERROR", strerror(errno());# bind to UNIX domain socket file "/tmp/socket", check return code
if ($sock.bind("/tmp/socket"))
throw "BIND-ERROR", strerror(errno());Table 4.343. Arguments for Socket::bind(string $bind_to, softbool $reuseaddr = False) Variant
Argument | Description |
|---|---|
| If a colon appears in the string, the string will be assumed to be a hostname:port specification, and the port on the named IP address will be bound, otherwise, if the string contains no colon, the socket will be bound to a UNIX domain socket file on the local filesystem with the given name. |
| If this optional argument evaluates to |
Table 4.344. Arguments for Socket::bind(int $port, softbool $reuseaddr = False) Variant
Argument | Description |
|---|---|
The first argument gives the port number on the local machine; all network interfaces will be bound with this port number. | |
| If this optional argument evaluates to |
Table 4.345. Return Values for Socket::bind()
Return Type | Description |
|---|---|
Returns 0 for success, -1 for error. |
This method does not throw any exceptions.
Opens and binds the socket to a port, interface and port (if the $bind_to string has a format "host:port"), or UNIX domain socket file (if no port or service name appears in the bind string). If the second parameter is True, then the socket will set the SO_REUSEADDR option, which will allow the socket to be bound to a port that is not yet closed (for example, in a TIME_WAIT state).
Internally, the getaddrinfo() function is used to look up bind addresses; internal bind() operations are tried in sequence for each address returned; as soon as a bind operation is successful, the method returns. If none of the addresses can be bound, then an error code is returned.
If any errors occur, an exception is thrown.
See also Socket::bind() and Socket::bindUNIX().
nothingSocket::bindINET(*string$interface, *softstring$service, softbool$reuseaddr = False, softint$family = AF_UNSPEC, softint$socktype = SOCK_STREAM, softint$protocol = 0)
# bind to port 80 on all interfaces on the local system and reuse the address
$sock.bindINET(NOTHING, 80, True);# bind to interface 192.168.2.23 port 8080 and do not reuse the address
$sock.bindINET("192.168.2.23", 8080);# bind to localhost port 8080 with ipv6 and do not reuse the address
$sock.bindINET("localhost", 8080, False, AF_INET6);# bind to ipv6 host address fe80::21c:42ff:fe00:8, port 1001, reuse the address
$sock.bindINET("fe80::21c:42ff:fe00:8", 1001, True);Table 4.346. Arguments for Socket::bindINET()
Argument | Description |
|---|---|
| The host name or IP address to bind to. |
| The service name (ex: |
| If this optional argument evaluates to |
The address family to use to bind; see Network Address Family Constants. AF_UNSPEC means to try all possible addres families. | |
| The type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets. |
| The protocol number for the socket; use 0 for the default protocol. |
Table 4.347. Exceptions Thrown by Socket::bindINET()
err | desc |
|---|---|
| Both hostname and service name are empty or not set; error opening socket for bind; error binding on socket. |
| error looking up either nodename or servicename (or not known) |
Opens and binds the socket to the given UNIX domain socket file as given by the filename argument. Note that the socket file is automatically deleted in the destructor when a UNIX socket is closed.
If any errors occur, an exception is thrown.
See also Socket::bind() and Socket::bindINET().
UNIX domain sockets are not available on native Windows ports.
nothingSocket::bindUNIX(string$path, softint$socktype = SOCK_STREAM, softint$protocol = 0)
# bind to UNIX domain socket file "/tmp/socket"
$sock.bindUNIX("/tmp/socket");Table 4.348. Arguments for Socket::bindUNIX()
Argument | Description |
|---|---|
| The path of the UNIX domain socket to create and bind to. |
| The type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets. |
| The protocol number for the socket; use 0 for the default protocol. |
Table 4.349. Exceptions Thrown by Socket::bindUNIX()
err | desc |
|---|---|
| Error opening socket for bind; error binding on socket. |
Accepts connections on a listening socket (see Socket::listen()) with a variant accepting a timeout value with a millisecond resolution. If the variant with a timeout is used, then if no connection is accepted within the timeout period, then NOTHING is returned, otherwise a Socket object for the new connection is returned.
Otherwise, if any errors occur, an exception is thrown.
The new Socket object returned will have the same character encoding as the current object. Once a new connection has been accepted, call Socket::getPeerInfo() to get information about the remote socket.
*SocketSocket::accept(timeout$timeout_ms)
my *Socket $new_socket = $sock.accept(30s);Table 4.350. Arguments for Socket::accept()
Argument | Description |
|---|---|
| If a timeout value is passed and the connection takes longer to establish than the timeout, then NOTHING is returned. |
Table 4.351. Return Values for Socket::accept()
Return Type | Description |
|---|---|
When a new connection has been accepted, a new Socket object is returned for the new connection, unless the variable with a timeout is used and a timeout occurs; in this case NOTHING is returned. |
Table 4.352. Exceptions Thrown by Socket::accept()
err | desc |
|---|---|
| The socket is not bound. |
| Error in accepting connection. |
Accepts connections on a listening socket (see Socket::listen()) and attempts to negotiate a TLS/SSL connection.
If the variant with a timeout is used, then if no connection is accepted within the timeout period, then NOTHING is returned, otherwise a Socket object for the new connection is returned.
Otherwise, if any errors occur, an exception is thrown.
The new Socket object returned will have the same character encoding as the current object. Once a new connection has been accepted, call Socket::getPeerInfo() to get information about the remote socket.
*SocketSocket::acceptSSL(timeout$timeout_ms)
my *Socket $new_sock = $sock.acceptSSL(30s);Table 4.353. Arguments for Socket::acceptSSL()
Argument | Description |
|---|---|
| If a timeout value is passed and the connection takes longer to establish than the timeout, then NOTHING is returned. |
Table 4.354. Return Values for Socket::acceptSSL()
Return Type | Description |
|---|---|
When a new connection is accepted and a TLS/SSL session has been successfully negotated, a new Socket object is returned for the new connection, unless the variable with a timeout is used and a timeout occurs; in this case NOTHING is returned. |
Table 4.355. Exceptions thrown by Socket::acceptSSL()
err | desc |
|---|---|
| The socket is not bound. |
| Error in accepting connection. |
| An error occurred establishing the TLS/SSL connection. |
| The TLS/SSL connection was shut down by the remote end. |
Connects the socket to a remote (or local) port or UNIX domain socket file, for network (ipv4 and ipv6) connections, accepts an optional timeout value in milliseconds (relative date/time values can be given instead of an integer in milliseconds to make the source more readable; ex: 20s). If any errors occur, an exception is thrown.
Enclose ipv6 addresses in square brackets (ex: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80", will connect to port 80 on the given ipv6 address); also if a hostname is enclosed in squark brackets, it will be resolved as an ipv6 adress (ex: "[localhost]:80" will connect to port 80 on ::1, assuming "localhost" can be resolved to the ipv6 loopback address on the system).
See also Socket::connectUNIX(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectUNIXSSL().
UNIX domain sockets are not available on native Windows ports.
nothingSocket::connect(string$dest, timeout$timeout_ms = -1)
# connect to ipv4 adress 192.168.1.45 port 8080 with a 30 second timeout
$sock.connect("192.168.1.45:8080", 30s);# connect to ipv6 adress 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 10 second timeout
$sock.connect("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80", 10s);# connect to localhost using ipv6 (::1) port 80 with a 10 second timeout
$sock.connect("[localhost]:80", 15s);# connect to UNIX domain socket file "/tmp/socket"
$sock.connect("/tmp/socket");EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED
Table 4.356. Arguments for Socket::connect()
Argument | Description |
|---|---|
| If a colon appears in the string, the string will be assumed to be a hostname:port specification to connect to. If the string contains no colon, the socket will try to connect to a UNIX domain socket file on the local filesystem with the given name. |
| If a timeout value is passed and the connection takes longer to establish than the timeout, an exception is thrown. |
Table 4.357. Exceptions Thrown by Socket::connect()
err | desc |
|---|---|
| An error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc). |
Connects the socket to a remote (or local) port; accepts an optional timeout value in milliseconds (relative date/time values can be given instead of an integer in milliseconds to make the source more readable; ex: 20s). If any errors occur, an exception is thrown.
Do not use square brackets to designate ipv6 addresses with this method; just give the address in its normal form (ex: "2001:0db8:85a3:0000:0000:8a2e:0370:7334").
See also Socket::connect(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectUNIXSSL().
UNIX domain sockets are not available on native Windows ports.
nothingSocket::connectINET(string$host, softstring$service, timeout$timeout_ms = -1, softint$family = AF_UNSPEC, softint$socktype = SOCK_STREAM)
# connect to ipv4 address 192.168.1.45 port 8080 with a 30 second timeout
$sock.connectINET("192.168.1.45", 8080, 30s);# connect to ipv6 address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 20 second timeout
$sock.connectINET("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 80, 20s);EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED
Table 4.358. Arguments for Socket::connectINET()
Argument | Description |
|---|---|
| The host name or IP address to connect to. |
| The service name (ex: |
| If a timeout value is passed and the connection takes longer to establish than the timeout, an exception is thrown. |
The address family to use to connect to the remote socket; see Network Address Family Constants. | |
| The type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets. |
Table 4.359. Exceptions Thrown by Socket::connectINET()
err | desc |
|---|---|
| An error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc). |
Connects the socket to a remote (or local) port and attempts to establish a TLS/SSL connection; accepts an optional timeout value in milliseconds (relative date/time values can be given instead of an integer in milliseconds to make the source more readable; ex: 20s). If any errors occur, an exception is thrown.
Do not use square brackets to designate ipv6 addresses with this method; just give the address in its normal form (ex: "2001:0db8:85a3:0000:0000:8a2e:0370:7334").
See also Socket::connect(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINET(), and Socket::connectUNIXSSL().
nothingSocket::connectINETSSL(string$host, softstring$service, timeout$timeout_ms = -1, softint$family = AF_UNSPEC, softint$socktype = SOCK_STREAM)
# connect to 192.168.1.45 port 8080 with a 30 second timeout
$sock.connectINETSSL("192.168.1.45", 8080, 30s);# connect to ipv6 address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 20 second timeout
$sock.connectINETSSL("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 80, 20s);EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED
Table 4.360. Arguments for Socket::connectINETSSL()
Argument | Description |
|---|---|
| The host name or IP address to connect to. |
| The service name (ex: |
| If a timeout value is passed and the connection takes longer to establish than the timeout, an exception is thrown. |
Table 4.361. Exceptions Thrown by Socket::connectINETSSL()
err | desc |
|---|---|
| An error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc). |
Connects the socket to a UNIX domain socket file. If any errors occur, an exception is thrown.
See also Socket::connect(), Socket::connectINET(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectUNIXSSL().
UNIX domain sockets are not available on native Windows ports.
nothingSocket::connectUNIX(string$file, softint$socktype = SOCK_STREAM, softint$protocol = 0)
# connect to UNIX domain socket file "/tmp/socket"
$sock.connectUNIX("/tmp/socket");Table 4.362. Arguments for Socket::connectUNIX()
Argument | Description |
|---|---|
| The socket will try to connect to a UNIX domain socket file on the local filesystem with the given name. |
| The type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets. |
| The protocol number for the socket; use 0 for the default protocol. |
Table 4.363. Exceptions Thrown by Socket::connectUNIX()
err | desc |
|---|---|
| An error occured connecting to the remote socket. |
Connects the socket to a UNIX domain socket file and attempts to establish a TLS/SSL connection. If any errors occur, an exception is thrown.
See also Socket::connect(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectINET().
UNIX domain sockets are not available on native Windows ports.
nothingSocket::connectUNIXSSL(string$file, softint$socktype = SOCK_STREAM, softint$protocol = 0)
# connect to UNIX domain socket file "/tmp/socket"
$sock.connectUNIXSSL("/tmp/socket");Table 4.364. Arguments for Socket::connectUNIXSSL()
Argument | Description |
|---|---|
| The socket will try to connect to a UNIX domain socket file on the local filesystem with the given name. |
| The type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets. |
| The protocol number for the socket; use 0 for the default protocol. |
Table 4.365. Exceptions Thrown by Socket::connectUNIXSSL()
err | desc |
|---|---|
| An error occured connecting to the remote socket. |
Connects to a remote socket and attempts to establish a TLS/SSL connection, for network (INET) connections, accepts an optional timeout value in milliseconds (relative date/time values can be given instead of an integer in milliseconds to make the source more readable; ex: 20s). If any errors occur, an exception is thrown.
Enclose ipv6 addresses in square brackets (ex: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80", will connect to port 80 on the given ipv6 address); also if a hostname is enclosed in squark brackets, it will be resolved as an ipv6 adress (ex: "[localhost]:80" will connect to port 80 on ::1, assuming "localhost" can be resolved to the ipv6 loopback address on the system).
See also Socket::connect(), Socket::connectUNIX(), Socket::connectUNIXSSL(), Socket::connectINETSSL(), and Socket::connectINET().
UNIX domain sockets are not available on native Windows ports.
nothingSocket::connectSSL(string$dest, timeout$timeout_ms = -1)
# connect to ipv4 adress 192.168.1.45 port 8080 with a 30 second timeout
$sock.connectSSL("192.168.1.45:8080", 30s);# connect to ipv6 adress 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 10 second timeout
$sock.connectSSL("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80", 10s);# connect to localhost using ipv6 (::1) port 80 with a 10 second timeout
$sock.connectSSL("[localhost]:80", 15s);# connect to UNIX domain socket file "/tmp/socket"
$sock.connectSSL("/tmp/socket");EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED
Table 4.366. Arguments for Socket::connectSSL()
Argument | Description |
|---|---|
| If a colon appears in the string, the string will be assumed to be a hostname:port specification to connect to. If the string contains no colon, the socket will try to connect to a UNIX domain socket file on the local filesystem with the given name. |
| If a timeout value is passed and the connection takes longer to establish than the timeout, an exception is thrown. |
Table 4.367. Exceptions thrown by Socket::connectSSL()
err | desc |
|---|---|
| An error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc). |
| An error occurred establishing the TLS/SSL connection. |
| The TLS/SSL connection was shut down by the remote end. |
Listens for new connections on a bound socket (see Socket::bind())
$sock.listen();
Table 4.368. Return Values for Socket::listen()
Return Type | Description |
|---|---|
Returns 0 for success, -1 for error. |
Shuts down the SSL connection on a secure connection; if any errors occur, an exception is raised.
$sock.shutdownSSL();
Table 4.370. Exceptions thrown by Socket::shutdownSSL()
err | desc |
|---|---|
| An error occurred shutting down the TLS/SSL connection. |
Returns the name of the cipher for an encrypted connection or NOTHING if an encrypted connection has not been established.
my *string $str = $sock.getSSLCipherName();Returns the version string of the cipher for encrypted connections or NOTHING if an encrypted connection has not been established.
my *string $str = $sock.getSSLCipherVersion();Returns True if the connection is a secure TLS/SSL connection.
my bool $b = $sock.isSecure();Table 4.373. Return Values for Socket::isSecure()
Return Type | Description |
|---|---|
True if the connection is encrypted, |
Returns True if the socket is open.
my bool $b = $sock.isOpen();Table 4.374. Return Values for Socket::isOpen()
Return Type | Description |
|---|---|
True if the socket is open, |
Sets the X.509 certificate to use for negotiating encrypted connections. Requires an SSLCertificate object as the only argument to the method or the PEM or DER-encoded form of the certificate.
nothingSocket::setCertificate(SSLCertificate$cert)
nothingSocket::setCertificate(string$cert_pem)
nothingSocket::setCertificate(binary$cert_der)
$sock.setCertificate($cert);
Table 4.375. Arguments for Socket::setCertificate()
Argument | Description |
|---|---|
| This must be an SSL Certificate object. |
| The PEM-encoded string for the SSL Certificate. |
| The DER-encoded binary for the SSL Certificate. |
Sets the private key to use for negotiating encrypted connections along with the X.509 certificate. Requires an SSLPrivateKey object as the only argument to the method or the PEM or DER-encoded form of the private key.
nothingSocket::setPrivateKey(SSLPrivateKey$key)
nothingSocket::setPrivateKey(string$key_pem, string$pass)
nothingSocket::setPrivateKey(string$key_pem)
nothingSocket::setPrivateKey(binary$key_der)
$sock.setPrivateKey($pkey);
Table 4.376. Arguments for Socket::setPrivateKey()
Argument | Description |
|---|---|
| The private key for the certificate. |
The PEM-encoded string for the private key for the certificate and optionally the password if required. | |
| The DER-encoded binary form of the private key for the certificate. |
Returns a string code giving the result of verifying the remote certificate. Return value are the keys described in the X509_VerificationReasons hash constant. This hash can also be used to generate a textual description of the verification result.
my *string $str = $sock.verifyPeerCertificate();Table 4.377. Return Values for Socket::verifyPeerCertificate()
Return Type | Description |
|---|---|
A string code giving the result of verifying the peer's certificate. No value is returned if a secure connection has not been established. |
Sets a Queue object to receive socket events. To remove the event queue and stop monitoring socket events; call with no arguments or pass NOTHING to the method. See Event Handling for more information.
$sock.setEventQueue($queue);
Returns True is data is available on the socket, takes an optional timeout. With a timeout of zero this method can be used for non-blocking polling the socket for data (can also be used to poll for new connections before Socket::accept()).
boolSocket::isDataAvailable(timeout$timeout_ms = 0)
my bool $b = $sock.isDataAvailable(); # returns True if data is available nowTable 4.379. Arguments for Socket::isDataAvailable()
Argument | Description |
|---|---|
| An optional timeout in milliseconds (1/1000 second). If no timeout is given, the method returns immediately. |
Table 4.380. Return Values for Socket::isDataAvailable()
Return Type | Description |
|---|---|
True if data is available on the socket, |
Returns True if all data has been written to the socket, takes an optional timeout. With a timeout of zero this method returns immediately.
boolSocket::isWriteFinished(timeout$timeout_ms = 0)
my bool $b = $sock.isWriteFinished(0);Table 4.381. Arguments for Socket::isWriteFinished()
Argument | Description |
|---|---|
| An optional timeout in milliseconds (1/1000 second). If no timeout is given, the method returns immediately. |
Table 4.382. Return Values for Socket::isWriteFinished()
Return Type | Description |
|---|---|
True if the send action is complete, |
Sends string or binary data over a connected socket. String data will be converted to the encoding set for the socket if necessary. If an error occurs, -1 will be returned; in this case check errno() for the error number.
See also Socket::sendBinary().
intSocket::send(data$data)
if ($sock.send($data) == -1)
printf("error sending data: %s\n", strerror(errno()));Sends string or binary data over a connected socket. String data will not be converted to the encoding set for the socket, but rather will be sent as-is, even if the string's encoding is different from the socket's encoding. If an error occurs, -1 will be returned; in this case check errno() for the error number.
See also Socket::send().
intSocket::sendBinary(data$data)
if ($sock.sendBinary($data) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.388. Exceptions Thrown by Socket::sendBinary()
err | desc |
|---|---|
| The socket is not connected. |
Sends a 1-byte integer over the socket. If an error occurs, -1 will be returned; in this case check errno() for the error number.
intSocket::sendi1(softint$i = 0)
if ($sock.sendi1($val) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.389. Arguments for Socket::sendi1()
Argument | Description |
|---|---|
| The integer to send; only the least-significant byte will be sent. |
Table 4.391. Exceptions Thrown by Socket::sendi1()
err | desc |
|---|---|
| The socket is not connected. |
Sends a 2-byte integer in big-endian format (network byte order) over the socket. If an error occurs, -1 will be returned; in this case check errno() for the error number.
intSocket::sendi2(softint$i = 0)
if ($sock.sendi2($val) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.392. Arguments for Socket::sendi2()
Argument | Description |
|---|---|
| The integer to send; only the least-significant 2-bytes will be sent. |
Table 4.394. Exceptions Thrown by Socket::sendi2()
err | desc |
|---|---|
| The socket is not connected. |
Sends a 4-byte integer in big-endian format (network byte order) over the socket. If an error occurs, -1 will be returned; in this case check errno() for the error number.
intSocket::sendi4(softint$i = 0)
if ($sock.sendi4($val) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.395. Arguments for Socket::sendi4()
Argument | Description |
|---|---|
| The integer to send; only the least-significant 4-bytes will be sent. |
Table 4.397. Exceptions Thrown by Socket::sendi4()
err | desc |
|---|---|
| The socket is not connected. |
Sends an 8-byte (64-bit) integer in big-endian format (network byte order) over the socket. If an error occurs, -1 will be returned; in this case check errno() for the error number.
intSocket::sendi8(softint$i = 0)
if ($sock.sendi8($val) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.400. Exceptions Thrown by Socket::sendi8()
err | desc |
|---|---|
| The socket is not connected. |
Sends a 2-byte integer in little-endian format over the socket. If an error occurs, -1 will be returned; in this case check errno() for the error number.
intSocket::sendi2LSB(softint$i = 0)
if ($sock.sendi2LSB($val) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.401. Arguments for Socket::sendi2LSB()
Argument | Description |
|---|---|
| The integer to send; only the least-significant 2-bytes will be sent. |
Table 4.403. Exceptions Thrown by Socket::sendi2LSB()
err | desc |
|---|---|
| The socket is not connected. |
Sends a 4-byte integer in little-endian format over the socket. If an error occurs, -1 will be returned; in this case check errno() for the error number.
intSocket::sendi4LSB(softint$i = 0)
if ($sock.sendi4LSB($val) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.404. Arguments for Socket::sendi4LSB()
Argument | Description |
|---|---|
| The integer to send; only the least-significant 4-bytes will be sent. |
Table 4.406. Exceptions Thrown by Socket::sendi4LSB()
err | desc |
|---|---|
| The socket is not connected. |
Sends an 8-byte (64-bit) integer in little-endian format over the socket. If an error occurs, -1 will be returned; in this case check errno() for the error number.
intSocket::sendi8LSB(softint$i = 0)
if ($sock.sendi8LSB($val) == -1)
printf("error sending data: %s\n", strerror(errno()));Table 4.409. Exceptions Thrown by Socket::sendi8LSB()
err | desc |
|---|---|
| The socket is not connected. |
Table 4.410. Arguments for Socket::sendHTTPMessage()
Argument | Description |
|---|---|
| The HTTP method name to send (i.e. POST, HEAD, etc) |
| The path component of the URL. |
| The HTTP protocol version (1.0 or 1.1). |
| A hash of additional headers to send (key-value pairs). |
| If present (and does not have a length of zero), the body to be sent with the message (if present, the Content-Length header will be automatically set). |
Table 4.411. Exceptions Thrown by Socket::sendHTTPMessage()
err | desc |
|---|---|
| The socket is not connected. |
| Send failed. |
Creates a properly-formatted HTTP response message and sends it over the Socket.
nothingSocket::sendHTTPResponse(softint$status_code, string$desc, string$http_version, hash$headers, data $body = binary())
$sock.sendHTTPResponse(200, "OK", "1.1", ("Connection":"Keep-Alive"));Table 4.412. Arguments for Socket::sendHTTPResponse()
Argument | Description |
|---|---|
| The HTTP status code to send (i.e. 200, 404, etc) |
| The descriptive text for the status code. |
| The HTTP protocol version (1.0 or 1.1). |
| A hash of additional headers to send (key-value pairs). |
| If present (and does not have a length of zero), the body to be sent with the message (if present, the Content-Length header will be automatically set). |
Table 4.413. Exceptions Thrown by Socket::sendHTTPResponse()
err | desc |
|---|---|
| The socket is not connected. |
| Send failed. |
Receives data from the socket and returns a string. If any errors occur reading from the socket, an exception is raised.
stringSocket::recv(softint$size = 0, timeout$timeout_ms = -1)
my string $data = $sock.recv(-1); # read all data availableTable 4.414. Arguments for Socket::recv()
Argument | Description |
|---|---|
| The amount of data to read in bytes. To read until the remote closes the connection, use -1. |
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.415. Return Values for Socket::recv()
Return Type | Description |
|---|---|
The data read, returned as a string. If any errors occur reading from the socket, an exception is raised. |
Table 4.416. Exceptions Thrown by Socket::recv()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives data from the socket and returns a binary object. If any errors occur reading from the socket, an exception is raised.
binarySocket::recvBinary(softint$size = 0, timeout$timeout_ms = -1)
my binary $bin = $sock.recvBinary(-1); # read all data availableTable 4.417. Arguments for Socket::recvBinary()
Argument | Description |
|---|---|
| The amount of data to read in bytes. To read until the remote closes the connection, use -1. |
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.418. Return Values for Socket::recvBinary()
Return Type | Description |
|---|---|
The data read, returned as a binary object. If any errors occur reading from the socket, an exception is raised. |
Table 4.419. Exceptions Thrown by Socket::recvBinary()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 1-byte signed integer from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvi1(timeout$timeout_ms = -1)
my int $val = $sock.recvi1();Table 4.420. Arguments for Socket::recvi1()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.421. Return Values for Socket::recvi1()
Return Type | Description |
|---|---|
The 1-byte signed integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.422. Exceptions Thrown by Socket::recvi1()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 2-byte signed integer in big-endian format (network byte order) from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvi2(timeout$timeout_ms = -1)
my int $val = $sock.recvi2();Table 4.423. Arguments for Socket::recvi2()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.424. Return Values for Socket::recvi2()
Return Type | Description |
|---|---|
The 2-byte signed integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.425. Exceptions Thrown by Socket::recvi2()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 4-byte signed integer in big-endian format (network byte order) from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvi4(timeout$timeout_ms = -1)
my int $val = $sock.recvi4();Table 4.426. Arguments for Socket::recvi4()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.427. Return Values for Socket::recvi4()
Return Type | Description |
|---|---|
The 4-byte signed integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.428. Exceptions Thrown by Socket::recvi4()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives an 8-byte (64-bit) signed integer in big-endian format (network byte order) from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvi8(timeout$timeout_ms = -1)
my int $val = $sock.recvi8();Table 4.429. Arguments for Socket::recvi8()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. A relative date/time value can be used as well (i.e. |
Table 4.430. Return Values for Socket::recvi8()
Return Type | Description |
|---|---|
The 8-byte (64-bit) signed integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.431. Exceptions Thrown by Socket::recvi8()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 2-byte signed integer in little-endian format from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvi2LSB(timeout$timeout_ms = -1)
my int $val = $sock.recvi2LSB();Table 4.432. Arguments for Socket::recvi2LSB()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.433. Return Values for Socket::recvi2LSB()
Return Type | Description |
|---|---|
The 2-byte signed integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.434. Exceptions Thrown by Socket::recvi2LSB()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 4-byte signed integer in little-endian format from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvi4LSB(timeout$timeout_ms = -1)
my int $val = $sock.recvi4LSB(1250ms); # timeout in 1.25 secondsTable 4.435. Arguments for Socket::recvi4LSB()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.436. Return Values for Socket::recvi4LSB()
Return Type | Description |
|---|---|
The 4-byte signed integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.437. Exceptions Thrown by Socket::recvi4LSB()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives an 8-byte (64-bit) signed integer in little-endian format from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvi8LSB(timeout$timeout_ms = -1)
my int $val = $sock.recvi8LSB();Table 4.438. Arguments for Socket::recvi8LSB()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. A relative date/time value can be used as well (i.e. |
Table 4.439. Return Values for Socket::recvi8LSB()
Return Type | Description |
|---|---|
The 8-byte (64-bit) signed integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.440. Exceptions Thrown by Socket::recvi8LSB()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 1-byte unsigned integer from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvu1(timeout$timeout_ms = -1)
my int $val = $sock.recvu1();Table 4.441. Arguments for Socket::recvu1()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.442. Return Values for Socket::recvu1()
Return Type | Description |
|---|---|
The 1-byte unsigned integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.443. Exceptions Thrown by Socket::recvu1()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 2-byte unsigned integer in big-endian format (network byte order) from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvu2(timeout$timeout_ms = -1)
intSocket::recvu2(date$timeout)
my int $val = $sock.recvu2();Table 4.444. Arguments for Socket::recvu2()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.445. Return Values for Socket::recvu2()
Return Type | Description |
|---|---|
The 2-byte unsigned integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.446. Exceptions Thrown by Socket::recvu2()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 4-byte unsigned integer in big-endian format (network byte order) from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvu4(timeout$timeout_ms = -1)
my int $val = $sock.recvu4();Table 4.447. Arguments for Socket::recvu4()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.448. Return Values for Socket::recvu4()
Return Type | Description |
|---|---|
The 4-byte unsigned integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.449. Exceptions Thrown by Socket::recvu4()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 2-byte unsigned integer in little-endian format from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvu2LSB(timeout$timeout_ms = -1)
my int $val = $sock.recvu2LSB();Table 4.450. Arguments for Socket::recvu2LSB()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.451. Return Values for Socket::recvu2LSB()
Return Type | Description |
|---|---|
The 2-byte unsigned integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.452. Exceptions Thrown by Socket::recvu2LSB()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Receives a 4-byte unsigned integer in little-endian format from the socket. If any errors occur reading from the socket, an exception is raised.
intSocket::recvu4LSB(timeout$timeout_ms = -1)
my int $val = $sock.recvu4LSB();Table 4.453. Arguments for Socket::recvu4LSB()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.454. Return Values for Socket::recvu4LSB()
Return Type | Description |
|---|---|
The 4-byte unsigned integer read. If any errors occur reading from the socket, an exception is raised. |
Table 4.455. Exceptions Thrown by Socket::recvu4LSB()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Reads an HTTP header and returns a hash representing the data read. If any errors occur reading from the socket, an exception is raised. Accepts an optional timeout value in milliseconds.
This method sets the keys in the following table in the hash returned as well to give additional information about the HTTP header received.
Table 4.456. Special Keys in Hash Returned By Socket::readHTTPHeader()
Key | Description of Value |
|---|---|
| A string giving the HTTP version set in the header |
| An integer giving the status code; this key is only set in HTTP responses |
| If present in an HTTP response, this key will be set to the message after the status code |
| A string giving the HTTP method (i.e. |
| A string giving the path in a request without any decoding; use decode_url() to decode if necessary. |
anySocket::readHTTPHeader(timeout$timeout_ms = -1)
my hash $hash = $sock.readHTTPHeader();Table 4.457. Arguments for Socket::readHTTPHeader()
Argument | Description |
|---|---|
| The timeout in milliseconds (1/1000 second). If no timeout is passed, then the call will not time out and will not return until all the data has been read or the remote end closes the connection. |
Table 4.458. Return Values for Socket::readHTTPHeader()
Return Type | Description |
|---|---|
The return hash will contain keys for each header, plus an http_version key, giving the HTTP protocol version. For HTTP responses, the following keys will be returned: |
Table 4.459. Exceptions Thrown by Socket::readHTTPHeader()
err | desc |
|---|---|
| The socket is not connected. |
| The remote end has closed the connection. |
| There was an error receiving the data. |
| The data requested was not received in the timeout period. |
| There was an SSL error while reading data from the socket. |
Returns a Socket Information Hash about the remote end for connected sockets; if the socket is not connected, an exception is thrown.
my hash $h = $sock.getPeerInfo();Table 4.460. Return Values for Socket::getPeerInfo()
Return Type | Description |
|---|---|
Returns a Socket Information Hash about the remote end for connected sockets; if the socket is not connected, an exception is thrown. |
Table 4.461. Exceptions thrown by Socket::getPeerInfo()
err | desc |
|---|---|
| Socket is not open or error in getpeername() |
Table 4.462. Socket Information Hash
Key | Description |
|---|---|
| The interface name if available (ex: |
| A descriptive string giving the hostname and the address family if the hostname is available (ex: |
| A string giving the address (ex: |
| A descriptive string giving the address and the address family (ex: |
| An integer port number if available (note that this key is not present when retrieving information about UNIX sockets) |
| The network address family (see Network Address Family Constants) |
| A string describing the network address family (ex: |
Returns the port number of the socket for INET sockets.
$port = $sock.getPort();
Table 4.463. Return Values for Socket::getPort()
Return Type | Description |
|---|---|
Returns the port number for an INET connection, -1 if no INET connection has been established. |
Returns a Socket Information Hash about the local socket; if the socket is not open, an exception is thrown.
my hash $h = $sock.getSocketInfo();Table 4.464. Return Values for Socket::getSocketInfo()
Return Type | Description |
|---|---|
Returns a Socket Information Hash about the local socket; if the socket is not open, an exception is thrown. |
Table 4.465. Exceptions thrown by Socket::getSocketInfo()
err | desc |
|---|---|
| Socket is not open or error in getsockname() |
Closes an open socket. Also deletes the UNIX domain socket file if it was created by a call to Socket::bind(). Returns 0 for success, -1 for error; in this case check errno() for the error number.
$sock.close();
Returns the character encoding for the socket.
$enc = $sock.getCharset();
Table 4.468. Return Value for Socket::getCharset()
Return Type | Description |
|---|---|
The character encoding for the socket. |
Sets the character encoding for the socket. If any string data is sent over the socket with Socket::send(), then the character encoding will be automatically converted if needed.
nothingSocket::setCharset(string$encoding)
$sock.setCharset("ISO-8859-1");Table 4.469. Arguments for Socket::setCharset()
Argument | Description |
|---|---|
| The character encoding for the socket. |
Returns the TCP_NODELAY setting for the socket. See also Socket::setNoDelay().
$nodelay = $sock.getNoDelay();
Table 4.470. Return Value for Socket::getNoDelay()
Return Type | Description |
|---|---|
The |
Sets the TCP_NODELAY setting for the socket; when this setting is True, then data will be immediately sent out over the socket, when it is False, then data transmission may be delayed to be packaged with other data for the same target.
Delayed data transmissions may cause problems when the sender immediately closes the socket after sending data; in this case the receiver may not get the data even though the send succeeded.
Note that if no value is given to the method, the argument will be assumed to be True, and output buffering will be turned off for the socket.
See also Socket::getNoDelay().
intSocket::setNoDelay(bool$nodelay = True)
$sock.setNoDelay(True);Table 4.471. Arguments for Socket::setNoDelay()
Argument | Description |
|---|---|
| The |
Table 4.472. Return Value for Socket::setNoDelay()
Return Type | Description |
|---|---|
0 for success, non-zero for errors. To get error information, see errno() and strerror(). |
There are no comments yet