简体   繁体   中英

Cannot connect to WebSocketServer using IP (but localhost is ok)

This is very straightforward. I have a WebSocketServer project that works great if the uri is http://localhost:8081/ but if I use the local IP instead of localhost http://10.0.0.201:8081/ , it refuses to answer requests on that IP. I am sure its a permissions issue or unable to access the port issue but I am, frankly, not getting any errors. It just simply doesn't respond. Can anyone shed some light on this? This is all in c#.

Again this is a SERVER. It is supposed to LISTEN on 10.0.0.201 (and, of course, localhost). Localhost works, but the IP does not from any client.

I am intentionally not posting code (although I could if necessary) because this is a multi-threaded application that supports multiple concurrent clients. And it works flawlessly through localhost. I am only changing the word "localhost" to the machine's IP. The failure feels like a binding or permissions issue.

This is probably due to Http Binding in your webserver (eg IIS), the binding could be explicitly requiring localhost and if the URI does not have localhost then it would refuse this connection.

Try changing the binding to empty hostname and URI should be allowed.

This is a suggestion, not an answer. Have you tried hitting the WebServer from a different machine? Even if it's just a static test page? It could be a perms problem, but this would at least tell you if any machine on the local network can get to it using the IP/Port configuration.

Try binding to *

If using HttpListener try this:

httpListener = new HttpListener();
httpListener.Prefixes.Add("http://*/");

instead of

httpListener.Prefixes.Add("http://localhost/");
httpListener.Prefixes.Add("http://10.0.0.201/");

Binding to localhost will not bind to all the rest, and binding to a specific IP address will not work with localhost or IPv6 for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM