Intermediate
cli
TCP Connector: Ping
An example of connecting to a TCP server on localhost and writing a 'ping' message to the server.
Instantiate an instance of text encoder to write to the TCP stream.
!--frsh-copybutton:1-->
const encoder = new TextEncoder();
Establish a connection to our TCP server that is currently being run on localhost port 8080.
!--frsh-copybutton:2-->
const conn = await Deno.connect({
hostname: "127.0.0.1",
port: 8080,
transport: "tcp",
});
Encode the 'ping' message and write to the TCP connection for the server to receive.
!--frsh-copybutton:3-->
await conn.write(encoder.encode("ping"));
Run this example locally using the Deno CLI:
deno run --allow-net https://byexample-wwv03xf36j0g.deno.dev/tcp-connector.ts
Additional resources: