A TCP non 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 SSL socket, use Lib.Sys.SSL.Socket library object.


Can be created using new method:

Lib.Sys.Net.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


Methods:

accept()

Accept a new connected client. This will return a connected socket on which you can read/write some data.


bind(hostport)

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(hostport)

Connect to the given server host/port. Throw an exception in case we couldn't successfully connect.

host - host object

port - port number

host()

Return the information about our side of a connected socket. Info structure:

{port:Int, host:Host}


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:

{port:Int, host:Host}


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

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

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(readwrite)

Shutdown the socket, either for reading or writing.

read - true/false

write - true/false

waitForRead()

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.Net.Socket.select(readwriteotherstimeout)

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

Created with the Personal Edition of HelpNDoc: Free EPub and documentation generator