Socket
A TCP SSL socket object : allow you to both connect to a given server and exchange messages or start your own server and wait for connections. If you need non SSL socket, use Lib.Sys.Net.Socket library object.
Can be created using new method:
Lib.Sys.SSL.Socket.new() |
create socket object |
|
Created socket object has properties:
custom |
A custom value that can be associated with the socket. Can be used to retrieve your custom infos after a select. |
any object, string, etc. |
input |
The stream on which you can read available data. By default the stream is blocking until the requested data is available, use setBlocking(false) or setTimeout to prevent infinite waiting. |
Input Object, read-only |
output |
The stream on which you can send data. Please note that in case the output buffer you will block while writing the data, use setBlocking(false) or setTimeout to prevent that. |
Output object. read-only |
verifyCert |
Define if peer certificate is verified during SSL handshake. |
nil or true/false |
Methods:
accept() |
Accept a new connected client. This will return a connected socket on which you can read/write some data. |
|
bind(host, port) |
Bind the socket to the given host/port so it can afterwards listen for connections there. |
host - host object port - port number |
close() |
Closes the socket : make sure to properly close all your sockets or you will crash when you run out of file descriptors. |
|
connect(host, port) |
Connect to the given server host/port. Throw an exception in case we couldn't successfully connect. |
host - host object port - port number |
handshake() |
Perform the SSL handshake. |
|
host() |
Return the information about our side of a connected socket. Info structure: |
|
listen(connections) |
Allow the socket to listen for incoming questions. The parameter tells how many pending connections we can have until they get refused. Use accept() to accept incoming connections. |
connections - number of connections |
peer() |
Return the information about the other side of a connected socket. Info structure: |
|
peerCertificate() |
Return the Certificate object received from the other side of a connection |
|
read() |
Read the whole data available on the socket. |
|
setBlocking(b) |
Change the blocking mode of the socket. A blocking socket is the default behavior. A non-blocking socket will abort blocking operations immediately by throwing a error. |
b - true/false |
setCA(cert) |
Configure the certificate chain for peer certificate verification. |
cert - Certificate object |
setCertificate(cert, key) |
Configure own certificate and private key. |
cert - Certificate object key - Key object |
setFastSend(b) |
Allows the socket to immediately send the data when written to its output : this will cause less ping but might increase the number of packets / data size, especially when doing a lot of small writes. |
b - true/false |
setHostname(name) |
Configure the hostname for Server Name Indication TLS extension. |
name - string |
addSNICertificate(cbServernameMatch, cert, key) |
Configure additional certificates and private keys for Server Name Indication extension. The callback may be called during handshake to determine the certificate to use. |
cbServernameMatch - callback function with string parameter, should return true/false cert - Certificate object key - Key object |
setTimeout(timeout) |
Gives a timeout after which blocking socket operations (such as reading and writing) will abort and throw an exception. |
timeout - time out value in seconds, float |
shutdown(read, write) |
Shutdown the socket, either for reading or writing. |
read - true/false write - true/false |
Block until some data is available for read on the socket. |
|
|
write(content) |
Write the whole data to the socket output. |
content - text content to write |
Static methods:
Lib.Sys.SSL.Socket.select(read, write, others, timeout) |
Wait until one of the sockets groups is ready for the given operation: - read contains sockets on which we want to wait for available data to be read, - write contains sockets on which we want to wait until we are allowed to write some data to their output buffers, - others contains sockets on which we want to wait for exceptional conditions. select will block until one of the condition is met, in which case it will return the sockets for which the condition was true. In case a timeout (in seconds) is specified, select might wait at worse until the timeout expires. |
read - array of sockets write - array of sockets others - array of sockets |
Examples:
socket = Lib.Sys.SSL.Socket.new()
print("Connecting to talk.google.com")
socket.connect( Lib.Sys.Net.Host.new( "talk.google.com" ), 5223 )
print("Connected")
print("Writing data")
socket.write( '<?xml version="1.0" encoding="UTF-8"?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="talk.google.com" xml:lang="en" version="1.0">' )
print( "Waiting for incoming XMPP stream ...." )
bufSize = 32
buf = Lib.Sys.IO.Bytes.alloc( 4096 )
pos = 0
len = -1
while true do
len = socket.input.readBytes( buf, pos, bufSize )
if( len < bufSize ) then break else pos = pos + len end
end
result = buf.toString()
print( 'Recieved xmpp stream: '..result )
--Should be something like: <stream:stream from="talk.google.com" id="E0F18D0BDA98612A" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client">
socket.write( '</stream>' )
socket.close()
Created with the Personal Edition of HelpNDoc: Easily create Help documents