TCP Socket Example
As of 2.6.0, Soldat Server Scripts are now able to establish TCP Connections.
NOTE: These features are only enabled when Safe Mode is turned off! Start the server with -safe 0 to disable Safe Mode.
Socket Related Functions:
- ConnectSocket Use this function to create the socket and connect it. - CloseSocket Disconnect a socket created with CreateSocket and destroy from memory. - SendData Send a string to a socket. - ReadLnSocket Read a line from a socket to a variable (Line end character is 0A (chr(10))
For your convenience, I have provided a basic example of how to put all these functions into practice.
SocketExample.pas:
{
////////////////////////////////////////////////
//// Soldat Server TCP Socket Usage Example ////
//// by EnEsCe - http://enesce.com ////
//// Please leave this message if you use ////
//// any of these functions! <3 <3 ////
////////////////////////////////////////////////
}
{
Usage: (Creates socket in a seperate thread)
CreateSocket('127.0.0.1',23073);
Use the OnDataReceived event below for any data parsing
}
var
SocketIndex: Integer;
SocketConnected: Array of Boolean;
procedure OnDataReceived(Index: Integer; Buffer: String);
begin
if (Index = 0) or (SocketConnected[Index] = false) then exit; // Disconnected socket?
end;
procedure MainSocketLoop(Index: Integer);
var
Buff: String;
begin
while SocketConnected[Index] do begin
ReadLnSocket(Index,Buff);
if Buff <> '' then OnDataReceived(Index,Buff);
sleep(1);
end;
end;
procedure OnConnected(Index: Integer);
begin
SetArrayLength(SocketConnected,GetArrayLength(SocketConnected)+2);
ThreadFunc([Index],'MainSocketLoop');
SocketConnected[GetArrayLength(SocketConnected)-1] := true;
end;
procedure OnDisconnected(Index: Integer);
begin
SocketConnected[GetArrayLength(SocketConnected)-1] := false;
end;
procedure ThreadedCreate(IP: String;Port: Integer;var IndexVar: Integer);
begin
ConnectSocket(IP,Port,IndexVar);
OnConnected(IndexVar);
end;
procedure CreateSocket(IP: String;Port: Integer);
begin
ThreadFunc([IP,Port,SocketIndex],'ThreadedCreate');
end;