1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 05:14:21 -05:00
denoland-deno/docs/examples/tcp_echo.md

42 lines
1.1 KiB
Markdown
Raw Normal View History

2020-05-06 18:21:13 -04:00
## TCP echo server
2020-05-26 10:08:23 -04:00
This is an example of a server which accepts connections on port 8080, and
returns to the client anything it sends.
2020-05-06 18:21:13 -04:00
```ts
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
for await (const conn of listener) {
Deno.copy(conn, conn);
}
```
When this program is started, it throws PermissionDenied error.
```shell
$ deno run https://deno.land/std/examples/echo_server.ts
error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag
► $deno$/dispatch_json.ts:40:11
at DenoError ($deno$/errors.ts:20:5)
...
```
For security reasons, Deno does not allow programs to access the network without
explicit permission. To allow accessing the network, use a command-line flag:
```shell
deno run --allow-net https://deno.land/std/examples/echo_server.ts
2020-05-06 18:21:13 -04:00
```
To test it, try sending data to it with netcat:
```shell
$ nc localhost 8080
hello world
hello world
```
Like the `cat.ts` example, the `copy()` function here also does not make
unnecessary memory copies. It receives a packet from the kernel and sends back,
without further complexity.