ioBuster framework v25.02.4 documentation


tcp

tcp

Collection of TCP functions

Methods

(static) close(connectionId)

Closes a TCP connection

Example
const connectionId = tcp.open('localhost', 9000)
tcp.close(connectionId)
Parameters:
Name Type Description
connectionId Number

TCP connection ID

(static) getReadableBytes(connectionId) → {Number}

Get readable data length in bytes from a TCP connection.

Example
const connectionId = tcp.open('localhost', 9000)
const bytes = tcp.getReadableBytes(connectionId)
Parameters:
Name Type Description
connectionId Number

TCP connection ID

Returns:
  • Number of bytes available for read
Type
Number

(static) open(host, port) → {Number}

Creates a TCP connection

Example
const connectionId = tcp.open('localhost', 9000)
Parameters:
Name Type Description
host Number | String

Host name or IP address

port Number

Host port

Returns:
  • Connection ID
Type
Number

(static) read(connectionId, bytesToRead, options) → {Buffer}

Reads data from a TCP connection

Example
const connectionId = tcp.openConnection('localhost', 9000)
const data = tcp.read(connectionId, 5)
Parameters:
Name Type Description
connectionId Number

TCP connection ID

bytesToRead Number

Size of data to read

options Object
Properties
Name Type Attributes Default Description
waitForRequestedData Boolean <optional>
false

Wait until all requested bytes are available

timeout Boolean <optional>
30

Timeout in seconds

Returns:
Type
Buffer

(static) write(connectionId, data)

Writes data to a TCP connection

Example
const connectionId = tcp.open('localhost', 9000)
tcp.write(connectionId, 'hello')
tcp.write(connectionId, Buffer('world'))
tcp.write(connectionId, Buffer([0, 1, 2, 3]))
Parameters:
Name Type Description
connectionId Number

TCP connection ID

data Buffer | String

Data to send over TCP connection