2022-03-30 07:57:40 +00:00
# "socket-api" sample introduction
2022-03-10 07:13:38 +00:00
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
2024-01-24 05:42:45 +00:00
Please install WASI SDK, download the [wasi-sdk release ](https://github.com/WebAssembly/wasi-sdk/releases ) and extract the archive to default path `/opt/wasi-sdk` .
2022-03-10 07:13:38 +00:00
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
```
2022-09-29 10:16:16 +00:00
`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`
2022-03-10 07:13:38 +00:00
2022-03-30 07:57:40 +00:00
> Note that iwasm is built with libc-wasi and lib-pthread enabled.
2022-03-10 07:13:38 +00:00
## Run workload
2022-09-29 10:16:16 +00:00
### TCP client/server
2022-03-10 07:13:38 +00:00
Start the tcp server, which opens port 1234 and waits for clients to connect.
2022-03-30 07:57:40 +00:00
2022-03-10 07:13:38 +00:00
```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.
2022-03-30 07:57:40 +00:00
2022-03-10 07:13:38 +00:00
```bash
cd build
./iwasm --addr-pool=127.0.0.1/15 tcp_client.wasm
```
The output of client is like:
2022-03-30 07:57:40 +00:00
2022-03-10 07:13:38 +00:00
```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
```
2022-03-30 07:57:40 +00:00
`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
2023-03-08 08:36:08 +00:00
$ ./iwasm --addr-pool=127.0.0.1/0 ./send_recv.wasm
2022-03-30 07:57:40 +00:00
```
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
```
2022-09-29 10:16:16 +00:00
### Socket options
2022-09-22 13:46:14 +00:00
`socket_opts.wasm` shows an example of getting and setting various supported socket options
```bash
2022-10-07 02:31:21 +00:00
$ ./iwasm socket_opts.wasm
2022-09-22 13:46:14 +00:00
```
2022-10-07 02:31:21 +00:00
The output is:
2022-09-22 13:46:14 +00:00
```bash
[Client] Create TCP socket
[Client] Create UDP socket
[Client] Create UDP IPv6 socket
2022-10-07 02:31:21 +00:00
setsockopt SO_RCVTIMEO result is expected
getsockopt SO_RCVTIMEO result is expected
2022-09-22 13:46:14 +00:00
...
[Client] Close sockets
```
2022-10-07 02:31:21 +00:00
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
2023-03-03 07:00:54 +00:00
$ ./iwasm --addr-pool=127.0.0.1/15 timeout_client.wasm
2022-10-07 02:31:21 +00:00
```
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
```
2022-09-29 10:16:16 +00:00
### Domain name server resolution
`addr_resolve.wasm` demonstrates the usage of resolving a domain name
```
2023-03-08 08:36:08 +00:00
$ ./iwasm --allow-resolve=*.com addr_resolve.wasm github.com
2022-09-29 10:16:16 +00:00
```
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
2022-03-10 07:13:38 +00:00
Refer to [socket api document ](../../doc/socket_api.md ) for more details.