mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-05-14 23:24:46 +00:00
Upload of pre-existing files
This commit is contained in:
commit
4c21152830
150 changed files with 730703 additions and 0 deletions
26
Node.js/Standard Packages/net.md
Normal file
26
Node.js/Standard Packages/net.md
Normal file
|
@ -0,0 +1,26 @@
|
|||
# Net Module (Connection Oriented Networking - TCP/IP)
|
||||
|
||||
## Server (`net.Server`)
|
||||
|
||||
```js
|
||||
const net = require('net');
|
||||
|
||||
const server = net.createServer((socket) => { /* connect listener */ });
|
||||
|
||||
server.on('error', (err) => {
|
||||
// handle error
|
||||
});
|
||||
|
||||
server.listen(8124, () => { }); // listen for connection
|
||||
```
|
||||
|
||||
## Sockets (`net.Socket`)
|
||||
|
||||
```js
|
||||
|
||||
const client = net.createConnection({ host: "127.0.0.1", port: 8124 }, () => { /* 'connect' listener. */ });
|
||||
|
||||
socket.on("data", (buffer) => { /* operate on input buffer */ });
|
||||
socket.on("error", (err) => { /* handle error */ });
|
||||
socket.on("end", () => {}); // client disconnection
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue