Skip to main content

PoTcpClient

Represents a TCP client.

Methods​

close(): void​

v0.42.0

Closes the socket. Pending callbacks gets an error.

connect(host: string, port: number, callback: function): void​

v0.42.0

Connects to the specified host and port. If an error occurs, the callback will be passed an error as the first argument.

Example​

client:connect("non-existent-domain", 9999, function(err)
if err then
print("Failed to connect", err)
end
end)

read(callback: function): void​

v0.42.0

Reads any available data. Returns at most 8192 bytes. EOF and failures arrive as err with nil data.

Example​

client:read(function(err, data)
if err then
print("Error when reading", err)
return
end

print("Received data", data)
end)

read_exactly(n: number, callback: function): void​

v0.42.0

Reads exactly n number of bytes from the connection.

Example​

client:read_exactly(4, function(err, data)
if err then
print("Error when reading", err)
return
end

print("Read 4 bytes", data)
end)

tls(callback: function): void​

v0.42.0

Begins a TLS handshake. The TCP client needs to be connected first.

Example​

client:tls(function(err)
if err then
print("Failed to perform TLS handshake")
return
end

-- connection is now secure
end)

write(data: string, callback?: function): void​

v0.51.0

Queues a write of the data to the socket. If given, callback is invoked when the data has been written (or errored).

Example​

client:write("hello", function(err)
if err then
print("Error when writing", err)
end
end)