summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/socket-api/README.md
blob: a3bc5ac1523217190e1982c900839e875712b790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# "socket-api" sample introduction

This sample demonstrates how to use WAMR socket-api to develop wasm network applications.
Two wasm applications are provided: tcp-server and tcp-client, and this sample demonstrates
how they communicate with each other.

## Preparation

Please install WASI SDK, download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`.
And install wabt, download the [wabt release](https://github.com/WebAssembly/wabt/releases) and extract the archive to default path `/opt/wabt`

## Build the sample

```bash
mkdir build
cd build
cmake ..
make
```

`iwasm` and the following Wasm modules (along with their corresponding native version) will be generated:
 * `addr_resolve.wasm`, `addr_resolve`
 * `send_recv.wasm`, `send_recv`
 * `socket_opts.wasm`, `socket_opts`
 * `tcp_client.wasm`, `tcp_client`
 * `tcp_server.wasm`, `tcp_server`
 * `udp_client.wasm`, `udp_client`
 * `udp_server.wasm`, `udp_server`

> Note that iwasm is built with libc-wasi and lib-pthread enabled.

## Run workload

### TCP client/server

Start the tcp server, which opens port 1234 and waits for clients to connect.

```bash
cd build
./iwasm --addr-pool=0.0.0.0/15 tcp_server.wasm
```

Start the tcp client, which connects the server and receives message.

```bash
cd build
./iwasm --addr-pool=127.0.0.1/15 tcp_client.wasm
```

The output of client is like:

```bash
[Client] Create socket
[Client] Connect socket
[Client] Client receive
[Client] 115 bytes received:
Buffer recieved:
Say Hi from the Server
Say Hi from the Server
Say Hi from the Server
Say Hi from the Server
Say Hi from the Server

[Client] BYE
```

`send_recv.wasm` contains a thread as a server and a thread as a client. They
send and receive data via 127.0.0.1:1234.

```bash
$ ./iwasm --addr-pool=127.0.0.1/0 ./send_recv.wasm
```

The output is:

```bash
Server is online ...
Client is running...
Start receiving.
Start sending.
Send 106 bytes successfully!
Receive 106 bytes successlly!
Data:
  The stars shine down
  It brings us light
  Light comes down
  To make us paths
  It watches us
  And mourns for us
```

### Socket options

`socket_opts.wasm` shows an example of getting and setting various supported socket options
```bash
$ ./iwasm socket_opts.wasm
```
The output is:
```bash
[Client] Create TCP socket
[Client] Create UDP socket
[Client] Create UDP IPv6 socket
setsockopt SO_RCVTIMEO result is expected
getsockopt SO_RCVTIMEO result is expected
...
[Client] Close sockets
```

The `timeout_client.wasm` and `timeout_server.wasm` examples demonstrate socket send and receive timeouts using the socket options. Start the server, then start the client.

```bash
$ ./iwasm --addr-pool=0.0.0.0/15 timeout_server.wasm
```

The output is:

```bash
Wait for client to connect
Client connected, sleeping for 10s
Shuting down
```

```bash
$ ./iwasm --addr-pool=127.0.0.1/15 timeout_client.wasm
```

The output is:

```bash
Waiting on recv, which should timeout
Waiting on send, which should timeout
Success. Closing socket 
```

The `multicast_client` and `multicast_server` examples demonstrate receiving multicast packets in WASM. Start the client and then the server with a multicast IP address and port. 

```bash
$ ./iwasm --addr-pool=0.0.0.0/0,::/0 multicast_client.wasm <Multicast IP> <Port>
$ ./iwasm --addr-pool=0.0.0.0/0,::/0 multicast_client.wasm 224.0.0.1
$ ./iwasm --addr-pool=0.0.0.0/0,::/0 multicast_client.wasm FF02:113D:6FDD:2C17:A643:FFE2:1BD1:3CD2
```

The output should be

```bash
Joined multicast group. Waiting for datagram...
Reading datagram message...OK.
The message from multicast server is: "Test message"
```

```bash
$ ./multicast_server <Multicast IP> <Port>
$ ./multicast_server 224.0.0.1
$ ./multicast_server FF02:113D:6FDD:2C17:A643:FFE2:1BD1:3CD2
```

The output should be

```bash
Datagram sent
```

### Domain name server resolution

`addr_resolve.wasm` demonstrates the usage of resolving a domain name
```
$ ./iwasm --allow-resolve=*.com addr_resolve.wasm github.com
```

The command displays the host name and its corresponding IP address:
```
Host: github.com
IPv4 address: 140.82.121.4 (TCP)
```

### UDP client/server

Start the UDP server, which opens port 1234 and waits for clients to send a message.

```bash
cd build
./iwasm --addr-pool=0.0.0.0/15 udp_server.wasm
```

Start the tcp client, which sends a message to the server and waits for the response.

```bash
cd build
./iwasm --addr-pool=127.0.0.1/15 udp_client.wasm
```

The output of client is like:

```bash
[Client] Create socket
[Client] Client send
[Client] Client receive
[Client] Buffer recieved: Hello from server
[Client] BYE
```

The output of the server is like:
```
[Server] Create socket
[Server] Bind socket
[Server] Wait for clients to connect ..
[Server] received 17 bytes from 127.0.0.1:60927: Hello from client
```

## Documentation

Refer to [socket api document](../../doc/socket_api.md) for more details.