Example client application to interact with a TCP listener

This section describes how to build and execute a simple example TCP client application to interact with a TCP listener, TCP listener map and listener trigger. From this simple example, the basics of sending data to a TCP listener and receiving a reply message from a listener trigger can be expanded to meet your solution requirements.

Prerequisites

  • You have an Enterprise Gateway node where the example TCP listener project (listener, listener map, project and trigger) will be imported.
  • You have installed  JDK 1.6 or later on the computer that you will use to compile and execute the example TCP client application. 

Step-by-step guide

  1. Import the attached into your Enterprise Gateway node using the Workbench.
  2. The imported items include:
    • A TCP listener, ExampleTCPListener5050:
      • Listens on Port 5050 for TCP clients to connect.
      • Mapping log is enabled
      • Payload format is ASCII
    • A TCP listener map, ExampleDataFromAndReplyToTCPClient:
      • Maps the data from the received message to be delivered to the listener trigger.
      • Maps reply data from the listener trigger to the reply message sent back to the example TCP client application
    • A project, ExampleTCPListenerProject, that contains a listener trigger, ExampleTCPListenerTrigger:
      • Max in Progress is set to 1
      • Trigger reporting is set to On
  3. Access your Enterprise Gateway node with a Workbench and start the listener ExampleTCPListener5050, the project ExampleTCPListenerProject and listener trigger ExampleTCPListenerTrigger.
  4. Copy the java source code included in the Info block below into a source file called ExampleTCPClient.java.
    The example client sends an ASCII request payload that has the following fields delimited by a comma (,):

    • ListenerMapIdentifer
    • Message
    • End-of-Message delimiter(\n).

    For example:   "DataFromTCPClient,Client1,HelloThere\n"

    The listener forwards the request to the listener trigger which echoes the original message back.

    Example Client Application Source

    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;


    public class ExampleTCPClient {

    public static void main(String[] args) {
    Socket sock;
    // Specify your Enterprise Gateway IP address here
    // or provide it as a command line argument
    String hostname = "127.0.0.1";
    byte[] readBuf = new byte[1024];
    if (args.length > 0) {
    hostname = args[0];
    }
    try {

    sock = new Socket(hostname, 5050);

    OutputStream output = sock.getOutputStream();

    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output,
    "UTF-8"), true);
    writer.append("ExampleDataFromAndReplyToTCPClient,Client1,HelloThere\n");
    writer.flush();

    InputStream input = sock.getInputStream();

    BufferedInputStream bis = new BufferedInputStream(input);

    sock.setSoTimeout(30000);

    int bytesread = bis.read(readBuf);

    System.out.println(" [" + bytesread + "] Bytes Received");
    System.out.println(new String(readBuf, 0, bytesread));

    sock.close();

    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    }

  5. Compile the included java source using the following command:
    javac ExampleTCPClient.java
  6. Execute the example java client application using the following command:
    java ExampleTCPClient <your listener host name or IP>

    For example: java ExampleTCPClient 127.0.0.1

    You should see the following output if all the components are configured correctly:

    Example Client Application Output

     [49] Bytes Received

    0,Hello [Client1], your message was [HelloThere]

Next steps

From this simple example TCP client application and example TCP listener (and listener map and listener trigger) build the application logic needed for your requirements.

The listener mapping log feature can be used to help understand the message format into the TCP listener and the reply message from the listener trigger.

The trigger reporting feature can be used to help understand the execution behavior of the listener trigger.