Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple UDP Client and Server in Node.js ==> ( Echo Server )
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 !!!');
}
});
@rodrigoms2004

This comment has been minimized.

Copy link

@rodrigoms2004 rodrigoms2004 commented Apr 5, 2019

Hi,
I made some changes and I am using it to test TCP and UDP connections for some IoT solutions.

https://github.com/rodrigoms2004/ServerSocketTCP_UDP

@CoolMan119

This comment has been minimized.

Copy link

@CoolMan119 CoolMan119 commented Mar 21, 2020

I love your Gist and it really helped me create my program. I have a quick question though, for the client what does the Buffer.from('siddheshrane") do (line 61), and is it required for the client? Thank You!

@DevJMD

This comment has been minimized.

Copy link

@DevJMD DevJMD commented May 10, 2020

I love your Gist and it really helped me create my program. I have a quick question though, for the client what does the Buffer.from('siddheshrane") do (line 61), and is it required for the client? Thank You!

It's not required, he's simply creating a Buffer from his GitHub username and then sending that data on line 69. You can send whatever you want. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.