So every TCP socket involves two ports; the client port and the server port.  
When you say "send Data over a specific port", that sounds like you want to 
specify the client source port to use in the connection.  For example, connect 
to port 80 on the server and originate packets from my port 12345.  Is that 
correct?

With the BSD socket API you would use the bind(2) call to achieve this which is 
why its called "binding" the socket to a particular source port or source 
address.

Unfortunately, it does not appear that node provides an interface to do this 
with TCP connections.  The docs don't seem to mention anything about binding to 
a specific local address or port.  The code in lib/net.js, however, does have 
an additional localAddress parameter on the connect() function that is not in 
the documentation.  This is passed to the Bind() method in the tcp_wrap.cc C++ 
code.

So it looks like there should at least be a documentation update to note the 
localAddress parameter on connect().  In order to support binding to a 
particular local port a new parameter would need to be added to the 
net.socket.connect() function.

If you're feeling adventurous you could try this completely untested patch to 
node/lib/net.js:

--- a/lib/net.js
+++ b/lib/net.js
@@ -618,7 +618,7 @@ function afterWrite(status, handle, req) {
 }
 
 
-function connect(self, address, port, addressType, localAddress) {
+function connect(self, address, port, addressType, localAddress, localPort) {
   if (port) {
     self.remotePort = port;
   }
@@ -632,9 +632,9 @@ function connect(self, address, port, addressType, 
localAddress) {
   if (localAddress) {
     var r;
     if (addressType == 6) {
-      r = self._handle.bind6(localAddress);
+      r = self._handle.bind6(localAddress, localPort);
     } else {
-      r = self._handle.bind(localAddress);
+      r = self._handle.bind(localAddress, localPort);
     }
 
     if (r) {


Hope that helps.

Ben

On Jun 20, 2012, at 1:46 PM, Fabian Korak wrote:

> My TCP is a little rusty (and not really from protocol terminology, but 
> rather from Posix/C), but I don't think I need to bind() in order to send, 
> but rather connect()? I actually just want to be able to force Node to send 
> Data over a specific port of my choice, instead of just connection to a port 
> of my choice and then sending the data using something from the 
> 5XXXX-port-range.
> 
> For a problem not related to my algorithm, but having the same root: If node 
> always sends TCP-data over 5XXXX but my firewall is set to block any 
> communication or another application uses that port range? Then there would 
> just be no way to manually change it?
> 
> 
> Am Mittwoch, 20. Juni 2012 19:21:50 UTC+2 schrieb Ben Kelly:
> Are you saying you want to bind the source port of the TCP socket? 
> 
> At first glance it does appear that node is lacking an interface to bind a 
> socket to a specific source port or source address. 
> 
> On Jun 20, 2012, at 12:46 PM, Fabian Korak wrote: 
> 
> > Agreed, but for the Echo-algorithm, which identifies nodes using specific 
> > routes (in my example ports) I need to explicitly set the port in order to 
> > fulfil the requirements set for the project. 
> > 
> > In the algorithm the code ends up looking like this: 
> > var nodes = []; 
> > nodes = {//some ports} 
> > 
> > client.on('data',function(data) 
> >     { 
> >     for(var i = 0; i < nodes.length; i++) 
> >             { 
> >                     var sendNode = net.connect(nodes[i],function(){ 
> >                         client.write('info'); 
> >                     }) 
> >                 
> >             } 
> > 
> > Am Mittwoch, 20. Juni 2012 18:39:24 UTC+2 schrieb Nathan Rajlich: 
> > Your server doesn't need to call net.connect() to connect back to the 
> > client. You already have the connection with the "client" object, so you 
> > can just call client.write() directly in the "data" event callback 
> > function. 
> > 
> > On Wed, Jun 20, 2012 at 8:45 AM, Ben Noordhuis  wrote: 
> > On Wed, Jun 20, 2012 at 5:27 PM, Fabian Korak  wrote: 
> > > Hello, sorry for the double-post, google-mail interface decided to post 
> > > before I was finished... 
> > > 
> > > I'm currently working on an implementation of the echo-algorithm in 
> > > Node.js. I currently have code like this(a lot snipped obviously): 
> > > * 
> > > Initial Node: 
> > > *var own_port = 33333; 
> > > var start_port = 22222; 
> > > var sendNode = net.connect(start_port,function(){ 
> > >             sendNode.write('info'); 
> > >         })* 
> > > *//and typical listening code for listening on own_port 
> > > *Recieving Node:* 
> > > var own_port = 22222; 
> > > var back_port; 
> > > 
> > > var nodeServer = net.createServer(); 
> > > nodeServer.on('connection', function(client) 
> > >     { 
> > >     client.on('data',function(data) 
> > >     { 
> > >     back_port = client.remotePort; 
> > >     var sendNode = net.connect(back_port,function(){ 
> > >                         client.write('info'); 
> > >                     }) 
> > > 
> > > } 
> > > 
> > > nodeServer.listen(own_port); 
> > > As some of you now may have noticed is that node does not send on the 
> > > same 
> > > port it listens. Is there any way to specify over which port 
> > > 'client.write()' sends, and if not, any alternatives or plans to add 
> > > that? 
> > 
> > No and no. No offense but your question doesn't make sense in the 
> > context of the TCP protocol. TCP is always connection-oriented. 
> > 
> > 
> > 
> > 
> > -- 
> > Job Board: http://jobs.nodejs.org/ 
> > Posting guidelines: 
> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines 
> > You received this message because you are subscribed to the Google 
> > Groups "nodejs" group. 
> > To post to this group, send email to [email protected] 
> > To unsubscribe from this group, send email to 
> > [email protected] 
> > For more options, visit this group at 
> > http://groups.google.com/group/nodejs?hl=en?hl=en 
> 
> 
> -- 
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: 
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to