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
}```
15
  • I don't understand. Both of them are connecting to the same host and port. Commented Jun 28, 2021 at 23:24
  • 2
    When the first one throws an error, the second one is never executed. Commented Jun 28, 2021 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. Commented Jun 28, 2021 at 23:28
  • You wrote "the first one always fails". Commented Jun 28, 2021 at 23:29
  • 1
    It doesn't throw an error, it triggers an error event. Commented Jun 28, 2021 at 23:31

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
So are you saying if you put the one with this.socket first, it doesn't throw an error?
it throws one, but only at this.a, if only run this.socket it doesn't throw an error
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?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.