Using pipe() in Node.js net

前端 未结 4 2121
长发绾君心
长发绾君心 2020-12-12 17:54

I\'m having trouble wrapping my head around the pipe function shown in several Node.js examples for the net module.

var net = require(\'net\');
         


        
4条回答
  •  轮回少年
    2020-12-12 18:35

    There are 2 sockets per Server-Client Connection (2 endpoints). Socket binds IP Address:Port Number. The client gets assigned random port numbers, while the server has dedicated port number. This is the basic explanation of how socket works.

    Piping is reserved for redirecting a readable stream to a writable stream.

    What socket.pipe(socket) does? It redirects all the data from the readable stream (server) to the writable stream (client). We can tweak this by adding event listeners as @hexacyanide has pointed out.

提交回复
热议问题