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 6250203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:22:48+00:00 2026-05-24T13:22:48+00:00

I need to send several commands over telnet to a server. If I try

  • 0

I need to send several commands over telnet to a server. If I try to send them without a time delay between every command, the server freaks out:

var net = require('net');
var conn = net.createConnection(8888, 'localhost');
conn.on('connect', function() {
    conn.write(command_1);
    conn.write(command_2);  
    conn.write(command_3);
    //...
    conn.write(command_n);
})

I guess the server needs some time to respond to command n before I send it command n+1. One way is to write something to the log and fake a “wait”:

var net = require('net');
var conn = net.createConnection(8888, 'localhost');
conn.on('connect', function() {
    console.log('connected to server');
    console.log('I'm about to send command #1');
    conn.write(command_1);
    console.log('I'm about to send command #2');
    conn.write(command_2);  
    console.log('I'm about to send command #3');
    conn.write(command_3);
    //...
    console.log('I'm about to send command #n');
    conn.write(command_n);
})

It might also be the fact that conn.write() is asynchronous, and putting one command after another doesn’t guranty the correct order??

Anyway, what is the correct pattern to assure correct order and enough time between two consecutive commands, for the server to respond?

  • 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-05-24T13:22:49+00:00Added an answer on May 24, 2026 at 1:22 pm

    First things first: if this is truly a telnet server, then you should do something with the telnet handshaking (where terminal options are negotiated between the peers, this is the binary data you can see when opening the socket).

    If you don’t want to get into that (it will depend on your needs), you can ignore the negotiation and go straight to business, but you will have to read this data and ignore it yourself.

    Now, in your code, you’re sending the data as soon as the server accepts the connection. This may be the cause of your troubles. You’re not supposed to “wait” for the response, the response will get to you asynchronously thanks to nodejs 🙂 So you just need to send the commands as soon as you get the “right” response from the server (this is actually useful, because you can see if there were any errors, etc).

    I’ve tried this code (based on yours) against a device I’ve got at hand that has a telnet server. It will do a login and then a logout. See how the events are dispatched according to the sever’s response:

    var net = require('net');
    var conn = net.createConnection(23, '1.1.1.1');
    var commands = [ "logout\n" ];
    var i = 0;
    conn.setEncoding('ascii');
    conn.on('connect', function() {
        conn.on('login', function () {
            conn.write('myUsername\n');
        });
        conn.on('password', function () {
            conn.write('myPassword\n');
        });
        conn.on('prompt', function () {
            conn.write(commands[i]);
            i++;
        });
        conn.on('data', function(data) {
            console.log("got: " + data + "\n");
            if (data.indexOf("login") != -1) {
                conn.emit('login');
            }
            if (data.indexOf("password") != -1) {
                conn.emit('password');
            }
            if (data.indexOf(">#") != -1) {
                conn.emit('prompt');
            }
        });
    });
    

    See how the commands are in an array, where you can iteratively send them (the prompt event will trigger the next command). So the right response from the server is the next prompt. When the server sends (in this case) the string ># another command is sent.

    Hope it helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need send certain attributes(say, human readable user name) from server to client after
I need to send and receive data over serial connections (RS-232 and RS-422). How
I need to send a very specific (non-standard) string to an FTP server: dir
I have several variables that I need to send from page to page... What
I need to send a newsletter to several thousands of subscribers with PHP. The
I need to encrypt bytecode to send over a connection to a webservice, preferably
My program may have several top-level windows open at a time, and I need
I need to send a C struct over the wire (using UDP sockets, and
So in this particular MVVM implementation I'm doing, I need several commands. I really
I need to send an email from a batch script. I tried several solutions

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.