TCP响应服务器

这是一个在8080端口接受请求的简单服务器,并且返回客户端发送的任何内容。

  1. const listener = Deno.listen({ port: 8080 });
  2. console.log("listening on 0.0.0.0:8080");
  3. for await (const conn of listener) {
  4. Deno.copy(conn, conn);
  5. }

当启动这个程序之后,它会抛出权限拒绝的错误。

  1. $ deno run https://deno.land/std/examples/echo_server.ts
  2. error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag
  3. $deno$/dispatch_json.ts:40:11
  4. at DenoError ($deno$/errors.ts:20:5)
  5. ...

由于安全性原因,Deno不允许程序在没有明确授权下访问网络。为了允许访问网络,使用命令行选项:

  1. deno run --allow-net https://deno.land/std/examples/echo_server.ts

尝试使用netcat发送数据来测试一下:

  1. $ nc localhost 8080
  2. hello world
  3. hello world

就像 cat.ts 的例子,这里的 copy() 函数依然没有使用多余的内存空间。代码接收到一个来自内核的包并返回,就这样而已。