├── .editorconfig ├── .eslintignore ├── .eslintrc.json ├── .gitignore ├── JavaScript ├── 1-tcp-server.js ├── 2-tcp-client.js ├── 3-server.js ├── 4-client.js ├── 5-udp-server.js ├── 6-udp-client.js ├── 7-resolve.js ├── 8-service.js ├── 9-servers.js └── a-lookup.js ├── LICENSE └── README.md /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | end_of_line = lf 6 | charset = utf-8 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | 10 | [{*.js,*.mjs,*.ts,*.json,*.yml}] 11 | indent_size = 2 12 | indent_style = space 13 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es6": true, 5 | "node": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "ecmaVersion": 2020 10 | }, 11 | "globals": { 12 | "BigInt": true 13 | }, 14 | "rules": { 15 | "indent": [ 16 | "error", 17 | 2 18 | ], 19 | "linebreak-style": [ 20 | "error", 21 | "unix" 22 | ], 23 | "quotes": [ 24 | "error", 25 | "single" 26 | ], 27 | "semi": [ 28 | "error", 29 | "always" 30 | ], 31 | "no-loop-func": [ 32 | "error" 33 | ], 34 | "block-spacing": [ 35 | "error", 36 | "always" 37 | ], 38 | "camelcase": [ 39 | "error" 40 | ], 41 | "eqeqeq": [ 42 | "error", 43 | "always" 44 | ], 45 | "strict": [ 46 | "error", 47 | "global" 48 | ], 49 | "brace-style": [ 50 | "error", 51 | "1tbs", 52 | { 53 | "allowSingleLine": true 54 | } 55 | ], 56 | "comma-style": [ 57 | "error", 58 | "last" 59 | ], 60 | "comma-spacing": [ 61 | "error", 62 | { 63 | "before": false, 64 | "after": true 65 | } 66 | ], 67 | "eol-last": [ 68 | "error" 69 | ], 70 | "func-call-spacing": [ 71 | "error", 72 | "never" 73 | ], 74 | "key-spacing": [ 75 | "error", 76 | { 77 | "beforeColon": false, 78 | "afterColon": true, 79 | "mode": "minimum" 80 | } 81 | ], 82 | "keyword-spacing": [ 83 | "error", 84 | { 85 | "before": true, 86 | "after": true, 87 | "overrides": { 88 | "function": { 89 | "after": false 90 | } 91 | } 92 | } 93 | ], 94 | "max-len": [ 95 | "error", 96 | { 97 | "code": 80, 98 | "ignoreUrls": true 99 | } 100 | ], 101 | "max-nested-callbacks": [ 102 | "error", 103 | { 104 | "max": 7 105 | } 106 | ], 107 | "new-cap": [ 108 | "error", 109 | { 110 | "newIsCap": true, 111 | "capIsNew": false, 112 | "properties": true 113 | } 114 | ], 115 | "new-parens": [ 116 | "error" 117 | ], 118 | "no-lonely-if": [ 119 | "error" 120 | ], 121 | "no-trailing-spaces": [ 122 | "error" 123 | ], 124 | "no-unneeded-ternary": [ 125 | "error" 126 | ], 127 | "no-whitespace-before-property": [ 128 | "error" 129 | ], 130 | "object-curly-spacing": [ 131 | "error", 132 | "always" 133 | ], 134 | "operator-assignment": [ 135 | "error", 136 | "always" 137 | ], 138 | "operator-linebreak": [ 139 | "error", 140 | "after" 141 | ], 142 | "semi-spacing": [ 143 | "error", 144 | { 145 | "before": false, 146 | "after": true 147 | } 148 | ], 149 | "space-before-blocks": [ 150 | "error", 151 | "always" 152 | ], 153 | "space-before-function-paren": [ 154 | "error", 155 | { 156 | "anonymous": "never", 157 | "named": "never", 158 | "asyncArrow": "always" 159 | } 160 | ], 161 | "space-in-parens": [ 162 | "error", 163 | "never" 164 | ], 165 | "space-infix-ops": [ 166 | "error" 167 | ], 168 | "space-unary-ops": [ 169 | "error", 170 | { 171 | "words": true, 172 | "nonwords": false, 173 | "overrides": { 174 | "typeof": false 175 | } 176 | } 177 | ], 178 | "no-unreachable": [ 179 | "error" 180 | ], 181 | "no-global-assign": [ 182 | "error" 183 | ], 184 | "no-self-compare": [ 185 | "error" 186 | ], 187 | "no-unmodified-loop-condition": [ 188 | "error" 189 | ], 190 | "no-constant-condition": [ 191 | "error", 192 | { 193 | "checkLoops": false 194 | } 195 | ], 196 | "no-console": [ 197 | "off" 198 | ], 199 | "no-useless-concat": [ 200 | "error" 201 | ], 202 | "no-useless-escape": [ 203 | "error" 204 | ], 205 | "no-shadow-restricted-names": [ 206 | "error" 207 | ], 208 | "no-use-before-define": [ 209 | "error", 210 | { 211 | "functions": false 212 | } 213 | ], 214 | "arrow-parens": [ 215 | "error", 216 | "always" 217 | ], 218 | "arrow-body-style": [ 219 | "error", 220 | "as-needed" 221 | ], 222 | "arrow-spacing": [ 223 | "error" 224 | ], 225 | "no-confusing-arrow": [ 226 | "error", 227 | { 228 | "allowParens": true 229 | } 230 | ], 231 | "no-useless-computed-key": [ 232 | "error" 233 | ], 234 | "no-useless-rename": [ 235 | "error" 236 | ], 237 | "no-var": [ 238 | "error" 239 | ], 240 | "object-shorthand": [ 241 | "error", 242 | "always" 243 | ], 244 | "prefer-arrow-callback": [ 245 | "error" 246 | ], 247 | "prefer-const": [ 248 | "error" 249 | ], 250 | "prefer-numeric-literals": [ 251 | "error" 252 | ], 253 | "prefer-rest-params": [ 254 | "error" 255 | ], 256 | "prefer-spread": [ 257 | "error" 258 | ], 259 | "rest-spread-spacing": [ 260 | "error", 261 | "never" 262 | ], 263 | "template-curly-spacing": [ 264 | "error", 265 | "never" 266 | ] 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /JavaScript/1-tcp-server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const net = require('node:net'); 4 | 5 | const onData = (data) => { 6 | console.log('📨:', data); 7 | }; 8 | 9 | const options = { noDelay: true }; 10 | 11 | const server = net.createServer((socket) => { 12 | console.dir(socket.address()); 13 | socket.write('💗'); 14 | socket.on('data', onData); 15 | socket.on('error', (err) => { 16 | console.log('Socket error', err); 17 | }); 18 | }); 19 | 20 | server.listen(2000, options); 21 | 22 | server.on('error', (err) => { 23 | console.log('Server error', err); 24 | }); 25 | -------------------------------------------------------------------------------- /JavaScript/2-tcp-client.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const net = require('node:net'); 4 | 5 | const socket = new net.Socket(); 6 | 7 | socket.on('data', (data) => { 8 | console.log('📨:', data); 9 | }); 10 | 11 | socket.connect({ 12 | port: 2000, 13 | host: '127.0.0.1', 14 | }, () => { 15 | socket.write('💋'); 16 | }); 17 | 18 | socket.unref(); 19 | -------------------------------------------------------------------------------- /JavaScript/3-server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const net = require('node:net'); 4 | 5 | const connection = (socket) => { 6 | 7 | console.dir({ 8 | localAddress: socket.localAddress, 9 | localPort: socket.localPort, 10 | remoteAddress: socket.remoteAddress, 11 | remoteFamily: socket.remoteFamily, 12 | remotePort: socket.remotePort, 13 | bufferSize: socket.bufferSize, 14 | }); 15 | 16 | socket.write('💗'); 17 | 18 | socket.on('data', (data) => { 19 | console.log('Event: 📨', data); 20 | console.log('Data:', data.toString()); 21 | }); 22 | 23 | socket.on('drain', () => { 24 | console.log('Event: 🤷'); 25 | }); 26 | 27 | socket.on('end', () => { 28 | console.log('Event: 🏁'); 29 | console.dir({ 30 | bytesRead: socket.bytesRead, 31 | bytesWritten: socket.bytesWritten, 32 | }); 33 | }); 34 | 35 | socket.on('error', (err) => { 36 | console.log('Event: 💩'); 37 | console.log(err); 38 | }); 39 | 40 | socket.on('timeout', () => { 41 | console.log('Event: ⌛'); 42 | }); 43 | 44 | }; 45 | 46 | const server = net.createServer(); 47 | 48 | server.on('connection', connection); 49 | 50 | server.listen(2000); 51 | -------------------------------------------------------------------------------- /JavaScript/4-client.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const net = require('node:net'); 4 | 5 | const socket = new net.Socket(); 6 | 7 | const send = (message) => { 8 | console.log('Client >', message); 9 | socket.write(message); 10 | }; 11 | 12 | socket.on('data', (data) => { 13 | console.log('Server >', data.toString(), data); 14 | }); 15 | 16 | socket.on('drain', () => { 17 | console.log('Event: 🤷'); 18 | }); 19 | 20 | socket.on('end', () => { 21 | console.log('Event: 🏁'); 22 | console.dir({ 23 | bytesRead: socket.bytesRead, 24 | bytesWritten: socket.bytesWritten, 25 | }); 26 | }); 27 | 28 | socket.on('error', (err) => { 29 | console.log('Event: 💩'); 30 | console.log(err); 31 | }); 32 | 33 | socket.on('timeout', () => { 34 | console.log('Event: ⌛'); 35 | }); 36 | 37 | socket.on('connect', () => { 38 | send('💋'); 39 | send('💋'); 40 | send('💋'); 41 | }); 42 | 43 | socket.connect({ 44 | port: 2000, 45 | host: '127.0.0.1', 46 | }); 47 | -------------------------------------------------------------------------------- /JavaScript/5-udp-server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const dgram = require('node:dgram'); 4 | 5 | const server = dgram.createSocket('udp4'); 6 | 7 | server.on('message', (msg, rinfo) => { 8 | console.dir({ msg, rinfo }); 9 | }); 10 | 11 | server.bind(3000); 12 | -------------------------------------------------------------------------------- /JavaScript/6-udp-client.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const dgram = require('node:dgram'); 4 | 5 | const message = Buffer.from('Hello'); 6 | const client = dgram.createSocket('udp4'); 7 | 8 | client.send(message, 3000, 'localhost', (err) => { 9 | if (err) { 10 | client.close(); 11 | throw err; 12 | } 13 | }); 14 | -------------------------------------------------------------------------------- /JavaScript/7-resolve.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const dns = require('node:dns'); 4 | 5 | dns.resolve('how.programming.works', (err, data) => { 6 | if (err) { 7 | if (err.code === 'ECONNREFUSED') { 8 | console.log('No internet connection'); 9 | } else { 10 | console.log('Web is dead'); 11 | } 12 | } 13 | console.log({ data }); 14 | }); 15 | 16 | dns.resolveAny('google.com', (err, data) => { 17 | if (err) throw err; 18 | console.log({ data }); 19 | }); 20 | -------------------------------------------------------------------------------- /JavaScript/8-service.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const dns = require('node:dns'); 4 | 5 | dns.lookupService('192.30.253.113', 443, (err, host, service) => { 6 | if (err) throw err; 7 | console.log({ host, service }); 8 | }); 9 | -------------------------------------------------------------------------------- /JavaScript/9-servers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const dns = require('node:dns'); 4 | 5 | const servers = dns.getServers(); 6 | console.log({ servers }); 7 | -------------------------------------------------------------------------------- /JavaScript/a-lookup.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const dns = require('node:dns'); 4 | 5 | const options = { 6 | all: true, 7 | family: 6, 8 | hints: dns.ADDRCONFIG | dns.V4MAPPED, 9 | }; 10 | 11 | dns.lookup('github.com', options, (err, addresses) => { 12 | if (err) throw err; 13 | console.dir({ addresses }); 14 | }); 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2022 How.Programming.Works contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Network Socket Endpoint Interface 2 | 3 | [![Клиент-сервер на Node.js TCP и UDP, DNS ](https://img.youtube.com/vi/bHn-wTlTTR0/0.jpg)](https://www.youtube.com/watch?v=bHn-wTlTTR0) 4 | --------------------------------------------------------------------------------