【问题标题】:UDP socket at webassemblywebassembly 上的 UDP 套接字
【发布时间】:2017-12-02 19:27:18
【问题描述】:

我正在尝试将我用 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;
}

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

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

【问题讨论】:

  • Wireshark 是否能够在环回接口上捕获数据包(即将数据包发送到同一台机器)取决于平台和您选择的数据包嗅探器接口。例如,在 Windows + winpcap 上,这是不可能的。你在哪个平台上?
  • 是的,我在 Windows 上,我会尝试将服务器放到另一台机器上。谢谢!
  • 我已尝试向远程主机发送数据包。现在,如果我运行本机构建的客户端应用程序,wireshark 会显示传出数据包,并且不会显示任何 webassembly 构建。
  • WebAssembly 仅支持 TCP 套接字,因为生成的流量嵌入到 Websocket 中。你想要的在 WebAssembly 中不起作用。
  • tofro,没错,谢谢。我已经找到了 wasm 的制作方法。

标签: c sockets webassembly


【解决方案1】:

我发现了 UDP 套接字是如何在 webassembly 中实现的。实际上,它们是由 websockets 模拟的。如果客户端和服务器都是 webassemblies,它可能会起作用,但我的服务器是本地构建的。由于 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;
        }
      }

【讨论】:

    【解决方案2】:

    在浏览器中运行的任何东西都不会为您提供本机套接字访问,我怀疑浏览器供应商会强烈反对任何此类访问,因为它可能会违反安全性。

    也许随着越来越多的原生应用程序迁移到 Web,因为 webassembly 导致性能差异缩小,类似的举措会让它们改变立场,但在那之前,任何想要直接套接字控制的东西都必须保持原生应用程序。

    【讨论】:

    • 好吧,我希望在 WebRTC 之上,甚至在 that W3C draft 的实验支持之上,有一些聪明的 UDP 套接字实现。
    • 您可能会认为他们会提供对 UDP 的访问权限,因为它对实时音频等某些事情很有用:\
    • @AdnanY :本地客户端应用程序可以以与 JavaScript 应用程序类似的方式启动,并且它们提供原始套接字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2012-01-30
    • 2010-12-22
    • 1970-01-01
    • 2014-09-25
    相关资源
    最近更新 更多