php 通过局域网 IP 地址连接 websocket 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21410435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:08:19  来源:igfitidea点击:

Connect websocket server by LAN IP address

phpwebsocketlan

提问by php_nub_qq

I have set up a websockets chat with the purpose of learning. Everything is working but I can't figure this issue out.

我已经建立了一个以学习为目的的 websockets 聊天。一切正常,但我无法弄清楚这个问题。

When I supply 127.0.0.1as the address of the connection on the client side then I can access the server from the computer that's hosting it, but when I change the address to the actual LAN address of the hosting computer I can't connect the server even from the host itself. See:

当我127.0.0.1在客户端提供连接地址时,我可以从托管它的计算机访问服务器,但是当我将地址更改为托管计算机的实际 LAN 地址时,我什至无法连接服务器主机本身。看:

Server = new FancyWebSocket('ws://127.0.0.1:9300');Appears to work but only the computer that's hosting the server can connect ( for obvious reasons )

Server = new FancyWebSocket('ws://127.0.0.1:9300');似乎可以工作,但只有托管服务器的计算机才能连接(出于显而易见的原因)

Server = new FancyWebSocket('ws://192.168.1.3:9300');No computers can connect. I confirm 192.168.1.3is the LAN address of the hosting computer.

Server = new FancyWebSocket('ws://192.168.1.3:9300');没有电脑可以连接。我确认192.168.1.3是主机的 LAN 地址。

What address do I need to put in there so that other computers from my local network can connect?

我需要在那里输入什么地址才能连接本地网络中的其他计算机?

采纳答案by php_nub_qq

I solved the problem. Since it was a combination of two answers I thought the only fair thing to do was add another answer with an explanation.

我解决了这个问题。由于它是两个答案的组合,我认为唯一公平的做法是添加另一个带有解释的答案。

As @Mehran suggested, I have had the server address set up as 127.0.0.1instead of the network address. After changing that to 192.186.1.3I was able to connect from the server itself, but other machines were unable to connect. Then I did the steps from the guide provided in @vtortola's answer to add a new inbound rule into the server's firewall in order to allow that port to be used.

正如@Mehran 所建议的,我已经将服务器地址设置为127.0.0.1而不是网络地址。将其更改为192.186.1.3我能够从服务器本身连接后,但其他机器无法连接。然后我按照@vtortola 的回答中提供的指南中的步骤将新的入站规则添加到服务器的防火墙中,以允许使用该端口。

So finally it all works now, thank you very much for helping me. +rep to everyone!

所以现在终于一切正常了,非常感谢你帮助我。+回复大家!

回答by Mehran

I'm pretty sure this is due to the configuration of your WebSocket server. It must be listening to localhost (127.0.0.1) to accept incoming connections in which case it won't answer to those aiming 192.168.1.3.

我很确定这是由于您的 WebSocket 服务器的配置。它必须侦听 localhost( 127.0.0.1) 以接受传入的连接,在这种情况下它不会响应那些瞄准的连接192.168.1.3

Since you didn't mention which server you are using I can not be specific but in general there are two ways to instantiate a listening socket, binding it to a specific IP address or *to bind whatever addresses system has. You need to configure the later if you intend to answer server connections coming from any computer within your LAN.

由于您没有提到您使用的是哪个服务器,我不能具体说明,但通常有两种方法可以实例化侦听套接字,将其绑定到特定 IP 地址或*绑定系统具有的任何地址。如果您打算应答来自 LAN 内任何计算机的服务器连接,则需要配置后者。

回答by JSON

Here are some things that you can safely assume while troubleshooting this issue:

在解决此问题时,您可以安全地假设以下一些事项:

  • If the service is able to work on 127.0.0.1 on the same machine, you can assume that the problem is not in the code or the PHP configuration

  • If you are not receiving an error when the server tries to bind to 192.168.1.3:9003you can safely presume that the service is working. Try opening the Resource Monitor to see if it is actually listening on this port to confirm. To do so, go to 'Start Menu' in Windows and type 'Resource Monitor' in the 'Search programs and files' box. After opening the Resource Monitor, click the 'Overview' tab and find the name of the server process (typically 'php' if your using a CLI). With your process selected, switch over to the 'Network' tab and you will be able to see if it is listening on any ports within the 'TCP Connections' panel. This will show you what address and port it is listing on, as well as the remote address and port of any clients connected to the service.

  • If you know the server is running, and you know that it is actively listening on the expected address and port, it is very likely a firewall issue within Windows or your router. Note that even though 192.168.1.3is the IP assigned to your interface, this is not a local IP and all communication to and from 192.168.1.3will still go through the Windows firewall, including if being sent on the same machine. If your already at this point, I would strongly suggest checking your windows firewall first. If it is not the Windows firewall, check your router to see if it is blocking the port, and also check port forwarding and other setting to make sure that the router isn't otherwise interfering. We can likely help you with router issues here, but have your router's manual handy.

  • 如果服务能在同一台机器上的127.0.0.1上运行,可以假设问题不在代码或PHP配置上

  • 如果您在服务器尝试绑定时没有收到错误消息,192.168.1.3:9003您可以放心地假定该服务正在运行。尝试打开资源监视器以查看它是否确实在此端口上侦听以进行确认。为此,请转到 Windows 中的“开始菜单”并在“搜索程序和文件”框中键入“资源监视器”。打开资源监视器后,单击“概览”选项卡并找到服务器进程的名称(如果您使用 CLI,通常为“php”)。选择您的进程后,切换到“网络”选项卡,您将能够查看它是否正在侦听“TCP 连接”面板中的任何端口。这将显示它列出的地址和端口,以及连接到该服务的任何客户端的远程地址和端口。

  • 如果您知道服务器正在运行,并且您知道它正在积极侦听预期的地址和端口,则很可能是 Windows 或路由器中的防火墙问题。请注意,即使192.168.1.3IP 分配给您的接口,这也不是本地 IP,所有进出的通信192.168.1.3仍将通过 Windows 防火墙,包括在同一台机器上发送的情况。如果您已经在这一点上,我强烈建议您先检查您的 Windows 防火墙。如果不是 Windows 防火墙,请检查您的路由器以查看它是否阻止了端口,并检查端口转发和其他设置以确保路由器不会以其他方式干扰。我们可能会在此处帮助您解决路由器问题,但请准备好您的路由器手册。

HTTP is a common service port so it is very possible that the router is not blocking the port, and windows may have automatically opened it if you are using IIS. 9300 is not a common port so it is unlikely to be open by default under any situation, unless your default is "all in", which effectively means your not using a firewall.

HTTP 是一个常见的服务端口,所以很可能路由器没有阻塞该端口,如果您使用的是 IIS,Windows 可能会自动打开它。9300 不是通用端口,因此在任何情况下都不太可能默认打开,除非您的默认设置是“全部输入”,这实际上意味着您没有使用防火墙。

Another thing you might try (if possible) is closing your existing HTTP service and bind to port 80 using your Websocket service, or if possible (and while exercising caution) turn off your windows firewall completely to see if it works long enough to connect.

您可能会尝试的另一件事(如果可能)是关闭现有的 HTTP 服务并使用您的 Websocket 服务绑定到端口 80,或者如果可能(并在谨慎行事的情况下)完全关闭您的 Windows 防火墙,看看它是否工作足够长以进行连接。

回答by vtortola

It looks like a Firewall/Policies issue to me.

对我来说,这看起来像是防火墙/策略问题。

Your TCP 80could be allowed because the IIS installation will open it, that will explain why normal web browsing works. But you are trying to connect to the TCP 9300, that is very unlikely that is allowed by default.

TCP 80可能会被允许,因为 IIS 安装将打开它,这将解释为什么正常的 Web 浏览工作。但是您正在尝试连接到TCP 9300,这是默认情况下不太可能允许的。

Give a try to this: How to Open a Port in the Windows 7 Firewall, and allow that port.

试试这个:如何在 Windows 7 防火墙中打开一个端口,并允许该端口。

回答by selkathguy

In general, don't try to reach your local network IP address from your own machine. There are very confusing things that happen at the socket layer here that I'll try not to delve too far into. The OS goes out of its way to make this work. Sometimes. I would expect that you cannot reach 192.168.1.3 (the server I'm assuming) from itself. There's a translation between local endpoint addresses when you do that which complicates everything.

通常,不要尝试从您自己的机器访问您的本地网络 IP 地址。这里的套接字层发生了非常令人困惑的事情,我将尽量不深入研究。操作系统不遗余力地完成这项工作。有时。我希望您无法从自身到达 192.168.1.3(我假设的服务器)。当您执行使一切复杂化的操作时,本地端点地址之间会进行转换。

A network switch will typically not send a frame back down a port it just received it from, so what you're seeing when you ping your local IP in cmd prompt is a loopback shortcut the OS is taking.

网络交换机通常不会将帧发送回刚刚从它接收到的端口,因此当您在 cmd 提示符下 ping 本地 IP 时所看到的是操作系统采用的回送快捷方式。

Not being able to reach it from another machine causes me suspect that the socket is not bound correctly on the server. Double check that you are explicitly declaring the socket on the server (address and port), and that your're bindingyour listener to that socket. Also ensure that the address you're binding to is for the correct network adapter. I see this all the time with laptops or machines that have multiple connected adapters.

无法从另一台机器访问它使我怀疑套接字在服务器上没有正确绑定。仔细检查您是否明确声明了服务器上的套接字(地址和端口),并且您将侦听器绑定到该套接字。还要确保您绑定的地址用于正确的网络适配器。我一直在使用具有多个连接适配器的笔记本电脑或机器上看到这一点。

Unfortunately I cannot be more targeted with my response as I am unfamiliar with what a FancyWebSocket is or how it is constructed.

不幸的是,我的回答不能更有针对性,因为我不熟悉 FancyWebSocket 是什么或它是如何构造的。

回答by Amar

I can help you if its a linux system.

如果它是一个 linux 系统,我可以帮助你。

If there is no name server on the local network, it is still possible to establish a small table mapping IP addresses and machine hostnames in the /etc/hostsfile, usually reserved for local network stations.

如果本地网络上没有名称服务器,仍然可以在/etc/hosts文件中建立一个映射 IP 地址和机器主机名的小表,通常为本地网络站保留。

This file is available even during network outages or when DNS servers are unreachable, but will only really be useful when duplicated on all the machines on the network. The slightest alteration in correspondence will require the file to be updated everywhere. This is why /etc/hosts generally only contains the most important entries.

即使在网络中断或 DNS 服务器无法访问时,该文件也可用,但只有在网络上的所有机器上复制时才真正有用。通信中最轻微的更改将需要在任何地方更新文件。这就是为什么 /etc/hosts 通常只包含最重要的条目。

This file will be sufficient for a small network not connected to the Internet, but with 5 machines or more, it is recommended to install a proper DNS server.

这个文件对于没有连接到 Internet 的小型网络来说已经足够了,但是有 5 台或更多的机器,建议安装一个合适的 DNS 服务器。

Try adding all the 'ip:port' along with a hostname and copy the template in file /etc/hosts in all the system.

尝试添加所有 'ip:port' 以及主机名,并将模板复制到所有系统的 /etc/hosts 文件中。

Hope it resolves the issue!

希望它能解决问题!