TCP listener: Ping
An example of a TCP listener on localhost that will log the message if written to and close the connection if connected to.
Instantiate an instance of text decoder to read the TCP stream bytes back into plaintext.
const decoder=newTextDecoder();Instantiate an instance of a TCP listener on localhost port 8080.
const listener= Deno.listen({hostname:"127.0.0.1",port:8080,transport:"tcp",});Await asynchronous connections that are established to our TCP listener.
forawait(const connof listener){Instantiate an buffer array to store the contents of our read TCP stream.
const buf=newUint8Array(1024);Read the contents of the TCP stream into our buffer array.
await conn.read(buf);Here we log the results of the bytes that were read into our buffer array.
console.log("Server - received: ", decoder.decode(buf));We close the connection that was established.
conn.close();}Runthis example locally using the Deno CLI:
deno run -N https://docs.deno.com/examples/scripts/tcp_listener.ts