Skip to main content

TCP

This module provides a TCP client implementation for establishing connections and exchanging data with TCP network services.

TCPClient

The TCPClient class represents a TCP client that enables communication with TCP network services. It allows you to connect to a server specified by a host address and port number.

Constructor

Create a new TCP Client instance with a specified host and port.

new TCPClient(host: string, port: number);

Methods

connect()

Initiates a connection with the server.

connect(): Promise<void>;

disconnect()

Terminates the connection with the server.

disconnect(): Promise<void>;

waitData()

Waits until the client has data available. Returns true if data is received within the optional timeout period; otherwise, returns false.

waitData(timeout?: number): Promise<boolean>;

waitNewData()

Waits until the client receives new data. Returns true if new data is received within the optional timeout period; otherwise, returns false. This method waits for new data only; if data was already available before calling, it will wait for new data to arrive.

waitNewData(timeout?: number): Promise<boolean>;

getData()

Returns the data received from the server. Throws an error if the input buffer is empty.

getData(): string;

clearData()

Clears the data buffer.

clearData(): void;

sendData()

Sends a stream of data to the server.

sendData(data: string): Promise<void>;

isConnected()

Checks if the client is currently connected to the server. Returns true if connected; otherwise, returns false.

isConnected(): boolean;

Properties

host

Host address of the TCP server.

string: host;

port

Port number of the TCP server. Default is 22.

number: port;