gpt4 book ai didi

node.js - NodeJS 如何发送带有原始十六进制的 TCP 数据包

转载 作者:行者123 更新时间:2023-12-02 03:16:33 28 4
gpt4 key购买 nike

我是 Node 的新手,正在尝试编写发送原始十六进制数据的最小的 tcp 客户端。如果我应该使用缓冲区,那该怎么办?如果我可以将十六进制作为字符串发送,那该怎么做?非常感谢指导!

这里是当前的,不工作的代码:

var hexVal = `504f5354202f6c696e653320485454502f312e310d0a557365722d4167656e743a206e6f64652d6170700d0a4163636570743a202a2f2a0d0a686f73743a203139322e3136382e31342e39343a333030300d0a636f6e74656e742d747970653a206170706c69636174696f6e2f6a736f6e0d0a636f6e74656e742d6c656e6774683a2031390d0a436f6e6e656374696f6e3a20636c6f73650d0a0d0a227b757365726e616d653a202776616c277d22` // my raw hex, unwantendly sent as string


var net = require('net');

var HOST = '192.168.14.94';
var PORT = 3000;

var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.write(hexVal);
});

client.on('data', function(data) { // 'data' is an event handler for the client socket, what the server sent
console.log('DATA: ' + data);
client.destroy(); // Close the client socket completely

});

// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});

服务器:

nc -lvp 3000

最佳答案

这解决了它:

var bytesToSend = [0x50, 0x4f, ...],
hexVal = new Uint8Array(bytesToSend);

关于node.js - NodeJS 如何发送带有原始十六进制的 TCP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55975195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com