webassembly上的UDP套接字

roo*_*oof 7 c sockets webassembly

我正在尝试将用C和C++编写的桌面应用程序移植到webassembly平台,并正在调查是否可行.该应用程序所做的重要事情之一是通过发送和接收UDP消息进行通信.我已经实现了最小的UDP客户端,它只创建UDP套接字并将数据包发送到服务器(它本地构建并在同一台机器上作为单独的可执行文件运行).socket,bind和sendto API不返回任何错误,一切看起来都有效,但服务器端没有收到任何消息,wireshark在该端口上没有显示任何活动.

UDP套接字是仅在webassembly libc端口存根,还是在某些Web标准连接(例如WebRTC)之上实现?

客户端代码如下.我检查了本机版本是否正常工作.

#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define BUFLEN 512
#define NPACK 100
#define PORT 9930

void diep(char *s)
{
  perror(s);
  exit(1);
}

#define SRV_IP "127.0.0.1"


int main(void)
{
  struct sockaddr_in si_other;
  int s, i, slen=sizeof(si_other);
  char buf[BUFLEN];

  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    diep("socket");

  memset((char *) &si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(PORT);
  if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
  }

  for (i=0; i<NPACK; i++) {
    printf("Sending packet %d\n", i);
    sprintf(buf, "This is packet %d\n", i);
    if (sendto(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, slen)==-1)
      diep("sendto()");
  }

  close(s);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我按照http://webassembly.org/getting-started/developers-guide/的说明来构建和运行它.

提前感谢任何帮助或线索!

roo*_*oof 6

我发现了如何在 webassembly 中实现 UDP 套接字。实际上,它们是由 websockets 模拟的。如果客户端和服务器都是网络组件,它可能会工作,但我的服务器是本地构建的。由于 wasm 不支持动态链接,所有代码(包括 libc 实现)都捆绑到一个 JS 文件中,我们是否可以找到 UDP sendto 实现:

// if we're emulating a connection-less dgram socket and don't have
      // a cached connection, queue the buffer to send upon connect and
      // lie, saying the data was sent now.
      if (sock.type === 2) {
        if (!dest || dest.socket.readyState !== dest.socket.OPEN) {
          // if we're not connected, open a new connection
          if (!dest || dest.socket.readyState === dest.socket.CLOSING || dest.socket.readyState === dest.socket.CLOSED) {
            dest = SOCKFS.websocket_sock_ops.createPeer(sock, addr, port);
          }
          dest.dgram_send_queue.push(data);
          return length;
        }
      }
Run Code Online (Sandbox Code Playgroud)