const dgram = require('dgram');
// Server
const server = dgram.createSocket('udp4');
server.on('listening', function () {
console.log('Server listening: ' + JSON.stringify(server.address()));
});
server.on('message', function (message, rinfo) {
console.log('Server got message: "' + message + '" from: ' + rinfo.address + ':' + rinfo.port);
server.send(message, 0, rinfo.size, rinfo.port, rinfo.address, function (error) {
if (error) {throw error;}
server.close();
});
});
server.on('close', function () {
console.log('Server closed');
});
server.on('error', function (error) {
console.log('Server error: ' + error.stack);
server.close();
});
server.bind(8080, '127.0.0.1', function () {
console.log('Datagram server started at 127.0.0.1:8080');
});
// Client
const client = dgram.createSocket('udp4');
client.on('message', function (message, rinfo) {
console.log('Client got message: "' + message + '" from: ' + rinfo.address + ':' + rinfo.port);
client.close();
});
client.on('close', function () {
console.log('Client closed');
});
client.on('error', function (error) {
console.log('Client error: ' + error.stack);
client.close();
});
client.send([Buffer.from('Some bytes.'), Buffer.from(' And another bytes.')], 8080, '127.0.0.1', function (error) {
if (error) {throw error;}
});
Комментариев нет:
Отправить комментарий