Node.js 连接到unix socket dgram宽度节点中的一个套接字文件,如php socket_create(AF_unix,SOCK_dgram,0),但它不起作用
我遇到了一个sock文件的udp dgram连接问题。我必须将udp dgram连接从php方式转换为nodejs。但是在尝试了各种方式后都失败了 php代码如下所示:Node.js 连接到unix socket dgram宽度节点中的一个套接字文件,如php socket_create(AF_unix,SOCK_dgram,0),但它不起作用,node.js,Node.js,我遇到了一个sock文件的udp dgram连接问题。我必须将udp dgram连接从php方式转换为nodejs。但是在尝试了各种方式后都失败了 php代码如下所示: $this->socket = socket_create(AF_UNIX, SOCK_DGRAM, 0); socket_connect($this->socket, '/tmp/agent.sock') ... socket_write($this->socket, $data, $total_len)
$this->socket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
socket_connect($this->socket, '/tmp/agent.sock')
...
socket_write($this->socket, $data, $total_len)
我试过很多方法。1。使用网络模块:
const net = require('net');
const client = net.createConnection('/tmp/agent.sock')
但获取“错误:连接EPROTOTYPE”错误
2.使用unix dgram npm软件包。
var unix = require('unix-dgram');
var client = unix.createSocket('unix_dgram');
client.on('error', function(err) {
console.error(err);
});
client.connect('/tmp/agent.sock');
但下面是一个错误:
node: ../src/unix_dgram.cc:299: Nan::NAN_METHOD_RETURN_TYPE {anonymous}::Send(Nan::NAN_METHOD_ARGS_TYPE): Assertion `node::Buffer::HasInstance(buf)' failed.
3。使用http.request:
const http = require('http');
const options = {
socketPath: '/tmp/agent.sock',
path: '',
};
const callback = res => {
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', data => console.log(data));
res.on('error', data => console.error(data));
};
const clientRequest = http.request(options, callback);
clientRequest.end();
但也会出现错误:
“错误:连接EPROTOTYPE”错误
我不知道怎么修,太糟糕了。你能试试这个吗?将数据作为缓冲区发送
var unix = require('unix-dgram');
var client = unix.createSocket('unix_dgram');
client.connect('/path/to/socket');
client.on('error', function(err) {
console.error(err);
});
client.on('connect', function() {
console.log('connected');
client.on('congestion', function() {
console.log('congestion');
/* The server is not accepting data */
});
client.on('writable', function() {
console.log('writable');
/* The server can accept data */
let message = Buffer.from('PING');
client.send(message);
});
});
您正在尝试在同一台机器上建立套接字连接,对吗?