简体   繁体   English

在任意端口上运行的应用程序如何从 Internet 获取数据包?

[英]How does application running on arbitrary port get packets from Internet?

while I was studying Internet Protocols, a question just occurred to me.在我学习互联网协议的时候,我突然想到了一个问题。 Typically, we could assign any ports that are not for typical usage (eg 80 for HTTP, 443 for HTTPS) to our applications.通常,我们可以将任何不用于典型用途的端口(例如,80 用于 HTTP,443 用于 HTTPS)分配给我们的应用程序。 For example, when I use Node.js Express to build a simple server, I could assign port 5000 to this process like below.例如,当我使用Node.js Express构建一个简单的服务器时,我可以将端口5000分配给这个进程,如下所示。

const express = require('express')
const app = express()
const port = 5000

// some code to configure server

app.listen(port, () => {
  console.log(`Server is now running on port ${port}`)
})

My Node.js application will listen to port 5000. If my ip is for example 10.10.10.10 , then my application will get a request if anyone hits 10.10.10.10:5000 .我的Node.js应用程序将监听端口 5000。如果我的 ip 是例如10.10.10.10 ,那么如果有人点击10.10.10.10:5000 ,我的应用程序将收到一个请求。 However, if that's a HTTP/HTTPS request, shouldn't the packets come from port 80 / 443?但是,如果这是一个 HTTP/HTTPS 请求,数据包不应该来自端口 80 / 443 吗? Can someone tell me why it's not the case or why application listening to different ports can receive packets if they indeed come from 80 / 443.有人可以告诉我为什么不是这样,或者为什么侦听不同端口的应用程序可以接收数据包,如果它们确实来自 80 / 443。

Thank you.谢谢你。

When a packet leaves your computer it went through all the layers of the OSI model.当一个数据包离开您的计算机时,它会通过 OSI model 的所有层。 It contains basically six specific information.它基本上包含六个具体信息。

The destination and source IP address (the IP address of the server and your IP address respectively), the destination and source port (the port it is destined to at the server and the port it uses on your machine) and the destination and source MAC address (the MAC address of the machine it is destined to (locally) and the MAC address of your computer).目标和源 IP 地址(服务器的 IP 地址和您的 IP 地址分别),目标和源端口(它在您的机器上使用的端口和源 MAC 端口)和目标服务器地址(目的地机器的 MAC 地址(本地)和计算机的 MAC 地址)。

In a simple configuration (the computer behind a router), when you send this packet, it will be rerouted to the router using it's MAC address.在一个简单的配置中(路由器后面的计算机),当您发送此数据包时,它将使用它的 MAC 地址重新路由到路由器。 The OS keeps a routing table which has the info on what to do with what IP address.操作系统保留一个路由表,其中包含有关如何处理 IP 地址的信息。 Whether it is "On-Link" or if it needs to send the packet to a default gateway.无论是“On-Link”还是需要将数据包发送到默认网关。 You can print the routing table of your computer by typing route print in Windows CMD.您可以通过在 Windows CMD 中键入route print来打印计算机的路由表。 If you are joining an outside server then the packet will be sent to the default gateway.如果您要加入外部服务器,则数据包将被发送到默认网关。 It may need to do an ARP request in order to get the MAC address of the default gateway (or not depending on your computer's ARP table at that moment).它可能需要执行 ARP 请求才能获得默认网关的 MAC 地址(或者不取决于您计算机的 ARP 表)。 You can see the ARP table by typing arp -a on Windows (in CMD).您可以通过在 Windows(在 CMD 中)上键入arp -a来查看 ARP 表。

Once the packet reaches the router, the router strips off the source IP (your internal network IP) and replaces it with the IP of it's external interface (your public IP).一旦数据包到达路由器,路由器就会剥离源 IP(您的内部网络 IP)并将其替换为外部接口(您的公共 IP)的 IP。 It does the link between those two addresses using the NAT table:它使用 NAT 表在这两个地址之间建立链接:

NAT 表示例

It also strips off the internal port and replaces it with a random available port (to the right).它还剥离了内部端口并用随机可用端口(右侧)替换它。 It means that 2 different machines accessing the same website can share the same local port.这意味着访问同一个网站的两台不同的机器可以共享同一个本地端口。 The destination port stays the same.目的端口保持不变。

In the end if you receive a request from outside your router.最后,如果您收到来自路由器外部的请求。 Your router doesn't have a NAT table entry for that packet because it wasn't initiated by you.您的路由器没有该数据包的 NAT 表条目,因为它不是由您发起的。 You'll need to use port forwarding to tell your router to forward incoming packets (destined to a certain port) to a certain internal IP.您需要使用端口转发来告诉您的路由器将传入数据包(发往某个端口)转发到某个内部 IP。

Some routers (like mine) don't support specifying an external port AND an internal port.一些路由器(比如我的)不支持指定外部端口和内部端口。 So both of these are the same (you cannot specify a different external vs internal port so you can't forward external 80 to internal 5000).所以这两者是相同的(你不能指定不同的外部和内部端口,所以你不能将外部 80 转发到内部 5000)。 In your case, you would need to specify an external port of 80/443 and an internal port of 5000 destined to 10.10.10.10 for your configuration to work.在您的情况下,您需要指定 80/443 的外部端口和 5000 的内部端口,以使您的配置正常工作。 Otherwise, it should not work.否则,它不应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将UDP数据包从一个端口转发到另一个端口 - Forward UDP packets from one port to another 将互联网域名指向套接字流应用程序/端口 - Point internet domain name to socketstream application/port 如何让 Nginx 服务器 NodeJS 应用程序在端口 8080 上运行以与 EC2 公共 IPv4 地址的 React 客户端通信 - How to get Nginx server NodeJS application running on port 8080 to talk to React Client at EC2 public IPv4 address 在IP和端口运行的节点应用程序,如何链接到URL - Node application running at IP and port, how to link to URL 如何从 mongoose 连接中获取主机/端口? - How to get host/port from mongoose Connection? 在 amazon linux 的 80 端口上运行 NodeJs 应用程序 - Running NodeJs application on port 80 of amazon linux 为DigitalOcean Droplet上运行的Node.js应用程序定制端口 - Customizing port for nodejs application running on DigitalOcean Droplet 运行 node.js 应用程序的端口错误 - Port Errors running node.js application 如何从端口 3000 上的 Nodejs 后端以及数据重定向到在端口 8080 上运行的 vuejs 前端 - How to redirect to a vuejs front-end running on port 8080 from Nodejs backend on port 3000 along with data 从在其他端口中运行的Vue应用程序调用节点快速服务器API - Calling node express server API from Vue application running in a different port
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM