Operator TCPServer

Toolkits > com.ibm.streamsx.tcp 2.1.1 > com.ibm.streamsx.tcp > TCPServer

The TCPServer is a multi-connection, multi-threaded full-duplex TCP operator that can accept data from one or more TCP sockets and send data back via the same socket connections. An instance of the TCPServer operator will listen for connections on the port number defined by its port parameter. A single instance of the operator can process multiple connections simultaneously (up to the value specified in the connectionCap parameter). Each line or block of data received from an external source is converted by the TCPServer operator to a Streams tuple.

To enable duplex support, add an input port to the operator. The schema of the input port must be of type TCPServerStrT or TCPServerBlobT or equivalent. The first attribute of the schema represents the data to send. The srcIP and srcPort attributes represent the server and port to send the data to. When a tuple is received on the input port, the data will be sent asynchronously via an existing socket connection. If an error occurs, the error will be logged.

A few important details about the TCPServer operator to be aware of:

  • By default, an instance of the TCPServer operator will listen for connections only on the network interface that is bound to the resolvable IP address for the hostname of the machine where this operator is running and on the port number defined by its port operator parameter. When trying to connect to the TCPServer operator from client applications, you must use the value configured in the address operator parameter. If the optional address operator parameter is not used as part of the TCPServer operator invocation, then the client applications must use the actual IP address or a fully qualified name of the TCPServer machine to connect to a TCPServer operator instance.
  • If you have used an older version of the MultiConnectionTCPServer toolkit, please note that moving to a newer version of this toolkit (i.e. version 1.0.3 or higher) will not provide backward compatibility. Because in the newer version, operator parameter syntax and the input/output stream specifications have been changed for this operator. If you are an existing user of this toolkit who is planning to use a newer version, you are advised to thoroughly read the operator invocation syntax below to understand the new features.

Exceptions

The TCPServer operator throws an exception and terminates the operator in the following cases:
* Unable to bind to port.       

Examples

// TCPServer with two threads, accepts a maximum of 4 connections

composite Main
{
graph
    stream<rstring line, rstring srcIP, uint32 srcPort> 
        Result = TCPServer() {
        param
            port : 22222u;
            threadPoolSize : 2u; 
            connectionCap: 4u;
    }
    () as Sink = FileSink(Result) {
        param
            file: "output";      
            flush: 1u;
    }
} 

// TCPServer with output as blob and optional 2nd output port
// TCPServer listen on "localhost"

composite Main
{
graph
    (stream<blob block> Result; stream<rstring status, rstring srcIP, uint32 srcPort> Status)= TCPServer() {
        param
            port : 22222u;
            threadPoolSize : 1u; 
            connectionCap: 5u;
            blockSize: 256u;
            address: "localhost";
    }
    () as ResultSink = FileSink(Result) {
        param
            file: "result";      
            flush: 1u;
    }
    () as StatusSink = FileSink(Status) {
        param
            file: "status";      
            flush: 1u;
    }
} 

// TCPServer with output as rstring
// generates output tuples when "\n" is received.
// If there is no "\n", it generates an output tuple after receiving 65535 bytes 

composite Main
{
graph
    stream<rstring line> 
        Result = TCPServer() {
        param
            port : 22222u;
            threadPoolSize : 1u; 
            connectionCap: 5u;
            blockSize: 65535u;
    }
    () as Sink = FileSink(Result) {
        param
            file: "output";      
            flush: 1u;
    }
} 

// TCPServer with keepAlive for the TCP connections

composite Main
{
graph
    stream<rstring line> 
        Result = TCPServer() {
        param
            port : 22222u;
            threadPoolSize : 1u; 
            connectionCap: 5u;
            keepAlive: {time=7200u, probes=5u, interval=100u};
     }
    () as Sink = FileSink(Result) {
        param
            file: "output";      
            flush: 1u;
    }
}

Summary

Ports
This operator has 1 input port and 2 output ports.
Windowing
This operator does not accept any windowing configurations.
Parameters
This operator supports 10 parameters.

Required: threadPoolSize

Optional: address, blockSize, broadcastResponse, connectionCap, disableDuplexConnection, format, keepAlive, maxUnreadResponseCount, port

Metrics
This operator reports 2 metrics.

Properties

Implementation
C++
Threading
Never - Operator never provides a single threaded execution context.

Input Ports

Ports (0)

The TCPServer operator has one optional input port. The schema of the input port must be of type TcpServerStrT or TCPServerBlobT or equivalent. Based on event attribute value the operator either sends the data received to the srcIP:srcPort or opens a new dynamic listener, as specified on the incoming tuple.

Properties

Output Ports

Assignments
This operator does not allow assignments to output attributes.
Ports (0)

The first output port is mandatory and must contain a tuple with one mandatory and up to two optional attributes. The output port punctuation mode is Generating. The TCPServer operator outputs a window marker punctuation when an existing connection is released. The mandatory attribute must have type rstring (for line attribute) or blob (for block attribute).

  • mandatory
    • attribute name line and type rstring
    • attribute name block and type blob
  • optional
    • attribute name srcIP and type rstring
    • attribute name srcPort and type uint32

This output stream will carry tuples depending on the mandatory attribute used:

  • line Collect characters until the end of line marker is detected or, when parameter blockSize is given, the number of received characters exceeds the blockSize. Valid values for the EOL marker are "n" and "rn". In this case, output tuples emitted by this operator will not contain the EOL marker.
  • block Collect bytes until the number of received bytes exceeds the given blockSize.

Properties

Ports (1)

The second output port is optional and must use a Stream schema with the following three attributes:

  • First attribute name is status and type rstring
  • Second attribute name is srcIP and type rstring
  • Third attribute name is srcPort and type uint32

This optional second output stream generates tuples when a new connection is established or when an existing connection is released, when a connect is rejected because of threadPoolSize/connectionCap is exceeded or when sending responses is disabled. Additional information provided in this output stream is useful for application-specific logging, filtering, book-keeping etc. The status attribute in this output stream will carry one of these values:

  • "connected"
  • "disconnected"
  • "rejected"
  • "responseDisabled"
  • "responseError"
    Example: `status="connected",srcIP="192.168.92.130",srcPort=39850`     
    

Properties

Parameters

Required: threadPoolSize

Optional: address, blockSize, broadcastResponse, connectionCap, disableDuplexConnection, format, keepAlive, maxUnreadResponseCount, port

address

This optional parameter with type rstring specifies a resolvable IP address or a machine name (e-g: myserver1.ibm.com, localhost etc.) on which the connections will be accepted. If "0.0.0.0" is given as an address, then the TCPServer operator will listen on all the available network interfaces including localhost (127.0.0.1). If the address parameter is not specified, this operator will listen on a network interface bound to the actual IP address of that machine's fully qualified host name.

Properties

blockSize

block size, flush output buffer in case of overflow. If blockSize unused or set to "0" flush buffer in case of "n" detected in TCP Stream.

Properties

broadcastResponse

This optional parameter with type boolean specifies the whether a response should be broadcasted to all clients at once.

Properties

connectionCap

This optional parameter with type uint32 specifies the maximum number of connections that can be established to a single instance of the TCPServer operator before it starts refusing additional connections.

Properties

disableDuplexConnection

This optional parameter with type ShutdownMode specifies disable mode: read only or total shutdown.

Properties

format

Format of the data on output port

Properties

keepAlive

This parameter is optional and it controls the internal behavior of the TCPServer operator in case of idle or broken connections. This parameter prevents a server from hanging on a connection when it drops, and it keeps a connection live when there is no normal activity. The value of the parameter is a tuple literal that specifies the configurable attributes. If any of the configurable attribute values for a specific keepAlive parameter is set to zero, the default system value of the keepAlive parameter is used. The configurable attributes for this parameter include:

  • uint32 time: The number of seconds that the connection sits idle before a keepalive probe is sent.
  • uint32 probes: The maximum number of probes to send to establish the state of the connection.
  • uint32 interval: The time interval in seconds between each probe.
Properties

maxUnreadResponseCount

This optional parameter with type uint32 specifies the maximum number of unread responses until the duplex connection is disabled.

Properties

port

This optional parameter with type uint32 specifies the port number on which the connections will be accepted.

Properties

threadPoolSize

This mandatory parameter with type uint32 specifies the number of threads per TCPServer operator instance.

Properties

Metrics

nConnections - Gauge

Number of inbound connections connected to the TCPServer operator.

nResponses - Counter

Number of outcoming events.

Libraries

TCP server library
Library Name: streams.spl.multiconnection.tcp.server
Library Path: ../../impl/lib
Include Path: ../../impl/include