Nodejs TCP 连接客户端端口分配 [英] Nodejs TCP connection client port assignment

查看:54
本文介绍了Nodejs TCP 连接客户端端口分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 nodejs(net 模块)在客户端和服务器之间创建了 tcp 连接.服务器正在侦听已经预定义的端口,而客户端正在连接到该端口.

I created tcp connection between client and server using nodejs (net module). Server is listening on already predefined port and client is connecting to that port.

据我所知,客户端的端口是由节点动态分配的?那是对的吗?

As far as i understand port for client is dynamically assigned by node? Is that correct?

  1. 使用哪种算法节点为客户端分配随机"端口?这是如何工作的,这是由节点决定的还是由操作系统决定的?
  2. 是否可以定义客户端要使用的静态端口?是否可以定义客户端使用的端口范围?

注意:我想我之前在 stackoverflow 上找到了类似主题的讨论/问题,但我再也找不到了.如果您能分享有关此主题的任何可靠资源,我将不胜感激.

NOTE: I think i found discussion/question with similar subject on stackoverflow before, but i cannot find it anymore. I would apprecaite if you can share any reliable resources regarding this subject.

推荐答案

源端口号通常与您的编程无关,除非您的路由器或防火墙在这方面有某种限制.它仅被底层 TCP 基础设施用于跟踪不同的 TCP 连接.

The source port number is usually pretty much irrelevant to your programming unless you have a router or firewall that is somehow restrictive in that regard. It is merely used by the underlying TCP infrastructure to keep track of different TCP connections.

来自这篇文章:

TCP/IP 连接由四元素元组标识:{source IP,源端口、目的IP、目的端口}.建立 TCP/IP连接只需要目标 IP 和端口号,操作系统自动选择源IP和端口.

A TCP/IP connection is identified by a four element tuple: {source IP, source port, destination IP, destination port}. To establish a TCP/IP connection only a destination IP and port number are needed, the operating system automatically selects source IP and port.

上面引用的文章描述了Linux如何选择源端口号.

The above referenced article describes how Linux selects the source port number.

至于您的特定问题:

使用什么样的算法节点来分配随机"端口给客户?这是如何工作的,这是由节点决定的还是由操作系统决定的?

What kind of algorithm node is using to assign "random" port for the client? How this works, is this determined by node or by OS?

这是由操作系统决定的.在连接到 node.js 之前,源端口号由原始主机在 TCP 级别选择.

It is determined by the OS. That source port number is selected by the originating host at the TCP level before the connection is even made to node.js.

其他一些参考文章:

TCP源端口是否必须每个主机都是唯一的?

应用程序如何使用端口 80/HTTP 不与浏览器冲突?

注意:我知道防火墙没有安全原因来限制源端口号或阻止某些源端口号.它们只是一个 TCP 簿记编号,与安全性或所使用的服务类型完全无关.请注意,这与目标端口不同,目标端口通常与所使用的服务类型直接相关(例如,80 是 HTTP,25 是 SMTP,143 是 IMAP,等等……当您与不同的主机建立 TCP 连接时,指定主机地址和目的端口号.不指定源端口号.

Note: there is no security reason I'm aware of for a firewall to limit the source port number or block certain source port numbers. They are a TCP bookkeeping number only, not related at all to security or the type of service being used. Note, this is different than the destination port which is usually correlated directly with the type of service being used (e.g. 80 is HTTP, 25 is SMTP, 143 is IMAP, etc... When you make a TCP connection to a different host, you specify the host address and the destination port number. You don't specify the source port number.

这篇关于Nodejs TCP 连接客户端端口分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆