Skip to content

Commit ffa77d1

Browse files
committed
Add TCP echo server in the example
1 parent 4378932 commit ffa77d1

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

tcp-port-forwarding/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,52 @@
22

33
This guide outlines methods for forwarding TCP ports between your local machine and remote dstack app instances.
44

5+
## A simple TCP echo server
6+
7+
Let's create a simple TCP echo server in python and deploy it to dstack:
8+
9+
```yaml
10+
services:
11+
echo-server:
12+
image: python:3.9-slim
13+
command: |
14+
python -c "
15+
import socket;
16+
HOST = '0.0.0.0';
17+
PORT = 8080;
18+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
19+
s.bind((HOST, PORT));
20+
s.listen();
21+
while True:
22+
conn, addr = s.accept();
23+
print('Connected by', addr);
24+
conn.sendall(b'welcome')
25+
while True:
26+
data = conn.recv(1024);
27+
if not data:
28+
break;
29+
conn.sendall(data)
30+
"
31+
ports:
32+
- "8080:8080"
33+
```
34+
35+
Run the following command to forward local port `8080` to the echo server:
36+
37+
```bash
38+
socat TCP-LISTEN:8080,fork,reuseaddr OPENSSL:<app-id>-8080.<dstack-gateway-domain>:443
39+
```
40+
41+
Use `nc` as client to test the echo server:
42+
43+
```bash
44+
$ nc 127.0.0.1 8080
45+
hello
46+
hello
47+
```
48+
Press Ctrl+C to stop the nc client.
49+
50+
551
## SSH Access
652
For dstack apps using dev OS images, SSH access is available through the CVM. Connect via dstack-gateway (formerly tproxy) by:
753

0 commit comments

Comments
 (0)