1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24 25// Make sure the net module's server doesn't throw an error when handling 26// responses that are either too long or too small (especially on Windows) 27// https://github.com/nodejs/node-v0.x-archive/issues/1697 28 29const net = require('net'); 30const cp = require('child_process'); 31 32if (process.argv[2] === 'server') { 33 // Server 34 35 const server = net.createServer(function(conn) { 36 conn.on('data', function(data) { 37 console.log(`server received ${data.length} bytes`); 38 }); 39 40 conn.on('close', function() { 41 server.close(); 42 }); 43 }); 44 45 server.listen(common.PORT, '127.0.0.1', function() { 46 console.log('Server running.'); 47 }); 48 49} else { 50 // Client 51 52 const serverProcess = cp.spawn(process.execPath, [process.argv[1], 'server']); 53 serverProcess.stdout.pipe(process.stdout); 54 serverProcess.stderr.pipe(process.stdout); 55 56 serverProcess.stdout.once('data', function() { 57 const client = net.createConnection(common.PORT, '127.0.0.1'); 58 client.on('connect', function() { 59 const alot = Buffer.allocUnsafe(1024); 60 const alittle = Buffer.allocUnsafe(1); 61 62 for (let i = 0; i < 100; i++) { 63 client.write(alot); 64 } 65 66 // Block the event loop for 1 second 67 const start = (new Date()).getTime(); 68 while ((new Date()).getTime() < start + 1000) {} 69 70 client.write(alittle); 71 72 client.destroySoon(); 73 }); 74 }); 75} 76