Ivane Gkomarteli
Ivane Gkomarteli

Reputation: 133

Uncaught Error: connect ETIMEDOUT (net.Socket)

I am trying to send text to my network printer via tcp connection.

   function print(buf2){
   var printer = new net.Socket();

   printer.connect(printer_port, printer_name, function() {
     console.log('Connected');
     printer.write(buf2);
     printer.end()
   });
   }

Everything works fine, but after some time my application throws me the error Uncaught Error: connect ETIMEDOUT and it doesn't connect with my printer.

To fix it, I open a browser and navigate to my printers address(192.168.1.111) and then my application connects again but after some time it stops connecting and throws the same error (Uncaught Error: connect ETIMEDOUT).

The applications is written with electron and i use the net npm

   var net = require('net');

In my application every 3 seconds i call a get request and then i call the print method

  function proxy() {
  var client = new HttpClient();
  client.get('my_link', function(response) {
    var jsonItem = JSON.parse(response)
    if(jsonItem.items.length > 0) 
    {
      var text_to_print = jsonItem.items[0].text
      print(text_to_print,text_id);
    }

Any suggestions what could cause this error?

Upvotes: 1

Views: 2174

Answers (1)

christo8989
christo8989

Reputation: 6826

This should help you to debug.

function print(printer_port, printer_name, buf2) {
    var printer = net.createConnection(printer_port, printer_name, function () {
        //'connect' listener
        console.log("Connected!");
        printer.end(buf2);
    });

    printer.setTimeout(60 * 1000); //1 minute

    printer.on("end", function () {
        console.log("Disconnected from server!");
    });

    printer.on("timeout", function () {
        console.log("Timeout!");
        printer.destroy();
    });
}

Upvotes: 1

Related Questions