1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| const net = require('net');
const PORT = 7000; const HOST = '0.0.0.0'; const LF = '\n'.charCodeAt(0); const CR = '\r'.charCodeAt(0); const MAX_LENGTH = 128; const STRIP_DELIMITER = false;
function findEndOfLine(buffer) { let i = buffer.indexOf(LF); if (i > 0 && buffer[i - 1] === CR) { i--; } return i; }
function fail(socket, length) { if (socket.writable) { socket.end(`frame length ${length} exceeds the allowed maximum ${MAX_LENGTH}`); } else { socket.end(); } }
const server = net.createServer();
server.maxConnections = 10240;
server.on('connection', (socket) => { console.log(`新的客户端加入:${socket.remoteAddress}:${socket.remotePort}`);
socket.setTimeout(5000); socket.on('timeout', () => { console.log('超时断开'); socket.end(); });
let readBuffer = Buffer.alloc(0); socket.on('data', (data) => { readBuffer = Buffer.concat([readBuffer, data]); const eol = findEndOfLine(readBuffer);
if (eol >= 0) { let frame; const delimLength = readBuffer[eol] === CR ? 2 : 1; const length = eol + 1; if (length > MAX_LENGTH) { fail(socket, length); return; }
if (STRIP_DELIMITER) { frame = readBuffer.slice(0, eol); } else { frame = readBuffer.slice(0, eol + delimLength); } readBuffer = readBuffer.slice(eol + delimLength + 1);
if (socket.writable) socket.write(frame); } else { const length = readBuffer.length; if (length > MAX_LENGTH) { fail(socket, length); return; } } });
socket.on('error', (err) => { console.error(`客户端错误:${err.name}\n${err.stack}`); });
socket.on('close', () => { console.log(`客户端断开:${socket.remoteAddress}:${socket.remotePort}`); });
socket.on('end', () => { console.log('FIN'); }); });
server.on('error', (err) => { console.error(`服务器错误:${err.name}\n${err.stack}`); });
server.on('close', () => { console.log('服务停止'); });
server.on('listening', () => { console.log(`Server listen on ${HOST}:${PORT}`); });
server.listen(PORT, HOST);
setInterval(() => { server.getConnections((err, connections) => { if (!err) { const now = (new Date()).toUTCString(); const memoryUsage = JSON.stringify(process.memoryUsage()); console.log(`[${now}] 连接数为:${connections},最大连接数为:${server.maxConnections},内存使用:${memoryUsage}`); } }); }, 2000);
process.on('uncaughtException', (err) => { console.error(`uncaughtException:${err.name}\n${err.stack}`); process.exit(1); });
|