2

I'm opening up a tcp connection to a server which is easy enough to do but I need a way to keep that socket open without having to call net.createConnection(port, host) again and again.

What I'm trying to implement is a socket server which accepts multiple connections then channels the requests through the one socket as mentioned above. I then need to channel the response to the correct socket. However, the only issue I'm having is to maintain an open socket which I'm trying to create outside the listening server code.

I've approached it with the Singleton pattern to create the socket..

var Singleton = (function() {

      var socket = null;

      function connectToHost(port, host) {
          socket = net.createConnection(port, host);
          return socket;
      }

      return {
          connectToHost: connectToHost
      };
})();

But from what I can see, on Event('end') that socket is no longer writable. If I reconnect the socket.

socket.on('end', function() { 
     socket = Singleton.connectToHost(port, host);
});

the same thing will happen on Event('end').

How can I approach this so that I can create and maintain one socket connection?

5
  • I don't understand what you're asking. Sockets are point-to-point connections. You have to have one per connection and the data never mixes. Commented Jun 17, 2011 at 10:44
  • Yes that is correct, but what I'm trying to do is to have multiple sockets connecting to the server then I need to channel the request from each socket to a another service (but here I can only have one socket open). When I get the response from the service then I need to channel that back to the correct socket. This is where I need to map the response to the correct socket! Commented Jun 17, 2011 at 11:00
  • So I'm listing to any socket connecting to my service but I need to channel each request to one socket connection I need to open with another server. This is the bit I'm having trouble with, keeping that one connection to that server open. Incoming traffic is no issue. let me know if you require further info. Thanks for your interest. Commented Jun 17, 2011 at 11:06
  • So I dont want to create that one connection to the server each time I get a request from incoming sockets, if at all possible. The restriction I have is that I can only have one open connection to the server I'm channelling requests too. Commented Jun 17, 2011 at 11:10
  • I was overlooking it - issue resolved.. Commented Jun 17, 2011 at 12:27

2 Answers 2

1

A late response to this.

If I understand your question correctly, are you trying to do something like this?

socket.on('close', function() {
    socket.connect(port, host);
});

According to the net Node.js v.0.12.0 documentation

It might work, but it will hammer the server pretty badly, so a setTimeout might be wise.

I'm curious: what did you end up with in the end?

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

Comments

0

It sounds like you want a mux/demux (multiplexer/demultiplexer) in front of your server which, presumably, replies in such a way as the frontend can properly route the reply.

There's nothing in TCP to support this so you'll have to write it yourself or find one already written. http://www.google.com/search?q=tcp+multiplexer

This link looks promising: http://sourceforge.net/projects/tcpmultiplexer/

(Don't confuse what you're looking for with "tcpmux" on port #1; that's completely different.)

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.