Qore Programming Language Reference Manual  0.8.12.11
 All Classes Namespaces Functions Variables Groups Pages
Qore::Socket Class Reference

The Socket class allows Qore programs safe access to network sockets. More...

Inheritance diagram for Qore::Socket:

Public Member Functions

Socket accept ()
 Accepts connections on a listening socket. More...
 
*Socket accept (timeout timeout_ms)
 Accepts connections on a listening socket (see Socket::listen()) accepting a timeout value with a millisecond resolution. More...
 
Socket acceptSSL ()
 Accepts connections on a listening socket and attempts to negotiate a TLS/SSL connection. More...
 
*Socket acceptSSL (timeout timeout_ms)
 Accepts connections on a listening socket and attempts to negotiate a TLS/SSL connection accepting a timeout value with a millisecond resolution. More...
 
int bind (string str, softbool reuseaddr=False)
 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. More...
 
int bind (int port, softbool reuseaddr=False)
 Opens and binds the socket to an INET port on all interfaces. More...
 
nothing bindINET (*string iface, *softstring service, softbool reuseaddr=False, softint family=AF_UNSPEC, softint socktype=SOCK_STREAM, softint protocol=0)
 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) More...
 
nothing bindUNIX (string path, softint socktype=SOCK_STREAM, softint protocol=0)
 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. More...
 
 clearStats ()
 Clears performance statistics. More...
 
nothing clearWarningQueue ()
 Removes any warning Queue object from the Socket. More...
 
int close ()
 Closes an open socket. More...
 
nothing connect (string target, timeout timeout_ms=-1)
 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. More...
 
nothing connectINET (string host, softstring service, timeout timeout_ms=-1, softint family=AF_UNSPEC, softint socktype=SOCK_STREAM, softint protocol=0)
 Connects to the given host and port with an optional timeout value with a millisecond resolution. More...
 
nothing connectINETSSL (string host, softstring service, timeout timeout_ms=-1, softint family=AF_UNSPEC, softint socktype=SOCK_STREAM, softint protocol=0)
 Connects to the given host and port and attempts to establish a TLS/SSL connection; accepts an optional timeout value with a millisecond resolution. More...
 
nothing connectSSL (string target, timeout timeout_ms=-1)
 Connects to a remote socket and attempts to establish a TLS/SSL connection; accepts an optional timeout value with a millisecond resolution. More...
 
nothing connectUNIX (string path, softint socktype=SOCK_STREAM, softint protocol=0)
 Connects to a UNIX domain socket file. More...
 
nothing connectUNIXSSL (string path, softint socktype=SOCK_STREAM, softint protocol=0)
 Connects to the given UNIX domain socket file and attempts to establish a TLS/SSL connection. More...
 
 constructor ()
 Creates the socket object. More...
 
 copy ()
 Creates a new Socket object, not based on the source being copied. More...
 
string getCharset ()
 Returns the character encoding for the socket. More...
 
string getEncoding ()
 Returns the character encoding for the socket. More...
 
bool getNoDelay ()
 Returns the TCP_NODELAY setting for the socket. More...
 
hash getPeerInfo (bool host_lookup=True)
 Returns a hash of information about the remote end for connected sockets. More...
 
int getPort ()
 Returns the port number of the socket for INET sockets. More...
 
int getRecvTimeout ()
 Returns the receive timeout socket option value as an integer in milliseconds. More...
 
*string getSSLCipherName ()
 Returns the name of the cipher for an encrypted connection or NOTHING if a secure connection has not been established. More...
 
*string getSSLCipherVersion ()
 Returns the version string of the cipher for an encrypted connection or NOTHING if a secure connection has not been established. More...
 
int getSendTimeout ()
 Returns the send timeout socket option value as an integer in milliseconds. More...
 
int getSocket ()
 Returns the socket file descriptor number. More...
 
hash getSocketInfo (bool host_lookup=True)
 Returns information about the local socket as a hash. More...
 
hash getUsageInfo ()
 Returns performance statistics for the socket. More...
 
bool isDataAvailable (timeout timeout_ms=0)
 Returns True or False depending on whether there is data to be read on the socket. More...
 
bool isOpen ()
 Returns True if the socket is open. More...
 
bool isSecure ()
 Returns True if the connection is a secure TLS/SSL connection. More...
 
bool isWriteFinished (timeout timeout_ms=0)
 Returns True or False depending on whether all the data has been written to the socket. More...
 
int listen (int backlog=20)
 Listens for connections on a bound socket; sets the socket in a listening state. More...
 
bool pendingHttpChunkedBody ()
 returns True if the socket is still connected, and a HTTP header was read indicating chunked transfer encoding, but no chunked body has been read yet More...
 
hash readHTTPChunkedBody (timeout timeout_ms=-1)
 Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a string in the "body" key of a hash (including footers received) More...
 
hash readHTTPChunkedBodyBinary (timeout timeout_ms=-1)
 Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a binary object in the "body" key of a hash (including footers received) More...
 
 readHTTPChunkedBodyBinaryWithCallback (code rcb, timeout timeout_ms=-1)
 Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a string in the "body" key of a hash (including footers received) More...
 
 readHTTPChunkedBodyWithCallback (code rcb, timeout timeout_ms=-1)
 Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a string in the "body" key of a hash (including footers received) More...
 
hash readHTTPHeader (timeout timeout_ms=-1, *reference info)
 Retuns a hash representing the data in the HTTP header read, or, if the data cannot be parsed as an HTTP header, then an exception is thrown, and the data read is returned as a string in the arg key of the exception hash. More...
 
string readHTTPHeaderString (timeout timeout_ms=-1)
 Retuns a string representing the data in the HTTP header read (reads until "\r\n\r\n") More...
 
string recv (softint size=0, timeout timeout_ms=-1)
 Receives data from the socket and returns a string tagged with the Socket's character encoding. More...
 
binary recvBinary (softint size=0, timeout timeout_ms=-1)
 Receives data from the socket and returns a binary object. More...
 
int recvi1 (timeout timeout_ms=-1)
 Receives a 1-byte signed integer from the socket. More...
 
int recvi2 (timeout timeout_ms=-1)
 Receives a 2-byte (16-bit) signed integer in big-endian format (network byte order) from the socket. More...
 
int recvi2LSB (timeout timeout_ms=-1)
 Receives a 2-byte (16-bit) signed integer in little-endian format from the socket. More...
 
int recvi4 (timeout timeout_ms=-1)
 Receives a 4-byte (32-bit) signed integer in big-endian format (network byte order) from the socket. More...
 
int recvi4LSB (timeout timeout_ms=-1)
 Receives a 4-byte (32-bit) signed integer in little-endian format from the socket. More...
 
int recvi8 (timeout timeout_ms=-1)
 Receives an 8-byte (64-bit) signed integer in big-endian format (network byte order) from the socket. More...
 
int recvi8LSB (timeout timeout_ms=-1)
 Receives an 8-byte (64-bit) signed integer in little-endian format from the socket. More...
 
int recvu1 (timeout timeout_ms=-1)
 Receives a 1-byte unsigned integer from the socket. More...
 
int recvu2 (timeout timeout_ms=-1)
 Receives a 2-byte (16-bit) unsigned integer in big-endian format (network byte order) from the socket. More...
 
int recvu2LSB (timeout timeout_ms=-1)
 Receives a 2-byte (16-bit) unsigned integer in little-endian format from the socket. More...
 
int recvu4 (timeout timeout_ms=-1)
 Receives a 4-byte (32-bit) unsigned integer in big-endian format (network byte order) from the socket. More...
 
int recvu4LSB (timeout timeout_ms=-1)
 Receives a 4-byte (32-bit) unsigned integer in little-endian format from the socket. More...
 
int send (binary bin, timeout timeout_ms=-1)
 Sends binary data over the socket; if any errors occur, an exception is thrown. More...
 
int send (string str, timeout timeout_ms=-1)
 Sends string data over the socket; string data is converted to the socket's encoding if necessary; if any errors occur, an exception is thrown. More...
 
nothing send2 (binary bin, timeout timeout_ms=-1)
 Sends binary data over the socket; if any errors occur, an exception is thrown. More...
 
nothing send2 (string str, timeout timeout_ms=-1)
 Sends string data over the socket; string data is converted to the socket's encoding if necessary; if any errors occur, an exception is thrown. More...
 
int sendBinary (string str, timeout timeout_ms=-1)
 Sends string data over the socket without converting the string to the socket's encoding, but instead is sent exactly as-is; if any errors occur, an exception is thrown. More...
 
int sendBinary (binary bin, timeout timeout_ms=-1)
 Sends binary data over the socket; if any errors occur, an exception is thrown. More...
 
nothing sendBinary2 (string str, timeout timeout_ms=-1)
 Sends string data over the socket without converting the string to the socket's encoding, but instead is sent exactly as-is; if any errors occur, an exception is thrown. More...
 
nothing sendBinary2 (binary bin, timeout timeout_ms=-1)
 Sends binary data over the socket; if any errors occur, an exception is thrown. More...
 
nothing sendHTTPMessage (string method, string path, string http_version, hash headers, *string body, *reference info, timeout timeout_ms=-1)
 Sends an HTTP message with a method and user-defined headers given as a hash and an optional message body. More...
 
nothing sendHTTPMessage (string method, string path, string http_version, hash headers, binary body, *reference info, timeout timeout_ms=-1)
 Sends an HTTP message with a method and user-defined headers given as a hash and an optional message body. More...
 
nothing sendHTTPMessageWithCallback (code scb, string method, string path, string http_version, hash headers, *reference info, timeout timeout_ms=-1)
 Sends an HTTP message with a method and user-defined headers given as a hash and an optional message body. More...
 
nothing sendHTTPResponse (softint status_code, string status_desc, string http_version, hash headers, *string body, timeout timeout_ms=-1)
 Sends an HTTP response with user-defined headers given as a hash and an optional message body. More...
 
nothing sendHTTPResponse (softint status_code, string status_desc, string http_version, hash headers, binary body, timeout timeout_ms=-1)
 Sends an HTTP response with user-defined headers given as a hash and a message body as literal binary data. More...
 
nothing sendHTTPResponseWithCallback (code scb, softint status_code, string status_desc, string http_version, hash headers, timeout timeout_ms=-1)
 Sends an HTTP response with user-defined headers given as a hash and a message body as literal binary data. More...
 
int sendi1 (softint i=0, timeout timeout_ms=-1)
 Sends a 1-byte integer over the socket. More...
 
int sendi2 (softint i=0, timeout timeout_ms=-1)
 Sends a 2-byte (16-bit) integer in big-endian format (network byte order) over the socket. More...
 
int sendi2LSB (softint i=0, timeout timeout_ms=-1)
 Sends a 2-byte (16-bit) integer in little-endian format over the socket. More...
 
int sendi4 (softint i=0, timeout timeout_ms=-1)
 Sends a 4-byte (32-bit) integer in big-endian format (network byte order) over the socket. More...
 
int sendi4LSB (softint i=0, timeout timeout_ms=-1)
 Sends a 4-byte (32-bit) integer in little-endian format over the socket. More...
 
int sendi8 (softint i=0, timeout timeout_ms=-1)
 Sends an 8-byte (64-bit) integer in big-endian format (network byte order) over the socket. More...
 
int sendi8LSB (softint i=0, timeout timeout_ms=-1)
 Sends an 8-byte (64-bit) integer in little-endian format over the socket. More...
 
nothing setCertificate (SSLCertificate cert)
 Sets the X.509 certificate to use for negotiating encrypted connections. More...
 
nothing setCertificate (string cert_pem)
 Sets the X.509 certificate to use for negotiating encrypted connections from the PEM-encoded string representing the X.509 certificate. More...
 
nothing setCertificate (binary cert_der)
 Sets the X.509 certificate to use for negotiating encrypted connections from the DER-encoded binary object representing the X.509 certificate. More...
 
nothing setCharset (string encoding)
 Sets the character encoding for the socket. More...
 
nothing setEncoding (string encoding)
 Sets the character encoding for the socket. More...
 
nothing setEventQueue ()
 Removes any Queue object from the Socket object so that socket events are no longer added to the Queue. More...
 
nothing setEventQueue (Queue queue)
 Sets a Queue object to receive socket events. More...
 
int setNoDelay (bool nd=True)
 Sets the boolean TCP_NODELAY setting for the socket. More...
 
nothing setPrivateKey (SSLPrivateKey key)
 Sets the private key to use for negotiating encrypted connections along with the X.509 certificate. More...
 
nothing setPrivateKey (string key_pem, *string pass)
 Sets the private key to use for negotiating encrypted connections along with the X.509 certificate from a PEM-encoded string representing the private key and an optional password. More...
 
nothing setPrivateKey (binary key_der)
 Sets the private key to use for negotiating encrypted connections along with the X.509 certificate from a DER-encoded binary object representing the private key. More...
 
int setRecvTimeout (timeout timeout_ms)
 sets the receive timeout as a socket option More...
 
int setSendTimeout (timeout timeout_ms)
 sets the send timeout as a socket option More...
 
nothing setWarningQueue (int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms=1s)
 Sets a Queue object to receive socket warnings. More...
 
int shutdown ()
 Ensures that a socket will be closed even if the file descriptor is shared with other processes (for example, after a call to fork()) More...
 
nothing shutdownSSL ()
 Shuts down the SSL connection on a secure connection. More...
 
nothing upgradeClientToSSL (timeout timeout_ms=-1)
 Upgrades a client socket connection to a TLS/SSL connection. More...
 
nothing upgradeServerToSSL (timeout timeout_ms=-1)
 Upgrades a server socket connection to a TLS/SSL connection. More...
 
*string verifyPeerCertificate ()
 Returns a string code giving the result of verifying the remote certificate or NOTHING if an encrypted connection is not currently established. More...
 

Detailed Description

The Socket class allows Qore programs safe access to network sockets.

Restrictions:
Qore::PO_NO_NETWORK
Note
This class is not available with the PO_NO_NETWORK parse option.

Non-blocking socket I/O can be performed by appending a timeout value in milliseconds all Socket methods performing I/O operations, 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:

1 bool rc = socket.isDataAvailable(1250ms); # times out in 1.25 seconds

Socket objects can automatically convert character encodings if desired when sending string data with Socket::send(). Use the Socket::setEncoding() 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 I/O Event Handling for more information.

The events raised by this object are listed in the following table:

Socket Events

Name Description
EVENT_PACKET_READ Raised when a network packet is received.
EVENT_PACKET_SENT Raised when a network packet is sent.
EVENT_CHANNEL_CLOSED Raised when a socket is closed.
EVENT_DELETED Raised when the object being monitored is deleted.
EVENT_HOSTNAME_LOOKUP Raised when a hostname lookup is attempted.
EVENT_HOSTNAME_RESOLVED Raised when a hostname lookup is resolved.
EVENT_HTTP_SEND_MESSAGE Raised when an HTTP message is sent.
EVENT_HTTP_MESSAGE_RECEIVED Raised when an HTTP message is received.
EVENT_CONNECTING Raised right before a socket connection attempt is made.
EVENT_CONNECTED Raised when the socket connection has been established.
EVENT_START_SSL Raised when socket SSL negotiation starts.
EVENT_SSL_ESTABLISHED Raised when SSL communication has been negotiated and established.

Socket Information Hash

Key Description
hostname The interface name if available (ex: "localhost"; note that this key is not present when retrieving information about UNIX sockets)
hostname_desc A descriptive string giving the hostname and the address family if the hostname is available (ex: "ipv6[localhost]"; note that this key is not present when retrieving information about UNIX sockets)
address A string giving the address (ex: "::ffff:0.0.0.0")
address_desc A descriptive string giving the address and the address family (ex: "ipv6[::ffff:0.0.0.0]")
port An integer port number if available (note that this key is not present when retrieving information about UNIX sockets)
family The network address family (see Network Address Family Constants)
familystr A string describing the network address family (ex: "ipv4")

Member Function Documentation

Socket Qore::Socket::accept ( )

Accepts connections on a listening socket.

Accepts connections on a listening socket; 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.

Example:
1 Socket new_socket = sock.accept();
Returns
a new Socket object is returned for the new connection
Exceptions
SOCKET-NOT-OPENThe socket is not bound
SOCKET-ACCEPT-ERRORError in accepting connection
See Also
Socket::acceptSSL(), Socket::listen(), Socket::getPeerInfo()
*Socket Qore::Socket::accept ( timeout  timeout_ms)

Accepts connections on a listening socket (see Socket::listen()) accepting a timeout value with a millisecond resolution.

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.

Example:
1 *Socket new_socket = sock.accept(30s);
Parameters
timeout_msIf a timeout value is passed and the connection takes longer to establish than the timeout, then NOTHING is returned.Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
If no connection is accepted within the timeout period, then NOTHING is returned, otherwise a Socket object for the new connection is returned.
Exceptions
SOCKET-NOT-OPENThe socket is not bound
SOCKET-ACCEPT-ERRORError in accepting connection
See Also
Socket::acceptSSL(), Socket::listen(), Socket::getPeerInfo()
Socket Qore::Socket::acceptSSL ( )

Accepts connections on a listening socket and attempts to negotiate a TLS/SSL connection.

Accepts connections on a listening socket and attempts to negotiate a TLS/SSL connection; 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.

Example:
1 Socket new_socket = sock.acceptSSL();
Returns
When a new connection is accepted and a TLS/SSL session has been successfully negotiated, a new Socket object is returned for the new connectio
Exceptions
SOCKET-NOT-OPENThe socket is not bound
SOCKET-ACCEPT-ERRORError in accepting connection
See Also
Socket::accept(), Socket::listen(), Socket::getPeerInfo()
*Socket Qore::Socket::acceptSSL ( timeout  timeout_ms)

Accepts connections on a listening socket and attempts to negotiate a TLS/SSL connection accepting a timeout value with a millisecond resolution.

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.

Example:
1 *Socket new_socket = sock.acceptSSL(30s);
Parameters
timeout_msIf a timeout value is passed and the connection takes longer to establish than the timeout, then NOTHING is returned. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
If no connection is accepted within the timeout period, then NOTHING is returned, otherwise a Socket object for the new connection is returned.
Exceptions
SOCKET-NOT-OPENThe socket is not bound
SOCKET-ACCEPT-ERRORError in accepting connection
See Also
Socket::acceptSSL(), Socket::listen(), Socket::getPeerInfo()
int Qore::Socket::bind ( string  str,
softbool  reuseaddr = False 
)

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 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).

If any errors occur a non-zero error code is returned.

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.

Example:
1  # bind to a UNIX socket on the local system and reuse the address, check return code
2 if (sock.bind("/tmp/my-socket", True))
3  throw "BIND-ERROR", strerror();
Parameters
strIf a colon appears in the string, the string will be assumed to be a "bind_address: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. 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 (if surrounded by square brackets) and binds them as such.
reuseaddrIf this optional argument evaluates to True, the SO_REUSEADDR option will be set on the socket, which will allow the socket to be bound to a port that is not yet closed (for example, in a TIME_WAIT state); note that this only applies to IPv4 (AF_INET) and IPv6 (AF_INET6) sockets; this option is ignored for UNIX (AF_UNIX) sockets.
See Also
Socket::bindINET() and Socket::bindUNIX()
Note
UNIX domain sockets are not available on native Windows ports
int Qore::Socket::bind ( int  port,
softbool  reuseaddr = False 
)

Opens and binds the socket to an INET port on all interfaces.

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).

If any errors occur a non-zero error code is returned.

Example:
1  # bind to port 80 on all interfaces on the local system and reuse the address, check return code
2 if (sock.bind(80, True))
3  throw "BIND-ERROR", strerror();
Parameters
portA port number to bind to on all interfaces
reuseaddrIf this optional argument evaluates to True, the SO_REUSEADDR option will be set on the socket, which will allow the socket to be bound to a port that is not yet closed (for example, in a TIME_WAIT state)
See Also
Socket::bindINET() and Socket::bindUNIX()
nothing Qore::Socket::bindINET ( *string  iface,
*softstring  service,
softbool  reuseaddr = False,
softint  family = AF_UNSPEC,
softint  socktype = SOCK_STREAM,
softint  protocol = 0 
)

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)

Opens and binds the socket to a port, interface and port (if the interface 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.

Examples:
1  # bind to port 80 on all interfaces on the local system and reuse the address
2 sock.bindINET(NOTHING, 80, True);
1  # bind to interface 192.168.2.23 port 8080 and do not reuse the address
2 sock.bindINET("192.168.2.23", 8080);
1  # bind to localhost port 8080 with ipv6 and do not reuse the address
2 sock.bindINET("localhost", 8080, False, AF_INET6);
1  # bind to ipv6 host address fe80::21c:42ff:fe00:8, port 1001, reuse the address
2 sock.bindINET("fe80::21c:42ff:fe00:8", 1001, True);
Parameters
ifacethe host name or IP address to bind to
servicethe service name (ex: "http" or port number (given as or converted to a string) to bind to
reuseaddrif this optional argument evaluates to True, the SO_REUSEADDR option will be set on the socket, which will allow the socket to be bound to a port that is not yet closed (for example, in a TIME_WAIT state)
familythe address family to use to bind; see Network Address Family Constants; AF_UNSPEC means to try all possible addres families
socktypethe type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets
protocolthe protocol number for the socket; use 0 for the default protocol
Exceptions
SOCKET-BIND-ERRORBoth hostname and service name are empty or not set; error opening socket for bind; error binding on socket.
QOREADDRINFO-GETINFO-ERRORerror looking up either nodename or servicename (or not known)
See Also
Socket::bind() and Socket::bindUNIX()
nothing Qore::Socket::bindUNIX ( string  path,
softint  socktype = SOCK_STREAM,
softint  protocol = 0 
)

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.

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.

Example:
1  # bind to UNIX domain socket file "/tmp/socket"
2 sock.bindUNIX("/tmp/socket");
Parameters
pathThe path of the UNIX domain socket to create and bind to
socktypethe type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets
protocolthe protocol number for the socket; use 0 for the default protocol
Exceptions
SOCKET-BIND-ERRORError opening socket for bind; error binding on socket
See Also
Socket::bind() and Socket::bindINET()
Note
UNIX domain sockets are not available on native Windows ports
Qore::Socket::clearStats ( )

Clears performance statistics.

Example:
1 sock.clearStats();
Since
Qore 0.8.9
See Also
Socket::getUsageInfo()
nothing Qore::Socket::clearWarningQueue ( )

Removes any warning Queue object from the Socket.

Example:
1 sock.clearWarningQueue();
See Also
Socket::setWarningQueue()
Since
Qore 0.8.9
int Qore::Socket::close ( )

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

Example:
1 if (sock.close())
2  stderr.printf("Error closing socket: %s\n", strerror(errno()));
Events:
EVENT_CHANNEL_CLOSED
Returns
0 for success, -1 for error; in this case check errno() for the error number
nothing Qore::Socket::connect ( string  target,
timeout  timeout_ms = -1 
)

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.

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.

Examples:
1 # connect to ipv4 adress 192.168.1.45 port 8080 with a 30 second timeout
2 sock.connect("192.168.1.45:8080", 30s);
1 # connect to ipv6 adress 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 10 second timeout
2 sock.connect("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80", 10s);
1 # connect to localhost using ipv6 (::1) port 80 with a 10 second timeout
2 sock.connect("[localhost]:80", 15s);
1 # connect to UNIX domain socket file "/tmp/socket"
2 sock.connect("/tmp/socket");
Events:
EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED
Parameters
targetThe target address interpreted with the following rules:
  • If a colon appears in the string, the string will be assumed to be a "hostname:port" specification to connect to; where "port" can also be a service name (ex: "example.com:https")
  • 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.
  • 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 square brackets, it will be resolved as an ipv6 address (ex: "[localhost]:80" will connect to port 80 on ::1, assuming "localhost" can be resolved to the ipv6 loopback address on the system).
timeout_msIf a timeout value is passed and the connection takes longer to establish than the timeout, a "SOCKET-CONNECT-ERROR" exception is thrown. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-CONNECT-ERRORAn error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc).
See Also
Socket::connectUNIX(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectUNIXSSL()
nothing Qore::Socket::connectINET ( string  host,
softstring  service,
timeout  timeout_ms = -1,
softint  family = AF_UNSPEC,
softint  socktype = SOCK_STREAM,
softint  protocol = 0 
)

Connects to the given host and port with an optional timeout value with a millisecond resolution.

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").

Examples:
1 # connect to ipv4 address 192.168.1.45 port 8080 with a 30 second timeout
2 sock.connectINET("192.168.1.45", 8080, 30s);
1 # connect to ipv6 address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 20 second timeout
2 sock.connectINET("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 80, 20s);
Events:
EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED
Parameters
hostThe host name or IP address to connect to
serviceThe service name (ex: "http" or port number (given as or converted to a string) to connect to
timeout_msIf a timeout value is passed and the connection takes longer to establish than the timeout, a "SOCKET-CONNECT-ERROR" exception is thrown. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
familyThe address family to use to connect to the remote socket; see Network Address Family Constants
socktypeThe type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets
protocolThe protocol number for the socket (if not sure leave this 0)
Exceptions
SOCKET-CONNECT-ERRORAn error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc)
See Also
Socket::connect(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectUNIXSSL()
nothing Qore::Socket::connectINETSSL ( string  host,
softstring  service,
timeout  timeout_ms = -1,
softint  family = AF_UNSPEC,
softint  socktype = SOCK_STREAM,
softint  protocol = 0 
)

Connects to the given host and port and attempts to establish a TLS/SSL connection; accepts an optional timeout value with a millisecond resolution.

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").

Examples:
1  # connect to 192.168.1.45 port 8080 with a 30 second timeout
2 sock.connectINETSSL("192.168.1.45", 8080, 30s);
1  # connect to ipv6 address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 20 second timeout
2 sock.connectINETSSL("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 80, 20s);
Events:
EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED
Parameters
hostThe host name or IP address to connect to
serviceThe service name (ex: "http" or port number (given as or converted to a string) to connect to
timeout_msIf a timeout value is passed and the connection takes longer to establish than the timeout, a "SOCKET-CONNECT-ERROR" exception is thrown. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
familyThe address family to use to connect to the remote socket; see Network Address Family Constants
socktypeThe type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets
protocolThe protocol number for the socket (if not sure leave this 0)
Exceptions
SOCKET-CONNECT-ERRORAn error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc)
SOCKET-SSL-ERRORAn error occurred establishing the TLS/SSL connection
See Also
Socket::connect(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINET(), and Socket::connectUNIXSSL()
nothing Qore::Socket::connectSSL ( string  target,
timeout  timeout_ms = -1 
)

Connects to a remote socket and attempts to establish a TLS/SSL connection; accepts an optional timeout value with a millisecond resolution.

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 square 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).

Examples:
1  # connect to ipv4 adress 192.168.1.45 port 8080 with a 30 second timeout
2 sock.connectSSL("192.168.1.45:8080", 30s);
1  # connect to ipv6 adress 2001:0db8:85a3:0000:0000:8a2e:0370:7334 port 80 with a 10 second timeout
2 sock.connectSSL("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80", 10s);
1  # connect to localhost using ipv6 (::1) port 80 with a 10 second timeout
2 sock.connectSSL("[localhost]:80", 15s);
1  # connect to UNIX domain socket file "/tmp/socket"
2 sock.connectSSL("/tmp/socket");
Events:
EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED
Parameters
targetThe target address interpreted with the following rules:
  • If a colon appears in the string, the string will be assumed to be a "hostname:port" specification to connect to; where "port" can also be a service name (ex: "example.com:https")
  • 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.
  • 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 square brackets, it will be resolved as an ipv6 address (ex: "[localhost]:80" will connect to port 80 on ::1, assuming "localhost" can be resolved to the ipv6 loopback address on the system).
timeout_msif a timeout value is passed and the connection takes longer to establish than the timeout, an exception is thrown. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-CONNECT-ERRORAn error occured connecting to the remote socket (cannot resolve hostname, no listener, timeout exceeded, etc)
SOCKET-SSL-ERRORAn error occurred establishing the TLS/SSL connection
See Also
Socket::connect(), Socket::connectUNIX(), Socket::connectUNIXSSL(), Socket::connectINETSSL(), and Socket::connectINET()
nothing Qore::Socket::connectUNIX ( string  path,
softint  socktype = SOCK_STREAM,
softint  protocol = 0 
)

Connects to a UNIX domain socket file.

Connects the socket to the given UNIX domain socket file; if any errors occur, an exception is thrown

Example:
1  # connect to UNIX domain socket file "/tmp/socket"
2 sock.connectUNIX("/tmp/socket");
Events:
EVENT_CONNECTING, EVENT_CONNECTED
Parameters
pathThe socket will try to connect to a UNIX domain socket file on the local filesystem with the given name
socktypeThe type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets
protocolThe protocol number for the socket (if not sure leave this 0)
Exceptions
SOCKET-CONNECT-ERRORAn error occured connecting to the socket
See Also
Socket::connect(), Socket::connectINET(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectUNIXSSL()
Note
UNIX domain sockets are not available on native Windows ports
nothing Qore::Socket::connectUNIXSSL ( string  path,
softint  socktype = SOCK_STREAM,
softint  protocol = 0 
)

Connects to the given UNIX domain socket file and attempts to establish a TLS/SSL connection.

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.

Example:
1  # connect to UNIX domain socket file "/tmp/socket"
2 sock.connectUNIXSSL("/tmp/socket");
Events:
EVENT_CONNECTING, EVENT_CONNECTED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED
Parameters
pathThe socket will try to connect to a UNIX domain socket file on the local filesystem with the given name
socktypeThe type of socket; see Socket Type Constants; typically SOCK_STREAM for TCP sockets
protocolThe protocol number for the socket (if not sure leave this 0)
Exceptions
SOCKET-CONNECT-ERRORAn error occured connecting to the socket
SOCKET-SSL-ERRORAn error occurred establishing the TLS/SSL connection
See Also
Socket::connect(), Socket::connectUNIX(), Socket::connectSSL(), Socket::connectINETSSL(), and Socket::connectINET()
Note
UNIX domain sockets are not available on native Windows ports
Qore::Socket::constructor ( )

Creates the socket object.

Example:
1 Socket sock();
Qore::Socket::copy ( )

Creates a new Socket object, not based on the source being copied.

Example:
1 Socket newsock = sock.copy();
string Qore::Socket::getCharset ( )

Returns the character encoding for the socket.

Code Flags:
CONSTANT

A method synonym for Socket::getEncoding(), kept for backwards-compatibility

Returns
the character encoding for the socket
string Qore::Socket::getEncoding ( )

Returns the character encoding for the socket.

Code Flags:
CONSTANT
Returns
the character encoding for the socket
bool Qore::Socket::getNoDelay ( )

Returns the TCP_NODELAY setting for the socket.

Code Flags:
CONSTANT
Example:
1 bool b = sock.getNoDelay();
Returns
the TCP_NODELAY setting for the socket
See Also
Socket::setNoDelay() for a description of the TCP_NODELAY setting
hash Qore::Socket::getPeerInfo ( bool  host_lookup = True)

Returns a hash of information about the remote end for connected sockets.

If the socket is not connected, an exception is thrown

Example:
1 hash h = sock.getPeerInfo();
Parameters
host_lookupdo a lookup of the hostname; if this is False then "hostname" and "hostname_desc" are not present in the response hash
Returns
a hash of information about the remote end for connected sockets
Exceptions
SOCKET-GETPEERINFO-ERRORSocket is not open or error in getpeername()
Since
Qore 0.8.8 added the host_lookup parameter
int Qore::Socket::getPort ( )

Returns the port number of the socket for INET sockets.

Code Flags:
CONSTANT
Example:
1 int port = sock.getPort();
Returns
the port number for an INET connection, -1 if no INET connection has been established
int Qore::Socket::getRecvTimeout ( )

Returns the receive timeout socket option value as an integer in milliseconds.

Code Flags:
CONSTANT
Example:
1 int to = sock.getRecvTimeout();
Returns
the receive timeout socket option value as an integer in milliseconds
int Qore::Socket::getSendTimeout ( )

Returns the send timeout socket option value as an integer in milliseconds.

Code Flags:
CONSTANT
Example:
1 int to = sock.getSendTimeout();
Returns
the send timeout socket option value as an integer in milliseconds
int Qore::Socket::getSocket ( )

Returns the socket file descriptor number.

Code Flags:
CONSTANT
Example:
1 int sock = sock.getSocket();
Returns
the socket file descriptor number or -1 if the socket is not open
hash Qore::Socket::getSocketInfo ( bool  host_lookup = True)

Returns information about the local socket as a hash.

If the socket is not open, an exception is thrown

Example:
1 hash h = sock.getSocketInfo();
Parameters
host_lookupdo a lookup of the hostname; if this is False then "hostname" and "hostname_desc" are not present in the response hash
Returns
a hash of information about the remote end for connected sockets
Exceptions
SOCKET-GETSOCKETINFO-ERRORSocket is not open or error in getsockname()
Since
Qore 0.8.8 added the host_lookup parameter
*string Qore::Socket::getSSLCipherName ( )

Returns the name of the cipher for an encrypted connection or NOTHING if a secure connection has not been established.

Code Flags:
CONSTANT
Example:
1 *string str = sock.getSSLCipherName();
Returns
the name of the cipher for an encrypted connection or NOTHING if a secure connection has not been established
*string Qore::Socket::getSSLCipherVersion ( )

Returns the version string of the cipher for an encrypted connection or NOTHING if a secure connection has not been established.

Code Flags:
CONSTANT
Example:
1 *string str = sock.getSSLCipherVersion();
Returns
the version string of the cipher for an encrypted connection or NOTHING if a secure connection has not been established
hash Qore::Socket::getUsageInfo ( )

Returns performance statistics for the socket.

Code Flags:
CONSTANT
Example:
1 hash h = sock.getUsageInfo();
Returns
a hash with the following keys:
  • "bytes_sent": an integer giving the total amount of bytes sent
  • "bytes_recv": an integer giving the total amount of bytes received
  • "us_sent": an integer giving the total number of microseconds spent sending data
  • "us_recv": an integer giving the total number of microseconds spent receiving data
  • "arg": (only if warning values have been set with Socket::setWarningQueue()) the optional argument for warning hashes
  • "timeout": (only if warning values have been set with Socket::setWarningQueue()) the warning timeout in microseconds
  • "min_throughput": (only if warning values have been set with Socket::setWarningQueue()) the minimum warning throughput in bytes/sec
Since
Qore 0.8.9
See Also
Socket::clearStats()
bool Qore::Socket::isDataAvailable ( timeout  timeout_ms = 0)

Returns True or False depending on whether there is data to be read on the socket.

With a timeout of zero this method returns immediately and can be used for non-blocking polling the socket for data (can also be used to poll for new connections before Socket::accept()).

Example:
1 bool b = sock.isDataAvailable();
Parameters
timeout_msan optional timeout in milliseconds (1/1000 second); if no timeout is given, the method returns immediately; Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
True if data is available on the Socket within the timeout period, False if not
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end closed the connection without sending any data
bool Qore::Socket::isOpen ( )

Returns True if the socket is open.

Code Flags:
CONSTANT
Example:
1 bool b = sock.isOpen();
Returns
True if the socket is open, False if not
bool Qore::Socket::isSecure ( )

Returns True if the connection is a secure TLS/SSL connection.

Code Flags:
CONSTANT
Example:
1 bool b = sock.isSecure();
Returns
True if the connection is encrypted, False if not
bool Qore::Socket::isWriteFinished ( timeout  timeout_ms = 0)

Returns True or False depending on whether all the data has been written to the socket.

With a timeout of zero this method returns immediately.

Example:
1 bool b = sock.isWriteFinished();
Parameters
timeout_msan optional timeout in milliseconds (1/1000 second); if no timeout is given, the method returns immediately; Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
True if the send action completes within the timeout period, False if not
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end closed the connection without sending any data
int Qore::Socket::listen ( int  backlog = 20)

Listens for connections on a bound socket; sets the socket in a listening state.

Listens for new connections on a bound socket.

Example:
1 sock.listen();
Parameters
backlogthe size of the queue for pending connections
Returns
Returns 0 for success, -1 for error
Exceptions
SOCKET-NOT-OPENThe socket is not bound
Since
Qore 0.8.8 the backlog parameter was added
bool Qore::Socket::pendingHttpChunkedBody ( )

returns True if the socket is still connected, and a HTTP header was read indicating chunked transfer encoding, but no chunked body has been read yet

Example:
1 bool b = sock.pendingHttpChunkedBody();
Since
Qore 0.8.10
hash Qore::Socket::readHTTPChunkedBody ( timeout  timeout_ms = -1)

Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a string in the "body" key of a hash (including footers received)

If any errors are encountered, an exception is raised

Example:
1 hash ans = sock.readHTTPChunkedBody(20s);
Events:
EVENT_HTTP_CHUNK_SIZE, EVENT_HTTP_FOOTERS_RECEIVED
Parameters
timeout_msThe timeout in milliseconds (1/1000 second). If no timeout or if a negative 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. If a timeout occurs, a "SOCKET-TIMEOUT" exception is raised. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
a hash of any HTTP footers received (with footer keys converted to lower case), with the body returned in the "body" key as a string in the Socket's encoding
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end has closed the connection
SOCKET-RECV-ERRORThere was an error receiving the data
SOCKET-TIMEOUTThe data requested was not received in the timeout period
SOCKET-SSL-ERRORThere was an SSL error while reading data from the socket
READ-HTTP-CHUNK-ERRORnegative value given for chunk size by server
hash Qore::Socket::readHTTPChunkedBodyBinary ( timeout  timeout_ms = -1)

Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a binary object in the "body" key of a hash (including footers received)

If any errors are encountered, an exception is raised

Example:
1 hash ans = sock.readHTTPChunkedBodyBinary(20s);
Events:
EVENT_HTTP_CHUNK_SIZE, EVENT_HTTP_FOOTERS_RECEIVED
Parameters
timeout_msThe timeout in milliseconds (1/1000 second). If no timeout or if a negative 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. If a timeout occurs, a "SOCKET-TIMEOUT" exception is raised. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
a hash of any HTTP footers received (with footer keys converted to lower case), with the body returned in the "body" key as a binary object
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end has closed the connection
SOCKET-RECV-ERRORThere was an error receiving the data
SOCKET-TIMEOUTThe data requested was not received in the timeout period
SOCKET-SSL-ERRORThere was an SSL error while reading data from the socket
READ-HTTP-CHUNK-ERRORnegative value given for chunk size by server
Qore::Socket::readHTTPChunkedBodyBinaryWithCallback ( code  rcb,
timeout  timeout_ms = -1 
)

Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a string in the "body" key of a hash (including footers received)

If any errors are encountered, an exception is raised

Example:
1 hash ans = sock.readHTTPChunkedBodyBinaryWithCallback(recv_callback, 20s);
Events:
EVENT_HTTP_CHUNK_SIZE, EVENT_HTTP_FOOTERS_RECEIVED
Parameters
rcbThe receive callback for the data received; first this method is called with a hash of the message headers, and then with any message body; if a chunked HTTP message is received, then the callback is called once for each chunk; when the message has been received, then the receive callback is called with a hash representing any trailer data received in a chunked transfer or NOTHING if the data was received in a normal message body or if there was no trailer data in a chunked transfer. The argument to this callback is always a hash; data calls have the following keys:
  • "data": the string or binary data
  • "chunked": True if the data was received with chunked transfer encoding, False if not
Header or trailer data is placed in a hash with the following keys:
  • "hdr": this can be assigned to NOTHING for the trailer hash if the data was not sent chunked or no trailers were included in a chunked message
  • "obj": this is the owning object (so socket parameters can be changed based on headers received, such as, for example, socket character encoding)
timeout_msThe timeout in milliseconds (1/1000 second). If no timeout or if a negative 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. If a timeout occurs, a "SOCKET-TIMEOUT" exception is raised. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end has closed the connection
SOCKET-RECV-ERRORThere was an error receiving the data
SOCKET-TIMEOUTThe data requested was not received in the timeout period
SOCKET-SSL-ERRORThere was an SSL error while reading data from the socket
READ-HTTP-CHUNK-ERRORnegative value given for chunk size by server
Since
Qore 0.8.10
Qore::Socket::readHTTPChunkedBodyWithCallback ( code  rcb,
timeout  timeout_ms = -1 
)

Reads in an HTTP message body sent in chunked transfer encoding and returns it with any footers received as a string in the "body" key of a hash (including footers received)

If any errors are encountered, an exception is raised

Example:
1 hash ans = sock.readHTTPChunkedBodyWithCallback(recv_callback, 20s);
Events:
EVENT_HTTP_CHUNK_SIZE, EVENT_HTTP_FOOTERS_RECEIVED
Parameters
rcbThe receive callback for the data received; first this method is called with a hash of the message headers, and then with any message body; if a chunked HTTP message is received, then the callback is called once for each chunk; when the message has been received, then the receive callback is called with a hash representing any trailer data received in a chunked transfer or NOTHING if the data was received in a normal message body or if there was no trailer data in a chunked transfer. The argument to this callback is always a hash; data calls have the following keys:
  • "data": the string or binary data
  • "chunked": True if the data was received with chunked transfer encoding, False if not
Header or trailer data is placed in a hash with the following keys:
  • "hdr": this can be assigned to NOTHING for the trailer hash if the data was not sent chunked or no trailers were included in a chunked message
  • "obj": this is the owning object (so socket parameters can be changed based on headers received, such as, for example, socket character encoding)
timeout_msThe timeout in milliseconds (1/1000 second). If no timeout or if a negative 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. If a timeout occurs, a "SOCKET-TIMEOUT" exception is raised. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end has closed the connection
SOCKET-RECV-ERRORThere was an error receiving the data
SOCKET-TIMEOUTThe data requested was not received in the timeout period
SOCKET-SSL-ERRORThere was an SSL error while reading data from the socket
READ-HTTP-CHUNK-ERRORnegative value given for chunk size by server
Since
Qore 0.8.10
hash Qore::Socket::readHTTPHeader ( timeout  timeout_ms = -1,
*reference  info 
)

Retuns a hash representing the data in the HTTP header read, or, if the data cannot be parsed as an HTTP header, then an exception is thrown, and the data read is returned as a string in the arg key of the exception hash.

If any errors occur reading from the socket or if invalid HTTP data is received, an exception is raised. Accepts an optional timeout value in milliseconds.

Example:
1 hash ans = sock.readHTTPHeader(10s);
Events:
EVENT_PACKET_READ, EVENT_HTTP_MESSAGE_RECEIVED
Parameters
timeout_msThe timeout in milliseconds (1/1000 second). If no timeout or if a negative 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. If a timeout occurs, a "SOCKET-TIMEOUT" exception is raised. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
infoAn optional reference to an lvalue that can accept a hash that will be used as an output variable with one of the following two keys:
  • request-uri: (only set when parsing a request header) gives the request URI in an HTTP request
  • response-uri: (only set when parsing a response header) gives the response URI in an HTTP response
  • body-content-type: this is the "Content-Type" header without any charset declaration
  • charset: if there is a charset declaration in the "Content-Type" header, the value is returned in this key
  • close: (only set when parsing a request header) set to True if the connection should be closed after responding, False if not; see notes below about how this value is calculated
  • accept-charset: this key will be set to an appropriate value from any "Accept-Charset" header; if any of "*", "utf8", or "utf-8" are present, then this will be set to "utf8", otherwise it will be set to the first requested character encoding in the list
  • accept-encoding: this key will be set to a list of values from any "Accept-Encoding" header
Returns
a hash of headers (where each header key is converted to lower-case) also including the following keys giving additional information about the HTTP header received:
  • http_version: a string giving the HTTP version set in the header
  • status_code: (HTTP responses only) an integer giving the status code
  • status_message: (HTTP responses only) if present in an HTTP response, this key will be set to the message after the status code
  • method: (HTTP requests only) a string giving the HTTP method (i.e. "GET", "POST", etc)
  • path: (HTTP requests only) a string giving the path in a request without any decoding; use decode_url() to decode if necessary
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end closed the connection without sending any data
SOCKET-RECV-ERRORThere was an error receiving the data
SOCKET-TIMEOUTThe data requested was not received in the timeout period
SOCKET-SSL-ERRORThere was an SSL error while reading data from the socket
SOCKET-HTTP-ERRORInvalid HTTP data was received, in the case of invalid header info received, the arg key of the exception hash will have the invalid data received
Note
  • if the header claims a certain character encoding via a charset declaration in the "Content-Type" header, the Socket's character encoding is automatically set accordingly
  • RFC 2616 3.7.1: if no encoding is specified, then set "iso-8859-1"
  • RFC 2068 19.7.1: Persistent connections in HTTP/1.0 must be explicitly negotiated as they are not the default behavior
  • RFC 1945 1.3: Except for experimental applications, current practice requires that the connection be established by the client prior to each request and closed by the server after sending the response.
  • the conclusion is that server applications MUST close the connection when a request is received by an HTTP 1.0 client without an explicit request to keep the connection open
Since
  • Qore 0.8.4 this method always returns a hash and raises a SOCKET-HTTP-ERROR if invalid HTTP data is received
  • Qore 0.8.8 added the close, charset, body-content-type, and accept-charset info keys as well as the encoding handling based on the detected charset
string Qore::Socket::readHTTPHeaderString ( timeout  timeout_ms = -1)

Retuns a string representing the data in the HTTP header read (reads until "\r\n\r\n")

If any errors occur reading from the socket, an exception is raised. Accepts an optional timeout value in milliseconds.

Example:
1 string str = sock.readHTTPHeaderString(10s);
Events:
EVENT_PACKET_READ
Parameters
timeout_msThe timeout in milliseconds (1/1000 second). If no timeout or if a negative 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. If a timeout occurs, a "SOCKET-TIMEOUT" exception is raised. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
a string representing the header data read
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-CLOSEDThe remote end closed the connection without sending any data
SOCKET-RECV-ERRORThere was an error receiving the data
SOCKET-TIMEOUTThe data requested was not received in the timeout period
SOCKET-SSL-ERRORThere was an SSL error while reading data from the socket
SOCKET-HTTP-ERRORmaximum header size was exceeded
Since
Qore 0.8.8
string Qore::Socket::recv ( softint  size = 0,
timeout  timeout_ms = -1 
)

Receives data from the socket and returns a string tagged with the Socket's character encoding.

If any errors occur reading from the socket, an exception is raised

Example:
1 string data = sock.recv(-1); # read all data available
Events:
EVENT_PACKET_READ
Parameters
sizethe amount of data to read in bytes; to read until the remote closes the connection, use -1; a value of 0 means to read all data currently available or available up until the timeout period (if non-negative)
timeout_msthe 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
the data read, returned as a string tagged with the Socket's character encoding
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection.
SOCKET-RECV-ERRORthere was an error receiving the data.
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
binary Qore::Socket::recvBinary ( softint  size = 0,
timeout  timeout_ms = -1 
)

Receives data from the socket and returns a binary object.

If any errors occur reading from the socket, an exception is raised

Example:
1 binary data = sock.recvBinary(-1); # read all data available
Events:
EVENT_PACKET_READ
Parameters
sizethe amount of data to read in bytes; to read until the remote closes the connection, use -1; a value of 0 means to read all data currently available or available up until the timeout period (if non-negative)
timeout_msthe 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
the data read, returned as a binary object
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection.
SOCKET-RECV-ERRORthere was an error receiving the data.
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recv()
int Qore::Socket::recvi1 ( timeout  timeout_ms = -1)

Receives a 1-byte signed integer from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvi1();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 1-byte signed integer read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvu1()
int Qore::Socket::recvi2 ( timeout  timeout_ms = -1)

Receives a 2-byte (16-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

Example:
1 int val = sock.recvi2();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 2-byte signed integer in big-endian format (network byte order) read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvu2(), Socket::recvi2LSB(), Socket::recvu2LSB()
int Qore::Socket::recvi2LSB ( timeout  timeout_ms = -1)

Receives a 2-byte (16-bit) signed integer in little-endian format from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvi2LSB();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 2-byte signed integer in little-endian format read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi2(), Socket::recvu2(), Socket::recvu2LSB()
int Qore::Socket::recvi4 ( timeout  timeout_ms = -1)

Receives a 4-byte (32-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

Example:
1 int val = sock.recvi4();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 4-byte signed integer in big-endian format (network byte order) read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvu4(), Socket::recvi4LSB(), Socket::recvu4LSB()
int Qore::Socket::recvi4LSB ( timeout  timeout_ms = -1)

Receives a 4-byte (32-bit) signed integer in little-endian format from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvi4LSB();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 4-byte signed integer in little-endian format read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi4(), Socket::recvu4(), Socket::recvu4LSB()
int Qore::Socket::recvi8 ( timeout  timeout_ms = -1)

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

Example:
1 int val = sock.recvi8();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 8-byte signed integer in big-endian format (network byte order) read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi8LSB()
int Qore::Socket::recvi8LSB ( timeout  timeout_ms = -1)

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

Example:
1 int val = sock.recvi8LSB();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 8-byte signed integer in little-endian format read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi8()
int Qore::Socket::recvu1 ( timeout  timeout_ms = -1)

Receives a 1-byte unsigned integer from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvu1();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 1-byte unsigned integer read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi1()
int Qore::Socket::recvu2 ( timeout  timeout_ms = -1)

Receives a 2-byte (16-bit) unsigned integer in big-endian format (network byte order) from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvu2();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 2-byte unsigned integer in big-endian format (network byte order) read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi2(), Socket::recvi2LSB(), Socket::recvu2LSB()
int Qore::Socket::recvu2LSB ( timeout  timeout_ms = -1)

Receives a 2-byte (16-bit) unsigned integer in little-endian format from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvi2LSB();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 2-byte unsigned integer in little-endian format read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi2(), Socket::recvu2(), Socket::recvi2LSB()
int Qore::Socket::recvu4 ( timeout  timeout_ms = -1)

Receives a 4-byte (32-bit) unsigned integer in big-endian format (network byte order) from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvu4();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 4-byte unsigned integer in big-endian format (network byte order) read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi4(), Socket::recvi4LSB(), Socket::recvu4LSB()
int Qore::Socket::recvu4LSB ( timeout  timeout_ms = -1)

Receives a 4-byte (32-bit) unsigned integer in little-endian format from the socket.

If any errors occur reading from the socket, an exception is raised

Example:
1 int val = sock.recvu4LSB();
Events:
EVENT_PACKET_READ
Parameters
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, 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. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
The 4-byte unsigned integer in little-endian format read; if any errors occur reading from the socket, an exception is raised
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-CLOSEDthe remote end has closed the connection
SOCKET-RECV-ERRORthere was an error receiving the data
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SSL-ERRORthere was an SSL error while reading data from the socket
See Also
Socket::recvi4(), Socket::recvu4(), Socket::recvi4LSB()
int Qore::Socket::send ( binary  bin,
timeout  timeout_ms = -1 
)

Sends binary data over the socket; if any errors occur, an exception is thrown.

Example:
1 sock.send(data, 20s);
Events:
EVENT_PACKET_SENT
Parameters
binSends the binary data over the socket
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Note
this method is as of Qore 0.8.6+ equivalent to Socket::send2() except for the return value
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error like Socket::send2(); this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::send ( string  str,
timeout  timeout_ms = -1 
)

Sends string data over the socket; string data is converted to the socket's encoding if necessary; if any errors occur, an exception is thrown.

String data will be converted to the encoding set for the socket if necessary.

Example:
1 sock.send(str, 20s);
Events:
EVENT_PACKET_SENT
Parameters
strsends the string data over the socket without the trailing null ('\0') character; the string's encoding is converted to the socket's encoding if necessary
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Note
this method is as of Qore 0.8.6+ equivalent to Socket::send2() except for the return value
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error like Socket::send2(); this change was made because it was not possible to catch SSL errors with this method with the previous implementation
nothing Qore::Socket::send2 ( binary  bin,
timeout  timeout_ms = -1 
)

Sends binary data over the socket; if any errors occur, an exception is thrown.

Example:
1 sock.send2(data);
Events:
EVENT_PACKET_SENT
Parameters
binSends the binary data over the socket
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Note
this method is as of Qore 0.8.6+ equivalent to Socket::send() except for the lack of a return value (since errors cause exceptions to be thrown, no return value is necessary)
Since
Qore 0.8.4
nothing Qore::Socket::send2 ( string  str,
timeout  timeout_ms = -1 
)

Sends string data over the socket; string data is converted to the socket's encoding if necessary; if any errors occur, an exception is thrown.

String data will be converted to the encoding set for the socket if necessary.

Example:
1 sock.send2(str);
Events:
EVENT_PACKET_SENT
Parameters
strsends the string data over the socket without the trailing null ('\0') character; the string's encoding is converted to the socket's encoding if necessary
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Note
this method is as of Qore 0.8.6+ equivalent to Socket::send() except for the lack of a return value (since errors cause exceptions to be thrown, no return value is necessary)
Since
Qore 0.8.4
int Qore::Socket::sendBinary ( string  str,
timeout  timeout_ms = -1 
)

Sends string data over the socket without converting the string to the socket's encoding, but instead is sent exactly as-is; if any errors occur, an exception is thrown.

Example:
1 sock.sendBinary(str);
Events:
EVENT_PACKET_SENT
Parameters
strstring data to be sent (without any conversion to the socket's encoding) over the socket without the trailing null ('\0') character
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Socket::send2()
Note
this method is as of Qore 0.8.6+ equivalent to Socket::sendBinary2() except for the return value
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error like Socket::send2(); this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::sendBinary ( binary  bin,
timeout  timeout_ms = -1 
)

Sends binary data over the socket; if any errors occur, an exception is thrown.

Example:
1 sock.sendBinary2(data);
Events:
EVENT_PACKET_SENT
Parameters
binSends the binary data over the socket
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Socket::send2()
Note
this method is as of Qore 0.8.6+ equivalent to Socket::sendBinary2() except for the return value
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error like Socket::send2(); this change was made because it was not possible to catch SSL errors with this method with the previous implementation
nothing Qore::Socket::sendBinary2 ( string  str,
timeout  timeout_ms = -1 
)

Sends string data over the socket without converting the string to the socket's encoding, but instead is sent exactly as-is; if any errors occur, an exception is thrown.

Example:
1 sock.sendBinary2(str);
Events:
EVENT_PACKET_SENT
Parameters
strstring data to be sent (without any conversion to the socket's encoding) over the socket without the trailing null ('\0') character
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Socket::send2()
Note
this method is as of Qore 0.8.6+ equivalent to Socket::sendBinary() except for the lack of a return value (since errors cause exceptions to be thrown, no return value is necessary)
Since
Qore 0.8.4
nothing Qore::Socket::sendBinary2 ( binary  bin,
timeout  timeout_ms = -1 
)

Sends binary data over the socket; if any errors occur, an exception is thrown.

Example:
1 sock.sendBinary2(data);
Events:
EVENT_PACKET_SENT
Parameters
binSends the binary data over the socket
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
See Also
Socket::send2()
Note
this method is as of Qore 0.8.6+ equivalent to Socket::sendBinary() except for the lack of a return value (since errors cause exceptions to be thrown, no return value is necessary)
Since
Qore 0.8.4
nothing Qore::Socket::sendHTTPMessage ( string  method,
string  path,
string  http_version,
hash  headers,
*string  body,
*reference  info,
timeout  timeout_ms = -1 
)

Sends an HTTP message with a method and user-defined headers given as a hash and an optional message body.

Creates a properly-formatted HTTP message and sends it over the Socket. To send a string without converting the encoding to the Socket's encoding, convert the string to a binary object using the binary() function and use the variant of this method that takes a binary object for the data argument.

Example:
1 sock.sendHTTPMessage("POST", "/RPC2", "1.1", ("Content-Type" : "text/xml"), xml);
Events:
EVENT_PACKET_SENT, EVENT_HTTP_SEND_MESSAGE
Parameters
methodthe HTTP method name to send (i.e. POST, HEAD, etc)
paththe path component of the URL
http_versionthe HTTP protocol version ("1.0" or "1.1")
headersa hash of additional headers to send (key-value pairs)
bodyif 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); string data will be automatically converted to the Socket's encoding if necessary
infoAn optional reference to an lvalue that can accept a hash that will be used as an output variable with the following hash key:
  • request-uri: The request URI as sent in the output message
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, then the call will not time out and will not return until all the data has been sent or the remote end closes the connection. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-TIMEOUTthe data requested was not received in the timeout period
ENCODING-CONVERSION-ERRORthe given string could not be converted to the socket's character encoding
SOCKET-SEND-ERRORSend failed
SOCKET-SSL-ERRORthere was an error sending SSL data
See Also
Socket::sendHTTPResponse(), Socket::readHTTPHeader()
nothing Qore::Socket::sendHTTPMessage ( string  method,
string  path,
string  http_version,
hash  headers,
binary  body,
*reference  info,
timeout  timeout_ms = -1 
)

Sends an HTTP message with a method and user-defined headers given as a hash and an optional message body.

Creates a properly-formatted HTTP message and sends it over the Socket

Example:
1 sock.sendHTTPMessage("POST", "/RPC2", "1.1", ("Content-Type" : "text/xml"), xml);
Events:
EVENT_PACKET_SENT, EVENT_HTTP_SEND_MESSAGE
Parameters
methodthe HTTP method name to send (i.e. POST, HEAD, etc)
paththe path component of the URL
http_versionthe HTTP protocol version ("1.0" or "1.1")
headersa hash of additional headers to send (key-value pairs)
bodyif it 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)
infoAn optional reference to an lvalue that can accept a hash that will be used as an output variable with the following hash key:
  • request-uri: The request URI as sent in the output message
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, then the call will not time out and will not return until all the data has been sent or the remote end closes the connection. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SEND-ERRORSend failed
SOCKET-SSL-ERRORthere was an error sending SSL data
See Also
Socket::sendHTTPResponse(), Socket::readHTTPHeader()
nothing Qore::Socket::sendHTTPMessageWithCallback ( code  scb,
string  method,
string  path,
string  http_version,
hash  headers,
*reference  info,
timeout  timeout_ms = -1 
)

Sends an HTTP message with a method and user-defined headers given as a hash and an optional message body.

Creates a properly-formatted HTTP message and sends it over the Socket

Example:
1 sock.sendHTTPMessageWithCallback(send_callback, "POST", "/RPC2", "1.1", ("Content-Type" : "text/xml"));
Events:
EVENT_PACKET_SENT, EVENT_HTTP_SEND_MESSAGE
Parameters
scbThe callback giving the chunked HTTP data to send; this callback must return either a string or a binary value each time it is called to give the chunked data to send; when all data has been sent, then a hash of message trailers can be sent or simply NOTHING which will close the chunked message
methodthe HTTP method name to send (i.e. POST, HEAD, etc)
paththe path component of the URL
http_versionthe HTTP protocol version ("1.0" or "1.1")
headersa hash of additional headers to send (key-value pairs)
infoAn optional reference to an lvalue that can accept a hash that will be used as an output variable with the following hash key:
  • request-uri: The request URI as sent in the output message
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, then the call will not time out and will not return until all the data has been sent or the remote end closes the connection. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-NOT-OPENthe socket is not connected
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SEND-ERRORSend failed
SOCKET-SSL-ERRORthere was an error sending SSL data
See Also
Socket::sendHTTPResponse(), Socket::readHTTPHeader()
Since
Qore 0.8.10
nothing Qore::Socket::sendHTTPResponse ( softint  status_code,
string  status_desc,
string  http_version,
hash  headers,
*string  body,
timeout  timeout_ms = -1 
)

Sends an HTTP response with user-defined headers given as a hash and an optional message body.

Creates a properly-formatted HTTP response message and sends it over the Socket (must already be connected). If any errors occur, an exception is raised. To send a string without converting the encoding to the Socket's encoding, convert the string to a binary object using the binary() function and use the variant of this method that takes a binary object for the data argument.

Example:
1 sock.sendHTTPResponse(200, "OK", "1.1", ("Connection":"Keep-Alive"));
Events:
EVENT_PACKET_SENT, EVENT_HTTP_SEND_MESSAGE
Parameters
status_codethe HTTP status code to send (i.e. 200, 404, etc)
status_descthe descriptive text for the status code.
http_versionthe HTTP protocol version (normally "1.0" or "1.1", however this method allows any string to be sent)
headersa hash of additional headers to send (key-value pairs)
bodyif 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); string data will be automatically converted to the Socket's encoding if necessary
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, then the call will not time out and will not return until all the data has been sent or the remote end closes the connection. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-SENDHTTPRESPONSE-STATUS-ERRORraised if the status_code (first argument) is < 100 or > 599
ENCODING-CONVERSION-ERRORthe given string could not be converted to the socket's character encoding
SOCKET-NOT-OPENthe socket is not connected
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SEND-ERRORsend failed
nothing Qore::Socket::sendHTTPResponse ( softint  status_code,
string  status_desc,
string  http_version,
hash  headers,
binary  body,
timeout  timeout_ms = -1 
)

Sends an HTTP response with user-defined headers given as a hash and a message body as literal binary data.

Creates a properly-formatted HTTP response message and sends it over the Socket (must already be connected). If any errors occur, an exception is raised.

Example:
1 sock.sendHTTPResponse(200, "OK", "1.1", ("Connection":"Keep-Alive"), bin);
Events:
EVENT_PACKET_SENT, EVENT_HTTP_SEND_MESSAGE
Parameters
status_codethe HTTP status code to send (i.e. 200, 404, etc)
status_descthe descriptive text for the status code.
http_versionthe HTTP protocol version (normally "1.0" or "1.1", however this method allows any string to be sent)
headersa hash of additional headers to send (key-value pairs)
bodyif the binary object given 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)
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, then the call will not time out and will not return until all the data has been sent or the remote end closes the connection. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-SENDHTTPRESPONSE-STATUS-ERRORraised if the status_code (first argument) is < 100 or > 599
SOCKET-NOT-OPENthe socket is not connected
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SEND-ERRORsend failed
nothing Qore::Socket::sendHTTPResponseWithCallback ( code  scb,
softint  status_code,
string  status_desc,
string  http_version,
hash  headers,
timeout  timeout_ms = -1 
)

Sends an HTTP response with user-defined headers given as a hash and a message body as literal binary data.

Creates a properly-formatted HTTP response message and sends it over the Socket (must already be connected). If any errors occur, an exception is raised.

Example:
1 sock.sendHTTPResponseWithCallback(send_callback, 200, "OK", "1.1", ("Connection":"Keep-Alive"));
Events:
EVENT_PACKET_SENT, EVENT_HTTP_SEND_MESSAGE
Parameters
scbThe callback giving the chunked HTTP data to send; this callback must return either a string or a binary value each time it is called to give the chunked data to send; when all data has been sent, then a hash of message trailers can be sent or simply NOTHING which will close the chunked message
status_codethe HTTP status code to send (i.e. 200, 404, etc)
status_descthe descriptive text for the status code.
http_versionthe HTTP protocol version (normally "1.0" or "1.1", however this method allows any string to be sent)
headersa hash of additional headers to send (key-value pairs)
timeout_msthe timeout in milliseconds (1/1000 second); If no timeout is passed or is negative, then the call will not time out and will not return until all the data has been sent or the remote end closes the connection. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SOCKET-SENDHTTPRESPONSE-STATUS-ERRORraised if the status_code (first argument) is < 100 or > 599
SOCKET-NOT-OPENthe socket is not connected
SOCKET-TIMEOUTthe data requested was not received in the timeout period
SOCKET-SEND-ERRORsend failed
Since
Qore 0.8.10
int Qore::Socket::sendi1 ( softint  i = 0,
timeout  timeout_ms = -1 
)

Sends a 1-byte integer over the socket.

If any errors occur, an exception is thrown

Example:
1 sock.sendi1(val, 20s);
Events:
EVENT_PACKET_SENT
Parameters
ithe integer to send; only the least-significant byte will be sent
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error; this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::sendi2 ( softint  i = 0,
timeout  timeout_ms = -1 
)

Sends a 2-byte (16-bit) integer in big-endian format (network byte order) over the socket.

If any errors occur, an exception is thrown

Example:
1 sock.sendi2(val, 20s);
Events:
EVENT_PACKET_SENT
Parameters
ithe integer to send; only the least-significant 2 bytes will be sent
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error; this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::sendi2LSB ( softint  i = 0,
timeout  timeout_ms = -1 
)

Sends a 2-byte (16-bit) integer in little-endian format over the socket.

If any errors occur, an exception is thrown

Example:
1 sock.sendi2LSB(val, 20s);
Events:
EVENT_PACKET_SENT
Parameters
ithe integer to send; only the least-significant 2 bytes will be sent
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error; this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::sendi4 ( softint  i = 0,
timeout  timeout_ms = -1 
)

Sends a 4-byte (32-bit) integer in big-endian format (network byte order) over the socket.

If any errors occur, an exception is thrown

Example:
1 sock.sendi4(val, 20s);
Events:
EVENT_PACKET_SENT
Parameters
ithe integer to send; only the least-significant 4 bytes will be sent
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error; this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::sendi4LSB ( softint  i = 0,
timeout  timeout_ms = -1 
)

Sends a 4-byte (32-bit) integer in little-endian format over the socket.

If any errors occur, an exception is thrown

Example:
1 sock.sendi4LSB(val, 20s);
Events:
EVENT_PACKET_SENT
Parameters
ithe integer to send; only the least-significant 4 bytes will be sent
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error; this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::sendi8 ( softint  i = 0,
timeout  timeout_ms = -1 
)

Sends an 8-byte (64-bit) integer in big-endian format (network byte order) over the socket.

If any errors occur, an exception is thrown

Example:
1 sock.sendi8(val, 20s);
Events:
EVENT_PACKET_SENT
Parameters
ithe integer to send
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error; this change was made because it was not possible to catch SSL errors with this method with the previous implementation
int Qore::Socket::sendi8LSB ( softint  i = 0,
timeout  timeout_ms = -1 
)

Sends an 8-byte (64-bit) integer in little-endian format over the socket.

If any errors occur, an exception is thrown

Example:
1 sock.sendi8LSB(val, 20s);
Events:
EVENT_PACKET_SENT
Parameters
ithe integer to send
timeout_msthe 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 sent or the remote end closes the connection; the timeout value is the longest value that a single send() operation can take with non-blocking I/O. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
always returns 0 for backwards compatibility; if an error occurs, an exception is thrown
Exceptions
SOCKET-NOT-OPENThe socket is not connected
SOCKET-TIMEOUTa single send() operation exceeded the given timeout period
SOCKET-SEND-ERRORan error occurred sending the socket data
SOCKET-SSL-ERRORthere was an SSL error while writing data to the socket
Since
Qore 0.8.6 this method takes a timeout value and throws exceptions on error; this change was made because it was not possible to catch SSL errors with this method with the previous implementation
nothing Qore::Socket::setCertificate ( SSLCertificate  cert)

Sets the X.509 certificate to use for negotiating encrypted connections.

Example:
1 sock.setCertificate(cert);
Parameters
certThis must be an SSLCertificate object
nothing Qore::Socket::setCertificate ( string  cert_pem)

Sets the X.509 certificate to use for negotiating encrypted connections from the PEM-encoded string representing the X.509 certificate.

Example:
1 sock.setCertificate(cert_string);
Parameters
cert_pemthe PEM-encoded string representing the X.509 certificate
nothing Qore::Socket::setCertificate ( binary  cert_der)

Sets the X.509 certificate to use for negotiating encrypted connections from the DER-encoded binary object representing the X.509 certificate.

Example:
1 sock.setCertificate(cert_der);
Parameters
cert_derthe DER-encoded binary object representing the X.509 certificate
nothing Qore::Socket::setCharset ( string  encoding)

Sets the character encoding for the socket.

A method synonym for Socket::setEncoding(), kept for backwards-compatibility

Parameters
encodingThe character encoding for the socket
nothing Qore::Socket::setEncoding ( string  encoding)

Sets the character encoding for the socket.

Parameters
encodingthe character encoding for the socket
nothing Qore::Socket::setEventQueue ( )

Removes any Queue object from the Socket object so that socket events are no longer added to the Queue.

Example:
1 sock.setEventQueue();
See Also
I/O Event Handling for more information
nothing Qore::Socket::setEventQueue ( Queue  queue)

Sets a Queue object to receive socket events.

Example:
1 sock.setEventQueue(queue);
Parameters
queuethe Queue object to receive socket events; note that the Queue passed cannot have any maximum size set or a QUEUE-ERROR will be thrown
Exceptions
QUEUE-ERRORthe Queue passed has a maximum size set
See Also
I/O Event Handling for more information
int Qore::Socket::setNoDelay ( bool  nd = True)

Sets the boolean 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.

Example:
1 if (sock.setNoDelay(True))
2  printf("error setting TCP_NODELAY: %s\n", strerror(errno()));
Parameters
ndthe boolean TCP_NODELAY setting for the socket
Returns
0 for success, non-zero for errors; to get error information, see errno() and strerror()
See Also
Socket::getNoDelay()
nothing Qore::Socket::setPrivateKey ( SSLPrivateKey  key)

Sets the private key to use for negotiating encrypted connections along with the X.509 certificate.

Example:
1 sock.setPrivateKey(key);
Parameters
keythe private key for the X.509 certificate to use when establishing TLS/SSL encrypted connections
nothing Qore::Socket::setPrivateKey ( string  key_pem,
*string  pass 
)

Sets the private key to use for negotiating encrypted connections along with the X.509 certificate from a PEM-encoded string representing the private key and an optional password.

Example:
1 sock.setPrivateKey(key_pem, password);
Parameters
key_pemthe PEM-encoded string representing the private key for the X.509 certificate to use when establishing TLS/SSL encrypted connections
passthe password for the private key, if needed
nothing Qore::Socket::setPrivateKey ( binary  key_der)

Sets the private key to use for negotiating encrypted connections along with the X.509 certificate from a DER-encoded binary object representing the private key.

Example:
1 sock.setPrivateKey(key_der);
Parameters
key_derthe DER-encoded binary object representing the private key for the X.509 certificate to use when establishing TLS/SSL encrypted connections
int Qore::Socket::setRecvTimeout ( timeout  timeout_ms)

sets the receive timeout as a socket option

Returns 0 for success, -1 for error (check errno() for the actual error in this case)

Example:
1 if (sock.setRecvTimeout(20s))
2  printf("error setting timeout on socket: %s\n", strerror(errno()));
Parameters
timeout_msthe receive timeout to set with a resolution of milliseconds; Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Returns
0 for success, -1 for error (check errno() for the actual error in this case)
int Qore::Socket::setSendTimeout ( timeout  timeout_ms)

sets the send timeout as a socket option

Returns 0 for success, -1 for error (check errno() for the actual error in this case)

Example:
1 if (sock.setSendTimeout(20s))
2  printf("error setting timeout on socket: %s\n", strerror(errno()));
Parameters
timeout_msthe send timeout to set with a resolution of milliseconds
Returns
0 for success, -1 for error (check errno() for the actual error in this case)
nothing Qore::Socket::setWarningQueue ( int  warning_ms,
int  warning_bs,
Queue  queue,
any  arg,
timeout  min_ms = 1s 
)

Sets a Queue object to receive socket warnings.

Example:
1 sock.setWarningQueue(5000, 5000, queue, "socket-1");
Parameters
warning_msthe threshold in milliseconds for individual socket actions (send, receive, connect), if exceeded, a socket warning is placed on the warning queue with the following keys:
  • "type": a string with the constant value "SOCKET-OPERATION-WARNING"
  • "operation": a string giving the operation that caused the warning (ex: "connect")
  • "us": an integer giving the number of microseconds for the operation
  • "timeout": an integer giving the warning threshold in microseconds
  • "arg": if any "arg" argument is passed to the Socket::setWarningQueue() method, it will be included in the warning hash here
warning_bsvalue in bytes per second; if any call has performance below this threshold, a socket warning is placed on the warning queue with the following keys:
  • "type": a string with the constant value "SOCKET-THROUGHPUT-WARNING"
  • "dir": either "send" or "recv" depending on the direction of the data flow
  • "bytes": the amount of bytes sent
  • "us": an integer giving the number of microseconds for the operation
  • "bytes_sec": a float giving the transfer speed in bytes per second
  • "threshold": an integer giving the warning threshold in bytes per second
  • "arg": if any "arg" argument is passed to the Socket::setWarningQueue() method, it will be included in the warning hash here
queuethe Queue object to receive warning events
argan optional argument to be placed in the "arg" key in each warning hash (could be used to identify the socket for example)
min_msthe minimum transfer time with a resolution of milliseconds for a transfer to be eligible for triggering a warning; transfers that take less than this period of time are not eligible for raising a warning
Exceptions
QUEUE-ERRORthe Queue passed has a maximum size set
SOCKET-SETWARNINGQUEUE-ERRORat least one of warning_ms and warning_bs must be > 0
See Also
Socket::clearWarningQueue()
Since
  • Qore 0.8.9
  • Qore 0.8.12 the minimum threshold for a warning is a transfer of at least 1024 bytes, smaller transfers will not result in a warning
int Qore::Socket::shutdown ( )

Ensures that a socket will be closed even if the file descriptor is shared with other processes (for example, after a call to fork())

Does not throw any exceptions; returns an error code in case of an error

Example:
1 if (sock.shutdown())
2  stderr.printf("Error calling Socket::shutdown(): %s\n", strerror(errno()));
Returns
0 for success, -1 for error; in this case check errno() for the error number
nothing Qore::Socket::shutdownSSL ( )

Shuts down the SSL connection on a secure connection.

If any errors occur, an exception is raised

Example:
1 sock.shutdownSSL();
Exceptions
SOCKET-SSL-ERRORan error occurred shutting down the TLS/SSL connection
nothing Qore::Socket::upgradeClientToSSL ( timeout  timeout_ms = -1)

Upgrades a client socket connection to a TLS/SSL connection.

Example:
1 sock.upgradeClientToSSL();
Parameters
timeout_msIf a positive timeout value is passed and the SSL upgrade takes longer to establish than the timeout, an "SSL-TIMEOUT-ERROR" exception is thrown. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SSL-TIMEOUT-ERRORtimeout error negotiating SSL connection
Since
Qore 0.8.12 the timeout_ms parameter was added
nothing Qore::Socket::upgradeServerToSSL ( timeout  timeout_ms = -1)

Upgrades a server socket connection to a TLS/SSL connection.

Example:
1 sock.upgradeServerToSSL();
Parameters
timeout_msIf a positive timeout value is passed and the SSL upgrade takes longer to establish than the timeout, an "SSL-TIMEOUT-ERROR" exception is thrown. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.)
Exceptions
SSL-TIMEOUT-ERRORtimeout error negotiating SSL connection
Since
Qore 0.8.12 the timeout_ms parameter was added
*string Qore::Socket::verifyPeerCertificate ( )

Returns a string code giving the result of verifying the remote certificate or NOTHING if an encrypted connection is not currently established.

Code Flags:
CONSTANT
Example:
1 *string str = sock.verifyPeerCertificate();
Returns
A string code giving the result of verifying the peer's certificate. No value is returned if a secure connection has not been established. The set of possible return values is made up of the keys of the X509_VerificationReasons hash. This hash can also be used to generate a textual description of the verification result.