MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / main

Function main

examples/capi/unix_domain_socket/server.cpp:12–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10#include "wrapper.h"
11
12int main() {
13 const char *SockPath = "/tmp/WasmEdge.socket";
14 int SockFd, ClientFd;
15 socklen_t Size, ClientSize;
16 sockaddr_un Addr{};
17 sockaddr_un ClientAddr{};
18
19 if (SockFd = WasmedgeSocket(AF_UNIX, SOCK_STREAM, 0); SockFd < 0) {
20 perror("ERROR opening socket");
21 exit(1);
22 }
23
24 Addr.sun_family = AF_UNIX;
25 strcpy(Addr.sun_path, SockPath);
26 Size = offsetof(sockaddr_un, sun_path) + strlen(Addr.sun_path);
27
28 // unlink(SockPath);
29 if (WasmedgeBind(SockFd, reinterpret_cast<sockaddr *>(&Addr), Size) < 0) {
30 perror("ERROR bind");
31 exit(1);
32 }
33
34 if (WasmedgeListen(SockFd, 10) < 0) {
35 perror("ERROR listen");
36 exit(1);
37 }
38
39 ClientSize = sizeof(ClientAddr);
40 if (ClientFd = WasmedgeAccept(
41 SockFd, reinterpret_cast<sockaddr *>(&ClientAddr), &ClientSize);
42 ClientFd < 0) {
43 perror("ERROR accept");
44 exit(1);
45 }
46
47 constexpr int BufMaxSize = 256;
48 char Buf[BufMaxSize + 1];
49
50 while (true) {
51 if (int Len = read(ClientFd, Buf, BufMaxSize); Len >= 0) {
52 if (Len == 0)
53 break;
54
55 Buf[Len] = 0;
56 printf("Client: %s\n", Buf);
57
58 // reverse the string
59 std::reverse(Buf, Buf + Len);
60 write(ClientFd, Buf, strlen(Buf));
61 } else {
62 perror("ERROR read");
63 exit(1);
64 }
65 }
66
67 printf("Server Closed\n");
68 close(ClientFd);
69 close(SockFd);

Callers

nothing calls this directly

Calls 6

WasmedgeSocketFunction · 0.85
WasmedgeBindFunction · 0.85
WasmedgeListenFunction · 0.85
WasmedgeAcceptFunction · 0.85
readFunction · 0.85
writeFunction · 0.85

Tested by

no test coverage detected