Skip to main content

Event Trigger - TCP

The TCP Trigger creates a TCP Listener which then listens persistently for connections from TCP network clients. For each established connection, the server communicates with the client based on the logic configured using some messaging rules.

TCP > Connection group and Interaction rules inner tab

Connection

Before configuring any interaction rules you must create a Connection. Click on Manage Connections to add a new Connection. Select TCP connection and alter connection settings.

Interaction rules tab

The tab is split into two panes: a tree on the left that holds the rule hierarchy, and a properties panel on the right that displays the settings for the rule selected in the tree. A toolbar above the tree provides commands to manage the rules.

Available commands:

Append rule: a drop‑down with three rule types — Send, Receive, and Check — for appending the corresponding rule under the current selection. Delete: delete a rule with all child rules. Demo: automatically create a set of rules with an example of a simple communication algorithm between a client and a server. The command is only available when the rule tree is empty.

The properties panel on the right shows a General tab for every rule (rule name, result message key, Use key as format string, Fire trigger, Force exit) and one of the following rule‑type tabs depending on the selected rule:

  • Send — text body, number of iterations, delay before send (ms), delay after send (ms)
  • Receive — number of iterations and receive timeout in seconds (-1 uses the Connection timeout)
  • Check — match condition, match value, and exclusive group index used to combine mutually exclusive conditions

See "TCP messaging rules" section in the TCP Task topic.

TCP > Test console inner tab

You are able to test the configured messaging rules without saving the settings. As a remote TCP client, a similar test scenario can be launched in the TCP Task UI form, or a test application can be compiled and launched (see sample TCP Client application code below).

Setup maximum number of iterations

All rules with a large or infinite number of repetitions automatically terminate after the specified maximum number of iterations. The value can be set between 1 and 999; the default is 10. Click the Test button to execute the configured rules against the Log window without saving the trigger.

TCP Trigger Result Variables

TextBody

The triggered message content decoded with connection's codepage.

ReceivedTime

The triggered message receipt time.

FromAddress

The triggered message sender address in the form "host:port".

Messages

A list of processed messages in the form { Key, TextBody }, so that the text of a particular message is available by specifying Key in a variable template.

Attention: when using variables with the result of TCP trigger in tasks, for example {TRIGGER(...|TCP.Result.TCP.TextBody)}, it should be mentioned that in case of simultaneous connection of several TCP clients, the content of a variable may change unexpectedly. In practice, a trigger may fire twice when receiving messages from two different clients, but the value of {TRIGGER(...|TCP.Result.TCP.TextBody)} will contain the message that came last.

Sample TCP Client application code (C#)

using System.Net.Sockets;
using System.Text;

namespace TCP_Client
{
internal class Program
{
static void Main()
{
/*
* Connecting
*/
using var tcpclnt = new TcpClient();
Console.WriteLine("Connecting .....");

tcpclnt.Connect("127.0.0.1", 9090);
using var stm = tcpclnt.GetStream();

if (stm.CanWrite)
{
Console.WriteLine("Connected");
Thread.Sleep(1000);
}
else
{
Console.WriteLine("The stream is unavailable");
return;
}

/*
* WARNING:
* the encoding should be the same as configured in the TCP Connection / Common settings / Code page
*/
var enc = Encoding.UTF8;
string message;
byte[] buffer;

/*
* Sending "Handshake"
*/
message = "Handshake";
Console.WriteLine();
Console.WriteLine($"Sending '{message}' .....");
buffer = enc.GetBytes(message);
stm.Write(buffer, 0, buffer.Length);

Console.WriteLine($"'{message}' sent");
Thread.Sleep(1000);

/*
* Receiving a response
*/
Console.WriteLine();
Console.WriteLine("Receiving .....");
buffer = new byte[tcpclnt.ReceiveBufferSize];
var cnt = stm.Read(buffer, 0, buffer.Length);

message = enc.GetString(buffer, 0, cnt);
Console.WriteLine("Message received : " + message);

/*
* Checking the response
*/
const string heartbeat_response = "Heartbeat";
if (message == heartbeat_response)
{
Console.WriteLine("Response checked. Ready to send a command");
Thread.Sleep(1000);
}
else
{
Console.WriteLine($"The response is invalid. Must be '{heartbeat_response}'");
return;
}

/*
* Sending some command
*/
Console.WriteLine();
Console.Write("Enter the command to be transmitted : ");
message = Console.ReadLine() ?? string.Empty;

Console.WriteLine($"Sending '{message}' .....");
buffer = enc.GetBytes(message);
stm.Write(buffer, 0, buffer.Length);

Console.WriteLine("Command sent.");
Console.WriteLine("Exit.");
}
}
}