TCP Echo Server
在 Github 上编辑
An echo server is a simple network application that listens for incoming connections and requests, and then repeats back any data it receives from clients.
To test this example, try sending data to it with Netcat (Linux/MacOS only). For example, in your terminal run: echo "Hello, Deno!" | nc localhost 8080
Create a TCP listener that listens on port 8080. Log that it is listening.
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
Wait for incoming connections, When a client connects to the server, read data from the client and write it back to the client.
for await (const conn of listener) {
conn.readable.pipeTo(conn.writable);
}
使用 Deno CLI 在本地运行 此示例:
deno run --allow-net echo_server.ts