0

When i call both of these functions only the upper one fails and returns an error

const { Socket } = require("net")

class Client {
  connect () {
    this.a = new Socket()  
    this.a.connect(this.port, this.host)
    
    this.socket = new Socket()  
    this.socket.connect(this.port, this.host)
  }
}

The problem is, that i know that the port this should connect to, is not used. Both functions should throw an error. If i call the lower one first, still the one with this.a fails.

If i use this.socket for both, the first one always fails even if i change the order of them.

To differentiate between them i used a different port to connect to but also unused.

this.socket = new Socket()
this.socket.connect(6743, this.host)

this.a = new Socket()
this.a.connect(6744, this.host)

The this.port and this.host variables are not the problem, because if run the script while the server on the port is online it works.

Error Message that should be thrown:

events.js:291
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:6744
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6744
}```
Mola 1.9
  • 41
  • 6
  • I don't understand. Both of them are connecting to the same host and port. – Barmar Jun 28 '21 at 23:24
  • 2
    When the first one throws an error, the second one is never executed. – Barmar Jun 28 '21 at 23:25
  • That why i said, that i change the order and the one with this.socket gets executed first then the one with socket.a, which is now below, still fails and not the first one. – Mola 1.9 Jun 28 '21 at 23:28
  • You wrote "the first one always fails". – Barmar Jun 28 '21 at 23:29
  • I meant if i rename this.a to this.socket then the one that is first always fails. With first i don't mean the one which is now first but the one that is first when i rearrange them – Mola 1.9 Jun 28 '21 at 23:29
  • `socket.connect)(` is asynchronous – Barmar Jun 28 '21 at 23:30
  • 1
    It doesn't throw an error, it triggers an `error` event. – Barmar Jun 28 '21 at 23:31
  • How are you detecting which one is getting the error? – Barmar Jun 28 '21 at 23:32
  • But then why does the first one throw an error? – Mola 1.9 Jun 28 '21 at 23:33
  • I changed the port number since it is shown in the error message, but i definitely know that the one i changed it to isn't used either – Mola 1.9 Jun 28 '21 at 23:34
  • @Mola1.9 **Which** error does it throw? Please post the exact error message. – Bergi Jun 28 '21 at 23:36
  • Where do you change the port number? – Barmar Jun 28 '21 at 23:36
  • @Barmar ```this.socket = new Socket() this.socket.connect(6743, this.host) this.a = new Socket() this.a.connect(6744, this.host)``` Both port numbers are unused, still only the lower one throws an error – Mola 1.9 Jun 28 '21 at 23:37
  • @Bergi events.js:291 throw er; // Unhandled 'error' event ^ Error: connect ECONNREFUSED 127.0.0.1:6744 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16) Emitted 'error' event on Socket instance at: at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) at processTicksAndRejections (internal/process/task_queues.js:80:21) { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 6744 } – Mola 1.9 Jun 28 '21 at 23:39
  • I edited the Post so it is more readable – Mola 1.9 Jun 28 '21 at 23:43

2 Answers2

0

Like other users have mentioned, the second error is not being triggered because the application effectively stops running after the first error. To prove this, you could try putting a console.log between your two attempts to connect to a socket and see that won't run as expected either.

Pytth
  • 4,008
  • 24
  • 29
  • Sorry, i explained it badly. The problem is if reverse it (as in the second code example i added) still the one with this.a trows an error, besides this.socket being called before it – Mola 1.9 Jun 28 '21 at 23:53
  • So are you saying if you put the one with `this.socket` first, it doesn't throw an error? – Pytth Jun 29 '21 at 00:00
  • it throws one, but only at this.a, if only run this.socket it doesn't throw an error – Mola 1.9 Jun 29 '21 at 00:01
  • If you are saying running `this.socket` on it's own works, but using `this.a` anywhere doesn't work, without knowing more about your setup, I would be inclined to think there's some tomfoolery happening with variable name collisions. Have you tried changing `this.a` to something like `this.abraKadabra` or something? – Pytth Jun 29 '21 at 00:45
0

In nodeJs some module throw error in event

Socket throw error in event.on('error') and you should checked event

I hope below link help you:

https://stackoverflow.com/a/25847067/6759368

Pooya
  • 2,968
  • 2
  • 12
  • 18