TCPServer (1.1c)

Summary
TCPServer (1.1c)
_Class_TCPServerObject that simplifiy the creation of Encrypted, multi-clients TCP Server.
Properties
ipIP Address to listen
portPort number to listen
max_recv_lenmax # of characters to receive (TCPRecv)
TimeoutReceiving timeout (in ms).
Callback_NewClientFunction called when a new client connects to the server.
Callback_LostClientFunction called when a client disconnects from the server.
Callback_RecvFunction that is called when data (Binary/String) is received from a client
Callback_ReceivingFunction that is called when being receiving data from a client
Callback_TimedoutFunction that is called when a client is timed out
Functions
_Class_TCPServerClass constructor
StartupStarts the server.
ShutdownDisconnect all clients, and shutdown the server
ProcessFunction that calls all the server callbacks when events occures (New client, Receiving...)
SendSend data (Binary/String) to a client
BroadcastSend data to all connected clients
DisconnectDisconnect a specified client
DisconnectAllDisconnect all connected clients.
NbrClientsReturn the total number of connected clients.
SocketIDListReturn a 1-based array of all currently connected client’s socket ID
SocketID2IPReturn the IP of a given client’s socket
IP2SocketIDReturn the socket ID of a given client’s IP
SocketID2hSocketReturn the Socket Handle of a client’s Socket ID
ClientPropertySetSet an extended property of a client to a specified value
ClientPropertyGetGet the value of a client’s extended property
ClientGetBufferLenReturn the current buffer lenght of a client

_Class_TCPServer

Object that simplifiy the creation of Encrypted, multi-clients TCP Server.

There can be only ONE server per script.

Summary
Properties
ipIP Address to listen
portPort number to listen
max_recv_lenmax # of characters to receive (TCPRecv)
TimeoutReceiving timeout (in ms).
Callback_NewClientFunction called when a new client connects to the server.
Callback_LostClientFunction called when a client disconnects from the server.
Callback_RecvFunction that is called when data (Binary/String) is received from a client
Callback_ReceivingFunction that is called when being receiving data from a client
Callback_TimedoutFunction that is called when a client is timed out
Functions
_Class_TCPServerClass constructor
StartupStarts the server.
ShutdownDisconnect all clients, and shutdown the server
ProcessFunction that calls all the server callbacks when events occures (New client, Receiving...)
SendSend data (Binary/String) to a client
BroadcastSend data to all connected clients
DisconnectDisconnect a specified client
DisconnectAllDisconnect all connected clients.
NbrClientsReturn the total number of connected clients.
SocketIDListReturn a 1-based array of all currently connected client’s socket ID
SocketID2IPReturn the IP of a given client’s socket
IP2SocketIDReturn the socket ID of a given client’s IP
SocketID2hSocketReturn the Socket Handle of a client’s Socket ID
ClientPropertySetSet an extended property of a client to a specified value
ClientPropertyGetGet the value of a client’s extended property
ClientGetBufferLenReturn the current buffer lenght of a client

Properties

ip

IP Address to listen

port

Port number to listen

max_recv_len

max # of characters to receive (TCPRecv)

Timeout

Receiving timeout (in ms).

When this timeout is reached, the buffer is fluched, and the data that was being transfered is lost.

See Callback_Timedout

Callback_NewClient

Function called when a new client connects to the server.

The function must be as this prototype

_Func($iSocket, $sIP)
$iSocketSocket ID of the client
$sIPIP Adress of the client

Callback_LostClient

Function called when a client disconnects from the server.

The function must be as this prototype

_Func($iSocket, $sIP)
$iSocketSocket ID of the client
$sIPIP Adress of the client

Callback_Recv

Function that is called when data (Binary/String) is received from a client

The function must be as this prototype

_Func($iSocket, $sIP, $s_bData)
$iSocketSocket ID of the client
$sIPIP Adress of the client
$s_bDataReceived data (Binary/String)

Callback_Receiving

Function that is called when being receiving data from a client

The function must be as this prototype

_Func($iSocket, $sIP, $BufferLenght)
$iSocketSocket ID of the client
$sIPIP Adress of the client
$BufferLenghtLenght of the client’s receiving buffer (Not equal to the final lenght/size of the received data, it’s just an indication)

Callback_Timedout

Function that is called when a client is timed out

The function must be as this prototype

_Func($iSocket, $sIP, $BufferLenght_LostData)
$iSocketSocket ID of the client
$sIPIP Adress of the client
$BufferLenght_LostDataLenght of the buffer when timed out, corresponds to the amount of lost data (because buffer is flushed when time out occures)

Functions

_Class_TCPServer

Class constructor

Parameters

$ipValue to assign to ip
$portValue to assign to port
$max_clientsMaximum number of clients at the same time
$Ext_Info_NbrNumber of extended data that can be associated with each client (see ClientPropertySet & ClientPropertyGet)
$encrypt_pwdData encryption password.  Must be the same as in the clients.

Startup

Starts the server.

Parameters

$cb_NewClientValue to assign to Callback_NewClient
$cb_LostClientValue to assign to Callback_LostClient
$cb_RecvValue to assign to Callback_Recv
$cb_ReceivingValue to assign to Callback_Receiving
$cb_TimedoutValue to assign to Callback_Timedout

Return value

SuccessReturns main socket identifier.
FailureReturns 0 and set @error to:
  • 1 IPAddr is incorrect.
  • 2 port is incorrect.

windows API WSAGetError return value (see MSDN).

Shutdown

Disconnect all clients, and shutdown the server

Return value

SuccessReturns 1.
FailureReturns 0 and set @error = windows API WSAGetError return value (see MSDN).

Process

Function that calls all the server callbacks when events occures (New client, Receiving...)

Remark

This function must be called in loop, or in AdlibRegister

Send

Send data (Binary/String) to a client

Parameters

$iSocketSocket ID of the client
$s_bDataThe data to be sent (Binary/String)

Return value

SuccessReturns number of bytes sent to the server.
FailureReturns 0 and set @error = windows API WSAGetError return value (see MSDN).

Broadcast

Send data to all connected clients

Parameters

$s_bDataData to be sent (Binary/String)

Return value

SuccessNumber of clients that data was sent to them successfully, @extended = Percentage of succes (Return value / Total number of clients)
Failure0 and

@error = 1 -> Server not started @error = 2 -> No any client received data

Disconnect

Disconnect a specified client

Parameters

$iSocketSocket ID of the client to disconnect

DisconnectAll

Disconnect all connected clients.

Return

Success1
Failure0 (The server is not started)

NbrClients

Return the total number of connected clients.

SocketIDList

Return a 1-based array of all currently connected client’s socket ID

Return value

1-based array of connected sockets ID, $elem[0] = Number of sockets

SocketID2IP

Return the IP of a given client’s socket

Parameters

$iSocketSocket ID of a client

Return value

SuccesThe IP Address
Failed0 And

@error = 1 -> Invalide SocketID @error = 2 -> No such SocketID connected

IP2SocketID

Return the socket ID of a given client’s IP

Parameters

$sIPIP of a client

Return value

SuccesSocketID of the corresponding IP Address
Failed0 And set @error = 1

SocketID2hSocket

Return the Socket Handle of a client’s Socket ID

Parameters

$iSocketSocket ID of a client

Return value

SuccesSocket Handle (usable by native TCP functions) of the corresponding SocketID
Failed-1 And

@error = 1 -> Invalide SocketID @error = 2 -> No such SocketID connected

ClientPropertySet

Set an extended property of a client to a specified value

Parameters

$hSocketSocket ID of the client
$iIndexIndex of the property we want to modifie (0-based)
$DataData to assign to the property

Return value

Succes1
Failed0 If invalid SocketID or Extended property ID

ClientPropertyGet

Get the value of a client’s extended property

Parameters

$hSocketSocket ID of the client
$iIndexIndex of the property we want to get (0-based)

Return value

SuccesThe value of the Extended Property
Failed0 If invalid SocketID or Extended property ID

ClientGetBufferLen

Return the current buffer lenght of a client

Parameters

$iSocketSocketID of a client

Return value

SuccesBinary Lenght of the client’s buffer
Failed-1 If invalid SocketID
Function that is called when a client is timed out
IP Address to listen
Port number to listen
Set an extended property of a client to a specified value
Get the value of a client’s extended property
Function called when a new client connects to the server.
Function called when a client disconnects from the server.
Function that is called when data (Binary/String) is received from a client
Function that is called when being receiving data from a client
Close