我正在尝试编写一个google原生客户端(pNacl)模块。客户端应该从远程服务器获取一些数据。
从模块调用函数可以正常工作。
我还处于起步阶段,根本无法让客户端发送任何数据。即使是用getaddrinfo查找IP地址也不起作用。
当我使用wireshark转储网络流量时,可以看到没有发送任何数据包。errno使用"Function not implemented“引用getaddrinfo。即使是这个简单的代码也不能工作。它可以作为一个独立的客户端工作,而不是作为一个本机客户端模块。
浏览器还会显示一个错误:
**来自不可信代码的信号4: pc=6d98000b3360
有谁知道我做错了什么吗?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <errno.h>
void foo(){
int rc;
struct addrinfo hints, *info;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
hints.ai_family = AF_INET;
fprintf(stderr, "Trying: www.google.com\n");
rc = getaddrinfo("www.google.com", "80", &hints, &info);
if(rc != 0) {
fprintf(stdout, "getaddrinfo: %s\n", gai_strerror(rc));
fprintf(stdout, "Error: %s\n", strerror(errno));
}
freeaddrinfo(info);
}发布于 2016-09-29 23:14:23
所以我最终还是让它工作了。主要问题是,函数是在主线程上调用的。我使用的是nacl_io库,它必须在后台线程中调用,并且需要用nacl_io_init_ppapi(...)正确初始化。由于某些原因,通过nacl_io_init()进行的初始化无法正常工作。
因此,我将两者都添加到pp::Instance类的构造函数中。在函数foo()中添加线程也不起作用。看起来必须在开始时调用它。
#include <pthread.h>
#include <nacl_io.h>
...
pthread_t handle_msg_thread;
void *handleMsgThreadFunc(void * data); // Calls function foo()
class MyInstance : public pp::Instance {
public:
explicit MyInstance(PP_Instance instance) :pp::Instance(instance) {
nacl_io_init_ppapi(instance, pp::Module::Get()->get_browser_interface());
pthread_create(&handle_msg_thread, NULL, &handleMsgThreadFunc, NULL);
}
...
}发布于 2016-09-24 01:49:17
您的网络访问是通过NaClIO层进行的,该层在其实现中调用PPAPI (Pepper Plugin API)。这些API需要特殊权限-出于安全原因,TCP/UDP不会暴露在开放的web上。
请查看此帖子:https://groups.google.com/forum/#!topic/native-client-discuss/NmIUvpLZ1uI
https://stackoverflow.com/questions/39665572
复制相似问题