Reconnecting to tcp socket server after closing does not work on LAN using Nodejs

681 views
Skip to first unread message

Nick Tsutsunava

unread,
Nov 1, 2017, 3:11:50 PM11/1/17
to nodejs
I'm experiencing bizarre behaviour which I can't seem to debug. I have a Electron (NodeJS) application which connects to a remote TCP socket server (also written in NodeJS) on startup. This works fine.

However, once I disconnect and try to reconnect it hangs on the connect method.

this.socket = net.connect(this.port, this.host, () => {
  this.connected = true
  console.log('Connected!')
})

The strange thing is, that I do not get this behaviour when the server runs on 'localhost', this only happens when I run it on a computer on the local network. The remote server doesn't have a firewall (Ubuntu 16.04).

Now, when I restart the Electron application it connects fine again.

Is there some sort of protocol I am missing when destroying a connection which is different for localhost as opposed to local networks?

I close the connection from the client like so

close() {
  this.connected = false
  try {
    this.socket.end()
    this.socket.destroy()
    console.log('Destroying socket connection')
  } catch (e) {
    console.log(e.message)
  }
}

Another test that I did was running ncat -l 30000 instead of my NodeJS server to find out if it was the client or the server causing the trouble.

Turns out it's the client. I can connect to ncat. I can then close the connection causing ncat to exit. If I restart ncat -l 30000 and try to connect to it from the client, it doesn't connect.

Repeating this experiment on the same computer as the client (localhost) I do not experience this behaviour.

Anders Thorsby

unread,
Apr 29, 2018, 5:16:34 PM4/29/18
to nodejs
Hi Nick,

I just found your post, as I'm experiencing the same problem. Client works as expected in NodeJS but in Electron it will not reconnect.
Do you have any new information?

best regards
Anders Thorsby

John Shaver

unread,
Apr 30, 2018, 11:43:06 AM4/30/18
to nod...@googlegroups.com
Hey Anders,


On Sun, Apr 29, 2018, 2:16 PM Anders Thorsby <and...@thorsby.dk> wrote:
Client works as expected in NodeJS but in Electron it will not reconnect.

What version of node is being used in your electron app?

John Shaver


ku...@abvtechnology.com

unread,
Jun 7, 2018, 1:26:41 PM6/7/18
to nodejs
I am experiencing this same issue.

The versions I am using are:

nodejs: v8.11.1
electron: 2.0.2
Reply all
Reply to author
Forward
0 new messages