nospaceships / node-raw-socket

Raw sockets for Node.js.
207 stars 69 forks source link

Can't send TCP packages #88

Open FunctionEurus opened 6 months ago

FunctionEurus commented 6 months ago

Hi, I'm trying to use raw socket to make TCP connections and send HTTP GET requests, but honestly I'm a totally beginner of network programming so I met some problems when coding. In README.md, send() function is declared as socket.send (buffer, offset, length, address, beforeCallback, afterCallback), but when I print the req in onSendReady(), the real object is this:

{
  buffer: <Buffer 68 65 6c 6c 6f>,
  offset: 0,
  length: 5,
  address: '155.138.142.54',
  afterCallback: [Function (anonymous)],
  beforeCallback: null
}

So the before callback and the after callback seems to be placed in wrong place? I'm not sure whether this is the source of my problem, because the error message is made up of some mojibake:

Error sending packet: Error: �ṩ��һ����Ч�IJ�����

    at Socket.onSendReady (C:\Users\funct\Desktop\node-socket\node_modules\raw-socket\index.js:102:14)
    at SocketWrap.emit (node:events:518:28)

And here is my code, thanks for your reading.

const raw = require("raw-socket");

const socket = raw.createSocket({
  protocol: raw.Protocol.TCP,
});

socket.send(
  Buffer.from("hello"),
  0,
  Buffer.from("hello").length,
  "155.138.142.54",

  (err, bytes) => {
    if (err) {
      console.error("Error sending packet:", err);
    } else {
      console.log("Packet sent successfully", bytes);
    }
  }
);

PS: when I added another function(() => {console.log("ready");}) as the fifth parameter, the error message disappeared and Packet sent successfully undefined was printed in the console. However I just couldn't catch that package in wireshark, so this was another failure.