16

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

When I supply 127.0.0.1 as 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:

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://192.168.1.3:9300'); No computers can connect. I confirm 192.168.1.3 is the LAN address of the hosting computer.

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

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • Maybe the firewall of the computer which runs the websocket server blocks the ingoing port? – Bernhard Frick Feb 17 '14 at 19:51
  • Some infos would be useful: What OS do you use? What Router/Switch is attached? Any firewall / anti-virus software? Basically connecting via the LAN address should work – Flixer Feb 17 '14 at 21:17
  • @Flixer I use windows 7 and the computers are being connected via a home router. I figure firewall isn't the issue here because as in the second case `Server = new FancyWebSocket('ws://192.168.1.3:9300');` not even the computer `192.168.1.3` can connect to itself, as well as when I access `http://192.168.1.3/` from other computers on the network I am allowed to the home page of the hosting computer `192.168.1.3`, so this leads me to believe it's not a firewall issue. – php_nub_qq Feb 17 '14 at 22:57
  • Could you please share the server side, binding socket code of http as well as websocket. In fact when you bind socket, you can use port and IP address, which will detect on which Network Interface to listen on, and that can lead to problem as you could bind it to non-LAN network interface or even just to system without listening on any network interfaces. So that will lead that no one can access you. As well going to port directly, gives you website, still does not cancel the fact that Firewall can block port 9300. – moka Feb 21 '14 at 11:19
  • @php_nub_qq I edited my answer to handle some of the more recent comments – JSON Feb 24 '14 at 00:35
  • @MaksimsMihejevs I apologize for the delay. Here are the files to my server http://www.4shared.com/rar/7RIos1tuce/PHPWebSocket-Chat-master.html Not actually mine. – php_nub_qq Mar 09 '14 at 11:34

6 Answers6

13

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.1 instead of the network address. After changing that to 192.186.1.3 I 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.

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

000
  • 26,951
  • 10
  • 71
  • 101
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
6

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.

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.

Mehran
  • 15,593
  • 27
  • 122
  • 221
  • The server shouldn't behave differently when using '127.0.0.1' and '192.168.1.3'. Trying to bind() to an address already in use will cause a specific error and will not allow the service to continue loading. By the sound of it the service is loading, but not responding when bound to 192.168.1.3:9300. This would almost certainly be a firewall issue. If the server isn't failing to bind to 192.168.1.3:9300 it should be presumed that it's listening but nothing is arriving. – JSON Feb 23 '14 at 23:59
  • I've experienced the same, if both `127.0.0.1:9300` and `192.168.1.3:9300` are available to listen to and you just bind to the former, you won't be able to receive connections from the later (dah). The stem doesn't mention if it has successfully managed to bind to `192.168.1.3:9300` (just the localhost) and that's why I assumed it's not! – Mehran Feb 24 '14 at 05:24
  • Naturally, if you bind to a port on '127.0.0.1' it wont be bound to '192.168.1.3'. We don't disagree with this point. However, the OP specifically stated that she attempted `Server = new FancyWebSocket('ws://192.168.1.3:9300');` which means she has attempted to implicitly bind to 192.168.1.3:9300. At that point she is no longer listing on 'localhost'. She also stated 'No computers can connect.' rather then 'it crapped out and said I couldn't bind to 192.168.1.3'. Naturally, she wouldn't be attempting to connect other computers to the service if it gave any indication of not starting – JSON Feb 24 '14 at 05:51
  • I have no idea what is `FancyWebSocket` (I googled but couldn't find anything) but `'ws://192.168.1.3:9300'` looks like a client connection string to me not a server bind address. And I suspect it's a she :) – Mehran Feb 24 '14 at 06:37
  • I can't believe I missed this. All I focused on was those ports and firewalls and stuff and I forgot all about the server. I automatically assumed 127.0.0.1 on the host machine would be the same as its current LAN IP, which is apparently not true. Big thanks!!! – php_nub_qq Mar 09 '14 at 10:43
  • Unfortunately I thought the problem was solved but actually now I can only connect from the host itself.. : – php_nub_qq Mar 09 '14 at 11:07
  • Unlike before though, it now takes some time for the other computer to respond instead of instantly saying that it cannot connect. – php_nub_qq Mar 09 '14 at 11:27
  • Could you please be more specific? I'm not sure how you are configuring your server now! I need to know how you are setting up your server (URL/IP to listen to) and the connection URL that you set for your clients. – Mehran Mar 09 '14 at 11:45
4

It looks like a Firewall/Policies issue to me.

Your TCP 80 could 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.

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

vtortola
  • 34,709
  • 29
  • 161
  • 263
  • @vtortola Is it possible to make use of hostname instead of IP with your websocket libraries – Munish Goyal Nov 03 '16 at 08:41
  • The component binds to an IP endpoint, you need IP address and port. You can bind to "all" IP addresses in the machine and validate the domain name during the handshake by checking the "Host" header. – vtortola Nov 03 '16 at 14:19
3

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:9003 you 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.3 is the IP assigned to your interface, this is not a local IP and all communication to and from 192.168.1.3 will 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.

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.

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.

JSON
  • 1,819
  • 20
  • 27
  • Very detailed information, thank you! I checked the resource monitor and it is actively listening on 9300 as well as it is allowed by firewall ( right next to the port under `firewall` it says 'Allowed, not restricted' ). So the last thing left was to check the router, but in the router's firewall settings there is no list or anything that appears to be blocking ports ( which is weird ). I also tried changing the port to 80 and 443 and it didn't work in both cases ( with web service off of course ). Can you give further advice please? Thanks! – php_nub_qq Mar 09 '14 at 10:30
1

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.

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.

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 binding your 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.

selkathguy
  • 1,171
  • 7
  • 17
  • Thanks for the advice. I am in check with your suggestions but still can't connect from another computer. FancyWebSocket is just an object that helps handle the connection and events on the client side, it's nothing fancy, really, just a 40 line script I can post in the question if it's of any difference. – php_nub_qq Mar 09 '14 at 11:31
1

I can help you if its a linux system.

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/hosts file, usually reserved for local network stations.

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.

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.

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

Hope it resolves the issue!

Amar
  • 2,171
  • 1
  • 13
  • 16