1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const dgram = require('dgram'); 5 6function pingPongTest(port, host) { 7 8 const server = dgram.createSocket('udp4', common.mustCall((msg, rinfo) => { 9 assert.strictEqual(msg.toString('ascii'), 'PING'); 10 server.send('PONG', 0, 4, rinfo.port, rinfo.address); 11 })); 12 13 server.on('error', function(e) { 14 throw e; 15 }); 16 17 server.on('listening', function() { 18 console.log(`server listening on ${port}`); 19 20 const client = dgram.createSocket('udp4'); 21 22 client.on('message', function(msg) { 23 assert.strictEqual(msg.toString('ascii'), 'PONG'); 24 25 client.close(); 26 server.close(); 27 }); 28 29 client.on('error', function(e) { 30 throw e; 31 }); 32 33 console.log(`Client sending to ${port}`); 34 35 function clientSend() { 36 client.send('PING', 0, 4, port, 'localhost'); 37 } 38 39 clientSend(); 40 }); 41 server.bind(port, host); 42 return server; 43} 44 45const server = pingPongTest(common.PORT, 'localhost'); 46server.on('close', common.mustCall(pingPongTest.bind(undefined, common.PORT))); 47