mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-15 22:21:21 +00:00

Refer to [Networking API design](https://github.com/WebAssembly/WASI/issues/370) and [feat(socket): berkeley socket API v2](https://github.com/WebAssembly/WASI/pull/459): - Support the socket API of synchronous mode, including `socket/bind/listen/accept/send/recv/close/shutdown`, the asynchronous mode isn't supported yet. - Support adding `--addr-pool=<pool1,pool2,..>` argument for command line to identify the valid ip address range - Add socket-api sample and update the document
58 lines
1.5 KiB
Markdown
58 lines
1.5 KiB
Markdown
"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
|
|
```
|
|
|
|
The file `tcp_server.wasm`, `tcp_client.wasm` and `iwasm` will be created.
|
|
And also file `tcp_server` and `tcp_client` of native version are created.
|
|
|
|
Note that iwasm is built with libc-wasi and lib-pthread enabled.
|
|
|
|
## Run workload
|
|
|
|
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
|
|
```
|
|
|
|
Refer to [socket api document](../../doc/socket_api.md) for more details.
|