Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:05:41+00:00 2026-06-14T22:05:41+00:00

I want to create a small ping script. I am a beginner in node

  • 0

I want to create a small ping script. I am a beginner in node js. My ultimate small goal is to have the client ping the server. I want the server to acknowledge the packet by logging the message in the console and I want it to send back the same packet/message back to the client.

This is what I have so far:

Server:

var PORT = 33333;
var HOST = '127.0.0.1';

var dgram = require('dgram');
var server = dgram.createSocket('udp4');

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);
    // I added a server.send but it gave me an infinite loop in the server console

});

server.bind(PORT, HOST);

Client:

var PORT = 33333;
var HOST = '127.0.0.1';

var dgram = require('dgram');
var message = new Buffer('My KungFu is Good!');

var client = dgram.createSocket('udp4');

client.on('message', function (message, remote) {

    console.log("The packet came back");

});


client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT);
    count++;
});

UPDATE:

Thanks! That really helped.
But I have another question.
Let’s say I want to send the packet in a specific number of bytes.
I would replace ‘message.length’ with 1000 for 1kb right?
But I get an error ‘throw new Error(‘Offset + length beyond buffer length’);’

I don’t quite understand why.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:05:41+00:00Added an answer on June 14, 2026 at 10:05 pm

    One thing is sending data and the other thing is receiveing it. Since UDP protocol is bidirectional, then actually there is no strict difference between client and server. So your server and client code will be almost the same, the difference is that actually one will send packets and other will only respond. Also note that you have an infinite loop, because your probably using .send with PORT and HOST variables and you have to send to different host/port pair.

    Here’s an example:

    server

    var host = "127.0.0.1", port = 33333;
    
    var dgram = require( "dgram" );
    
    var server = dgram.createSocket( "udp4" );
    
    server.on( "message", function( msg, rinfo ) {
        console.log( rinfo.address + ':' + rinfo.port + ' - ' + msg );
        server.send( msg, 0, msg.length, rinfo.port, rinfo.address ); // added missing bracket
    });
    server.bind( port, host );
    

    client

    // NOTE: the port is different
    var host = "127.0.0.1", port = 33334;
    
    var dgram = require( "dgram" );
    
    var client = dgram.createSocket( "udp4" );
    
    client.on( "message", function( msg, rinfo ) {
        console.log( "The packet came back" );
    });
    
    // client listens on a port as well in order to receive ping
    client.bind( port, host );
    
    // proper message sending
    // NOTE: the host/port pair points at server
    var message = new Buffer( "My KungFu is Good!" );
    client.send(message, 0, message.length, 33333, "127.0.0.1" );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a small database for my inventory but I have some
If I want to create online phone for small office with web client (through
I have samsung galaxy s2 and I want to create a small app to
I have a directory full of small text files. I want to create a
I have 4 small JPEG images (40px x 30px) and I want create an
i want to create functions what get the folder size. I write small function:
I'm learning to do TDD in practice in small project. I want to create
I want to create a small button with three dots on it, like we
I want to create a small java application in order to communicate with an
I want to create a small document management system. There are several users who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.