区分同一 Intranet 上的两台计算机

发布于 2024-08-20 21:25:43 字数 185 浏览 8 评论 0原文

我正在尝试创建一个套接字“Hello World”程序。

理想情况下,我想创建两个可以点对点交换简单文本的程序。

我可以创建一个 Web 服务,同行可以将其 IP 地址发布到其中。

然后,一个对等点可以选择另一个进行配对。

如果其中一台是我的台式电脑,另一台是我的笔记本电脑,我如何区分两者?

I am trying to create a Socket "Hello World" program.

I would ideally like to create two programs who can exchange simple text, peer-to-peer.

I can create a webservice that the peers can post their IP Address to.

One peer can then select another to pair up with.

If one of the peers is my desktop computer, and the other is my laptop, how can I differentiate the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱你不解释 2024-08-27 21:25:43

首先。如果所有这些都在您的本地网络上,没有任何 NAT 网关或类似的 - 这没什么大不了的。您的服务器将看到具有不同 IP 地址的客户端,并且客户端可以直接相互连接。

另一方面,如果您的服务器在互联网可访问的 IP 地址上运行,并且您的客户端从任何可以访问该服务器的地方进行连接,您将不得不考虑本文的其余部分。

客户端 A 连接到您的服务器。现在之间存在联系:
(ClientAIP:ClientAPort,ServerIP:ServerPort)

客户端 B 连接到您的服务器。现在之间存在联系:
(ClientBIP:ClientBPort,ServerIP:ServerPort)

如果您的两个客户端都位于同一个 NAT 后面gateway,服务器将看到 ClientAIP == ClientBIP。
但ClientAPort 与ClientBPort 会有所不同。这足以保持与服务器的两个连接。

现在,如果你想将其中两个配对,麻烦就来了。让客户端向您发送其内部 IP 地址(也许还有其内部端口,以便可用于额外验证。)

您现在知道客户端(外部)IP 及其内部 IP。
这些情况将是:

  1. ClientAInternalIP == ClientAIP、ClientBinternalIP == ClientBIP。两者都位于 NAT 网关后面 - 选择其中一个作为服务器,另一个直接连接到它。

  2. ClientAInternalIP != ClientAIP, ClientBinternalIP == ClientBIP。 ClientA 位于 NAT 之后,ClientB 则不然。让 ClientB 成为服务器,并且 ClientA 直接连接到 ClientB

  3. ClientAInternalIP == ClientAIP, ClientBinternalIP != ClientBIP。正好相反的情况 2)。

  4. ClientAInternalIP != ClientAIP,ClientBinternalIP != ClientBIP,但 ClientAIP == ClientBIP 。特殊情况 - 它们都位于 NAT 网关后面,但位于同一内部网络上。这与情况 1) 相同。除了客户端可以在其内部网络而不是互联网上进行通信之外。

  5. ClientAInternalIP != ClientAIP,ClientBinternalIP != ClientBIP。两者都位于 NAT 网关后面。 - 您无法相互连接 - 至少不能要求某人在客户端的网关之一上设置端口转发。您必须通过服务器路由通信。 (尽管有提供 API 的路由器/网关,您也许可以使用它以编程方式从客户端站点打开端口转发。它并不广泛可用,并且可能已被锁定)。

即使在上述情况下,也可能会设置防火墙,因此即使客户端的真实 IP 地址可在互联网上路由,客户端也无法直接对等 - 您可能需要回退到通过服务器进行路由。

First of all. If all this is on your local network, without any NAT gateways or similar - it's no big deal. Your server will see your clients with different IP addresses, and the clients can connect directly to each other.

On the other hand, if your server runs on an internet reachable IP address, and your clients connect from wherever they can reach that server you will have to consider the rest of this post.

Client A connects to your server. There's now a connection between:
(ClientAIP:ClientAPort,ServerIP:ServerPort)

Client B connects to to your server. There's now a connection between:
(ClientBIP:ClientBPort,ServerIP:ServerPort)

If both your clients are behind the same NAT'ed gatway, the server will see ClientAIP == ClientBIP.
But ClientAPort will be different from ClientBPort. And that's enough to to keeep up two connections to your server.

Now the trouble comes if you want to pair two of them. Have the clients send you its internal IP address(And perhaps its internal port too so that could be used for extra verification.)

You now know the clients (external) IP and their internal IP.
These will be the scenarios:

  1. ClientAInternalIP == ClientAIP, ClientBinternalIP == ClientBIP. Great neither are behind a NAT gateway - select one of them to be a server and the other one to connect directly to it.

  2. ClientAInternalIP != ClientAIP, ClientBinternalIP == ClientBIP. ClientA is behind NAT, ClientB is not. Have ClientB be a server and ClientA connect diectly to ClientB

  3. ClientAInternalIP == ClientAIP, ClientBinternalIP != ClientBIP. Just the reverse case 2).

  4. ClientAInternalIP != ClientAIP, ClientBinternalIP != ClientBIP, but ClientAIP == ClientBIP . Special case - they're both behind a NAT gateway but they're on the same internal network. This is the same as case 1). Except the clients can communicate on their internal network instead of the internet.

  5. ClientAInternalIP != ClientAIP, ClientBinternalIP != ClientBIP. Both are behind NAT gatways. -You can't have the connect to eachother - atleast not without asking someone to set up port forwarding on one of the gateways at the client. You'll have to route the communication through your server. (Though there are routers/gateways providing APIs you might be able to use to programatically open port forwarding from a client site. It's not widely available and probably locked down anyways).

And even with the above scenarios there might be firewalls set up so clients can't peer directly even if their real IP address is routable on the internet - you might need to fall back to routing through your server anyway.

时光沙漏 2024-08-27 21:25:43

通常不是您发起请求,而是客户发起。您收到请求并做出响应。假设您的请求包含类似“对话标识符”之类的内容,并且您将所有讲话者与同一讲话者进行匹配。

You don't usually initiate the requests, the clients do. You get a request and respond to it. Presumably your request contains something like a "conversation Identifier" and you match up all the talkers with the same one.

小红帽 2024-08-27 21:25:43

在每一个上运行 ipconfig 并找出它们的 ip。您也许还可以根据名称对它们执行 ping 操作,具体取决于它们的连接方式。

要识别网络外部的任何一台计算机,您需要向互联网提供商付费,以便他们为您分配一系列公共 IP,然后您将每台计算机公开为拥有这些地址。您家里的地址范围可能是 192.168.1.x,这是私有的。

Run ipconfig on each one of them and find out what is their ip. You might also be able to ping them by name depending on how they are connected.

To identify either machine outside your network you need to pay money to your internet provider so they allocate a range of public IPs for you and then you expose each one as having those addresses. What you have at your home is probably a 192.168.1.x address range which is private.

固执像三岁 2024-08-27 21:25:43

我会在路由器中设置端口转发,这样您在外部 IP 中连接到的任何端口都会转发到内部网络中的正确计算机。

I would set up port forwarding in the router so whatever port you connect to in the external IP will forward to the right computer in the internal network.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文