Socket Component

Socket component is implementing a client socket. The socket can be of type TCP or type UDP.

Creating a Socket:

From scripting you can create a variable of the type Socket by the following code:
var s = new TSocket("tcp");
where the case insensitive string parameter from the constructor is the socket type, which can be either TCP or UDP.

Example:

var s = new TSocket("tcp"); s.host = "localhost"; s.port = 1; s.Timeout = 0; if(s.Connect()){ var data = ''; while((data=s.ReceiveLine()) != 'bye\n' && s.IsConnected){ s.Send(Plain2B64(data)); } s.Close(); } else { Trace(s.LastError); }

The above code will construct a client socket of type TCP which will be connected to localhost port 1. If the connection was made it will wait until the server sends some text delimited by a line end and then sends back the base 64 encoded version of the message to the server. It will continue to do this until either the connection is reseted by the server or server send a message with the content "bye".

Functions:

Properties:

function Send(Data)

Sends the buffer specified in the data parameter on the socket. If the type of the socket is TCP sending data will fail if the socket is not connected. If while sending, the current timeout expires the function will return without sending all the data on the socket.

Parameter list:

Return values:

The function will return an integer representing the number of bytes actually sent. If the function returns 0, the send failed and last error properties are set.

function Receive(WaitTimeout)

Read all the available data from the socket.

Parameter list:

Return values:

The function will return the data received as string. Return value can be empty string if timeout has expired, no data was received or an error occured. In the later case last error properties are set.

function ReceiveUntil(Delimiter)

Reads data from the socket until the given delimiter has been received or timeout has expired.

Parameter list:

Return values:

The function will return the data received as string without the delimiter. Return value can be empty string if timeout has expired or an error occured. In that cases last error properties are set.

function ReceiveLine()

Reads data from the socket until line terminator is received.

Return values:

The function will return the data received as string without the line end. Return value can be empty string if timeout has expired or an error occured. In that cases last error properties are set.

function ReceiveBytes(MaxBytes)

Reads data from the socket until a given number of bytes has been received.

Parameter list:

Return values:

The function will return the data received as string. Return value can be empty string if timeout has expired or an error occured. In that cases last error properties are set.

function Connect()

Connects the socket to the remote host. For connectionless UDP protocol, by convention Connect must be called to receive the response.

Return values:

The function will return true on success or false on failure in which case last error properties are set.

function Close()

Closes the connection on the socket.

property ErrorCode

Read only property of type integer containing the error code for the last operation. Code 0 means no error.

property LastError

Read only property of type string containing the error message for the last operation. Empty string means no error.

property Host

Read and write property of type string containing the remote host. This property must be set before calling Connect.

property Port

Read and write property of type integer containing the remote port. This property must be set before calling Connect.

property IsConnected

Read only property of type boolean. This property is true if the socket is connected.

property Timeout

Read and write property of type float containing the timeout value in seconds of the read or write operations.