I am working with UDP sockets, I am facing a certain issue that when a code is run for the first time it works, but when it is run for the second time it gives me this error:
at _handle.lookup (dgram.js:266:18)
at _combinedTickCallback (internal/process/next_tick.js:142:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
I deduced that this error is issued because the port is still in use, so I am trying to work on a sample code that checks if a socket is running on a certain port, if yes close it and then create the socket again on the same port.
Here is the sample code:
var PORT = 7777;
var HOST = '10.0.1.10';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
gener(server, PORT, HOST);
function gener(sock, prt, hst){
sock.close();
sock.bind(prt, hst);
}
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
server.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
});
When I run it, it gives me the following error:
dgram.js:638
throw new errors.Error('ERR_SOCKET_DGRAM_NOT_RUNNING');
^
Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running
at Socket._healthCheck (dgram.js:638:11)
at Socket.bind (dgram.js:186:8)
at gener (/home/caracall/Desktop/server.js:11:18)
at Object.<anonymous> (/home/caracall/Desktop/server.js:7:1)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)