11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst dgram = require('dgram'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cifunction pingPongTest(port, host) { 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci const server = dgram.createSocket('udp4', common.mustCall((msg, rinfo) => { 91cb0ef41Sopenharmony_ci assert.strictEqual(msg.toString('ascii'), 'PING'); 101cb0ef41Sopenharmony_ci server.send('PONG', 0, 4, rinfo.port, rinfo.address); 111cb0ef41Sopenharmony_ci })); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci server.on('error', function(e) { 141cb0ef41Sopenharmony_ci throw e; 151cb0ef41Sopenharmony_ci }); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci server.on('listening', function() { 181cb0ef41Sopenharmony_ci console.log(`server listening on ${port}`); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci const client = dgram.createSocket('udp4'); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci client.on('message', function(msg) { 231cb0ef41Sopenharmony_ci assert.strictEqual(msg.toString('ascii'), 'PONG'); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci client.close(); 261cb0ef41Sopenharmony_ci server.close(); 271cb0ef41Sopenharmony_ci }); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci client.on('error', function(e) { 301cb0ef41Sopenharmony_ci throw e; 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci console.log(`Client sending to ${port}`); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci function clientSend() { 361cb0ef41Sopenharmony_ci client.send('PING', 0, 4, port, 'localhost'); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci clientSend(); 401cb0ef41Sopenharmony_ci }); 411cb0ef41Sopenharmony_ci server.bind(port, host); 421cb0ef41Sopenharmony_ci return server; 431cb0ef41Sopenharmony_ci} 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ciconst server = pingPongTest(common.PORT, 'localhost'); 461cb0ef41Sopenharmony_ciserver.on('close', common.mustCall(pingPongTest.bind(undefined, common.PORT))); 47