Created
July 25, 2016 08:39
-
Star
(126)
You must be signed in to star a gist -
Fork
(26)
You must be signed in to fork a gist
-
-
Save sid24rane/6e6698e93360f2694e310dd347a2e2eb to your computer and use it in GitHub Desktop.
Simple UDP Client and Server in Node.js ==> ( Echo Server )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var udp = require('dgram'); | |
// --------------------creating a udp server -------------------- | |
// creating a udp server | |
var server = udp.createSocket('udp4'); | |
// emits when any error occurs | |
server.on('error',function(error){ | |
console.log('Error: ' + error); | |
server.close(); | |
}); | |
// emits on new datagram msg | |
server.on('message',function(msg,info){ | |
console.log('Data received from client : ' + msg.toString()); | |
console.log('Received %d bytes from %s:%d\n',msg.length, info.address, info.port); | |
//sending msg | |
server.send(msg,info.port,'localhost',function(error){ | |
if(error){ | |
client.close(); | |
}else{ | |
console.log('Data sent !!!'); | |
} | |
}); | |
}); | |
//emits when socket is ready and listening for datagram msgs | |
server.on('listening',function(){ | |
var address = server.address(); | |
var port = address.port; | |
var family = address.family; | |
var ipaddr = address.address; | |
console.log('Server is listening at port' + port); | |
console.log('Server ip :' + ipaddr); | |
console.log('Server is IP4/IP6 : ' + family); | |
}); | |
//emits after the socket is closed using socket.close(); | |
server.on('close',function(){ | |
console.log('Socket is closed !'); | |
}); | |
server.bind(2222); | |
setTimeout(function(){ | |
server.close(); | |
},8000); | |
// -------------------- udp client ---------------- | |
var buffer = require('buffer'); | |
// creating a client socket | |
var client = udp.createSocket('udp4'); | |
//buffer msg | |
var data = Buffer.from('siddheshrane'); | |
client.on('message',function(msg,info){ | |
console.log('Data received from server : ' + msg.toString()); | |
console.log('Received %d bytes from %s:%d\n',msg.length, info.address, info.port); | |
}); | |
//sending msg | |
client.send(data,2222,'localhost',function(error){ | |
if(error){ | |
client.close(); | |
}else{ | |
console.log('Data sent !!!'); | |
} | |
}); | |
var data1 = Buffer.from('hello'); | |
var data2 = Buffer.from('world'); | |
//sending multiple msg | |
client.send([data1,data2],2222,'localhost',function(error){ | |
if(error){ | |
client.close(); | |
}else{ | |
console.log('Data sent !!!'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to get my home PC's public IP(behind NAT) by modified these codes a little:
On the server side, print client's IP in the "onMessage()", and on the client side, send UDP datagram by nc command (echo -n 'x' | nc -4uw0 $server $port).
It works fine from my notebook, but not work from the home PC.
I run tcpdump on the server, it can captured all datagrams, but the datagrams from home PC can't reach "onMessage()", why?
To simplify the case, I run nc on the server side, found out that the first message from home PC alway lost in server's stdout.

However, nc works fine from the notebook.

The server and home PC' OS is ubuntu 22.04. The notebook is Mac OS.
Thanks.