2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-07-05 22:38:13 +02:00
|
|
|
|
2013-10-28 22:58:37 +01:00
|
|
|
var fs = require('fs'),
|
2010-12-05 20:15:30 +01:00
|
|
|
dgram = require('dgram'), server, client,
|
2014-12-17 14:30:04 +01:00
|
|
|
server_port = common.PORT,
|
2013-10-28 22:58:37 +01:00
|
|
|
message_to_send = 'A message to send',
|
2010-07-05 22:38:13 +02:00
|
|
|
timer;
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
server = dgram.createSocket('udp4');
|
|
|
|
server.on('message', function(msg, rinfo) {
|
|
|
|
console.log('server got: ' + msg +
|
|
|
|
' from ' + rinfo.address + ':' + rinfo.port);
|
|
|
|
assert.strictEqual(rinfo.address, '127.0.0.1');
|
2010-07-05 22:38:13 +02:00
|
|
|
assert.strictEqual(msg.toString(), message_to_send.toString());
|
|
|
|
server.send(msg, 0, msg.length, rinfo.port, rinfo.address);
|
|
|
|
});
|
2010-12-05 20:15:30 +01:00
|
|
|
server.on('listening', function() {
|
2010-07-05 22:38:13 +02:00
|
|
|
var address = server.address();
|
2010-12-05 20:15:30 +01:00
|
|
|
console.log('server is listening on ' + address.address + ':' + address.port);
|
|
|
|
client = dgram.createSocket('udp4');
|
|
|
|
client.on('message', function(msg, rinfo) {
|
|
|
|
console.log('client got: ' + msg +
|
|
|
|
' from ' + rinfo.address + ':' + address.port);
|
|
|
|
assert.strictEqual(rinfo.address, '127.0.0.1');
|
2010-07-05 22:38:13 +02:00
|
|
|
assert.strictEqual(rinfo.port, server_port);
|
|
|
|
assert.strictEqual(msg.toString(), message_to_send.toString());
|
|
|
|
client.close();
|
|
|
|
server.close();
|
|
|
|
});
|
2010-12-05 20:15:30 +01:00
|
|
|
client.send(message_to_send, 0, message_to_send.length,
|
2013-06-27 00:43:23 +02:00
|
|
|
server_port, 'localhost', function(err) {
|
2010-12-05 20:15:30 +01:00
|
|
|
if (err) {
|
|
|
|
console.log('Caught error in client send.');
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
client.on('close',
|
|
|
|
function() {
|
|
|
|
if (server.fd === null) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
});
|
2010-07-05 22:38:13 +02:00
|
|
|
});
|
2010-12-05 20:15:30 +01:00
|
|
|
server.on('close', function() {
|
2010-07-05 22:38:13 +02:00
|
|
|
if (client.fd === null) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
server.bind(server_port);
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
timer = setTimeout(function() {
|
|
|
|
throw new Error('Timeout');
|
2010-07-05 22:38:13 +02:00
|
|
|
}, 200);
|