A
A
Andrew K2019-03-14 21:02:17
Node.js
Andrew K, 2019-03-14 21:02:17

How to pass a large batch of bytes to node.js using buffer?

There is such a package (dump from wireshark).

0000   02 00 00 00 45 00 00 ae 78 8b 40 00 80 06 00 00   [email protected]
0010   7f 00 00 01 7f 00 00 01 00 50 da 6e 75 42 cc 04   .........P.nuB..
0020   b2 4b fe 9e 50 18 00 1e fe a2 00 00 48 54 54 50   .K..P.......HTTP
0030   2f 31 2e 31 20 32 30 30 20 4f 4b 0d 0a 44 61 74   /1.1 200 OK..Dat
0040   65 3a 20 54 68 75 2c 20 31 34 20 4d 61 72 20 32   e: Thu, 14 Mar 2
0050   30 31 39 20 31 36 3a 35 38 3a 35 37 20 47 4d 54   019 16:58:57 GMT
0060   0d 0a 53 65 72 76 65 72 3a 20 41 70 61 63 68 65   ..Server: Apache
0070   2f 32 2e 34 2e 31 30 20 28 44 65 62 69 61 6e 29   /2.4.10 (Debian)
0080   0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68   ..Content-Length
0090   3a 20 38 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a   : 8..Connection:
00a0   20 63 6c 6f 73 65 0d 0a 0d 0a 04 00 00 00 07 50    close.........P
00b0   f9 6e                                             .n

How to send it using node?
As you can see, there are also standard headers, and I tried writeHead.
It turned out that 100% similarity is needed ...
I googled that you can work with bytes using Buffer.
const buffer = new Buffer.alloc([0x04,0x00,0x00,0x00,0x07,0x50,0xf9,0x6e])

I made a server, I answer the headers.
res.write(buffer, 'binary')
    res.end(null, 'binary')

Here such line does not cause any questions, everything correctly leaves.
But I have a huge package - how do I change the code so that something like 00a0x20 is inserted there? This is immediately a syntax error.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-03-14
@1px

Let's leave the weirdness of what you're doing out of the box - everyone has fun as they want.
If you are just too lazy to fill it with your hands and want to somehow quickly - push it all into a line in the form "02 00 00 00 45 00 00 ae 78 8b 40 00 80 06 00 00" or whatever it is convenient for you to make a copy-paste from the line already create a buffer by parsing it all as you need. Or put the dump in a binary file and read it directly into the buffer as is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question