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:
* Unable to bind to port.
// 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; } }
Required: threadPoolSize
Optional: address, blockSize, broadcastResponse, connectionCap, disableDuplexConnection, format, keepAlive, maxUnreadResponseCount, port
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.
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).
This output stream will carry tuples depending on the mandatory attribute used:
The second output port is optional and must use a Stream schema with the following three attributes:
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:
Example: `status="connected",srcIP="192.168.92.130",srcPort=39850`
Required: threadPoolSize
Optional: address, blockSize, broadcastResponse, connectionCap, disableDuplexConnection, format, keepAlive, maxUnreadResponseCount, port
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.
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.
This optional parameter with type boolean specifies the whether a response should be broadcasted to all clients at once.
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.
This optional parameter with type ShutdownMode specifies disable mode: read only or total shutdown.
Format of the data on output port
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:
This optional parameter with type uint32 specifies the maximum number of unread responses until the duplex connection is disabled.
This optional parameter with type uint32 specifies the port number on which the connections will be accepted.
This mandatory parameter with type uint32 specifies the number of threads per TCPServer operator instance.