<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.soldat.pl/index.php?action=history&amp;feed=atom&amp;title=TCP_Socket_Example</id>
		<title>TCP Socket Example - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.soldat.pl/index.php?action=history&amp;feed=atom&amp;title=TCP_Socket_Example"/>
		<link rel="alternate" type="text/html" href="https://wiki.soldat.pl/index.php?title=TCP_Socket_Example&amp;action=history"/>
		<updated>2026-05-26T02:25:00Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>https://wiki.soldat.pl/index.php?title=TCP_Socket_Example&amp;diff=181&amp;oldid=prev</id>
		<title>Freeman: Created page with &quot;As of 2.6.0, Soldat Server Scripts are now able to establish TCP Connections.  '''&lt;pre&gt;NOTE: These features are only enabled when Safe Mode is turned off!       Start the serv...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.soldat.pl/index.php?title=TCP_Socket_Example&amp;diff=181&amp;oldid=prev"/>
				<updated>2012-08-15T11:40:55Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;As of 2.6.0, Soldat Server Scripts are now able to establish TCP Connections.  &amp;#039;&amp;#039;&amp;#039;&amp;lt;pre&amp;gt;NOTE: These features are only enabled when Safe Mode is turned off!       Start the serv...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;As of 2.6.0, Soldat Server Scripts are now able to establish TCP Connections.&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;pre&amp;gt;NOTE: These features are only enabled when Safe Mode is turned off!&lt;br /&gt;
      Start the server with -safe 0 to disable Safe Mode.&amp;lt;/pre&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
'''Socket Related Functions:&amp;lt;br&amp;gt;'''&lt;br /&gt;
 - ConnectSocket&lt;br /&gt;
 Use this function to create the socket and connect it. &lt;br /&gt;
 - CloseSocket&lt;br /&gt;
 Disconnect a socket created with CreateSocket and destroy from memory.&lt;br /&gt;
 - SendData&lt;br /&gt;
 Send a string to a socket.&lt;br /&gt;
 - ReadLnSocket&lt;br /&gt;
 Read a line from a socket to a variable (Line end character is 0A (chr(10))&lt;br /&gt;
&lt;br /&gt;
For your convenience, I have provided a basic example of how to put all these functions into practice.&lt;br /&gt;
&lt;br /&gt;
'''SocketExample.pas:&amp;lt;br&amp;gt;''' &lt;br /&gt;
&amp;lt;source lang=&amp;quot;pascal&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
 ////////////////////////////////////////////////&lt;br /&gt;
 //// Soldat Server TCP Socket Usage Example ////&lt;br /&gt;
 ////     by EnEsCe - http://enesce.com      ////&lt;br /&gt;
 ////  Please leave this message if you use  ////&lt;br /&gt;
 ////      any of these functions! &amp;lt;3 &amp;lt;3     ////&lt;br /&gt;
 ////////////////////////////////////////////////&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
 Usage: (Creates socket in a seperate thread)&lt;br /&gt;
  CreateSocket('127.0.0.1',23073);&lt;br /&gt;
 Use the OnDataReceived event below for any data parsing&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var&lt;br /&gt;
 SocketIndex: Integer;&lt;br /&gt;
 SocketConnected: Array of Boolean;&lt;br /&gt;
&lt;br /&gt;
procedure OnDataReceived(Index: Integer; Buffer: String);&lt;br /&gt;
begin&lt;br /&gt;
 if (Index = 0) or (SocketConnected[Index] = false) then exit; // Disconnected socket?&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
procedure MainSocketLoop(Index: Integer);&lt;br /&gt;
var&lt;br /&gt;
 Buff: String;&lt;br /&gt;
begin&lt;br /&gt;
 while SocketConnected[Index] do begin&lt;br /&gt;
  ReadLnSocket(Index,Buff);&lt;br /&gt;
  if Buff &amp;lt;&amp;gt; '' then OnDataReceived(Index,Buff);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
 end;&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
procedure OnConnected(Index: Integer);&lt;br /&gt;
begin&lt;br /&gt;
 SetArrayLength(SocketConnected,GetArrayLength(SocketConnected)+2);&lt;br /&gt;
 ThreadFunc([Index],'MainSocketLoop');&lt;br /&gt;
 SocketConnected[GetArrayLength(SocketConnected)-1] := true;&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
procedure OnDisconnected(Index: Integer);&lt;br /&gt;
begin&lt;br /&gt;
 SocketConnected[GetArrayLength(SocketConnected)-1] := false;&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
procedure ThreadedCreate(IP: String;Port: Integer;var IndexVar: Integer);&lt;br /&gt;
begin&lt;br /&gt;
 ConnectSocket(IP,Port,IndexVar);&lt;br /&gt;
 OnConnected(IndexVar);&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
procedure CreateSocket(IP: String;Port: Integer);&lt;br /&gt;
begin&lt;br /&gt;
 ThreadFunc([IP,Port,SocketIndex],'ThreadedCreate');&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Freeman</name></author>	</entry>

	</feed>