Send and receive tcp requests using nodejs

Use nodejs transmission request and receive tcp 
// promise-socket using the synchronization executing 
const} {PromiseSocket = the require ( "promise-socket" ) 

const Client = the async (path, fileName) => { 
  const Socket = new new PromiseSocket (); 
  Socket. the setTimeout ( 1000 ) 
  the await Socket.connect ({Host: IP, Port: Port}); 
  the let Response = the await socket.read (. 1 ) 
  the await socket.write (Buffer.from ([IP.length % 16, IP.length / 16]));
   the await socket.end ();
   return  to true ; 
} 

TCP byte stream is to send and receive data, can be seen through Wireshark
 00 50 99 E8 00 56 is Fe. 3C FD 7C 78 01 08 00 45 00 
00 3B 40 00 36 CC 34 is 06 A4 3D 77 58 15 0A. 2F CE
A9 F4 1D 67 A6 89 A6 CC dd C8 9F E0 F7. 11 FD 80 
00 08 00 6B. 4A 0A 00 01 6E 01 08 67 B1 05 ED . 2F 
6E 7B 
wherein 00 represents one byte (8 bits), 8-bit binary number may represent up to 255 (11111111 ); 
therefore be calculated if a specific number of bytes is the number can be obtained by the n-th power of 256 or modulo quotient;
 12345 
. 1 226 64 
. 1 E2 of 40 

from right to left: the n bits = (n-th power of 12345/256) 256%
 n-start from 0 

examples: the number 1234 2 bytes sent
 . 1 1234 to 16 hex:. 04D2
 2 . 
Buffer.from ([ 1234/256, 1234% 256]); // big endian 
Buffer.from ([1234% 256, 1234/256 ]); // small end
 
computer hardware, there are two data storage by: big-endian byte order (big endian) and little endian (little endian).
For example, the use of two bytes to store the value 0x2211: high byte is 0x22, the low byte is 0x11. 
Big endian: the high byte, the lower byte, the value of which is read-write method humans. 
Little-endian: low byte first, high byte after that is stored in the form of 0x1122.

 

Guess you like

Origin www.cnblogs.com/stellar/p/11545036.html