• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const dgram = require('dgram');
6
7const client = dgram.createSocket('udp4');
8const server = dgram.createSocket('udp4');
9
10const toSend = [Buffer.alloc(256, 'x'),
11                Buffer.alloc(256, 'y'),
12                Buffer.alloc(256, 'z'),
13                'hello'];
14
15const received = [];
16
17server.on('listening', common.mustCall(() => {
18  const port = server.address().port;
19  client.connect(port, (err) => {
20    assert.ifError(err);
21    client.send(toSend[0], 0, toSend[0].length);
22    client.send(toSend[1]);
23    client.send([toSend[2]]);
24    client.send(toSend[3], 0, toSend[3].length);
25
26    client.send(new Uint8Array(toSend[0]), 0, toSend[0].length);
27    client.send(new Uint8Array(toSend[1]));
28    client.send([new Uint8Array(toSend[2])]);
29    client.send(new Uint8Array(Buffer.from(toSend[3])),
30                0, toSend[3].length);
31  });
32}));
33
34server.on('message', common.mustCall((buf, info) => {
35  received.push(buf.toString());
36
37  if (received.length === toSend.length * 2) {
38    // The replies may arrive out of order -> sort them before checking.
39    received.sort();
40
41    const expected = toSend.concat(toSend).map(String).sort();
42    assert.deepStrictEqual(received, expected);
43    client.close();
44    server.close();
45  }
46}, toSend.length * 2));
47
48server.bind(0);
49