This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
wasm-micro-runtime/samples/socket-api
Cengizhan Pasaoglu 2746d29751
Make robust on choosing target assumption for X86_32 support (#1241)
When WAMR_BUILD_TARGET isn't set, choosing right target is decided
by checking `CMAKE_SIZEOF_VOID_P` variable. However, choosing `X86_32`
target is not doing specifically checking size of void pointer. It is kind
a fallback target for others.

This patch explicitly checks the size of void pointer before setting the target
to `X86_32` to fix the issue.
2022-06-27 20:30:31 +08:00
..
wasm-src Fix invalid calculation of total size of bytes to send and recv (#1162) 2022-05-10 10:43:34 +08:00
CMakeLists.txt Make robust on choosing target assumption for X86_32 support (#1241) 2022-06-27 20:30:31 +08:00
README.md Enable socket-api recvmsg() and sendmsg() (#1042) 2022-03-30 15:57:40 +08:00

"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 and extract the archive to default path /opt/wasi-sdk. And install wabt, download the wabt release and extract the archive to default path /opt/wabt

Build the sample

mkdir build
cd build
cmake ..
make

iwasm and three Wasm modules, tcp_server.wasm, tcp_client.wasm, send_recv.wasm will be generated. And their corresponding native version, tcp_server, tcp_client, send_recv are generated too.

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.

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

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

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

The output of client is like:

[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.

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

The output is:

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

Refer to socket api document for more details.