Deno logoDenoby example

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.
const encoder = new TextEncoder();
Establish a connection to our TCP server that is currently being run on localhost port 8080.
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.
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