SignalR on Home Network - c#

Edit/Summary
After a lot of trial and error, Paul Kearney - pk helped lead me to the answer, although I still don't know why this happens, at least I know how to get it to work.
Quick Summary: Clients can connect to port 8080 on my laptop when I'm directly connected to my network at work. Clients cannot connect to port 8080 when I'm on a home network. To solve this, I created a firewall rule (on my laptop) to allow inbound traffic on 8080. I'd really like to know why this is the case. Does my laptop's Windows Firewall service actually change its settings based on the network I'm connected to?
Note: This all works when I'm on my network at work, but it doesn't work on my home network, or someone else's home network. The host computer (my laptop) is the same at both locations.
I have a web app that uses SignalR. Everything works when I run the web app on the same machine as where the SignalR host is running. When I try to connect from a different machine, I get this error:
> GET http://10.0.0.13:8080/signalr/hubs net::ERR_CONNECTION_TIMED_OUT. Cannot read property 'client' of undefined.
That error comes from my index.html page:
<script src="http://10.0.0.13:8080/signalr/hubs"></script>
From the research that I've done, I know that I shouldn't be using localhost in my SignalR URL. So I used an asterisk. This is in my self-hosted SignalR app:
class Program
{
static void Main(string[] args)
{
string url = "http://*:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class RaceHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
And this is my JavaScript to connect:
var signalRUrl = 'http://10.0.0.13:8080/signalr';
$.connection.hub.url = signalRUrl;
var hub = $.connection.raceHub; // proxy reference to hub
// Create a function that the hub can call to broadcast messages.
hub.client.addMessage = function (name, message) {
receiveSignalRMessage(name, message);
};
$.connection.hub.start(); // Start the connection.
Like I said, this all works locally. But it doesn't work from another machine. I do not have Windows Firewall enabled, so that's not the issue. Any ideas?
HTTPS didn't work. It works when I'm on my network at work, but not at home. The configuration is the same. Must have something to do with my home network.
Also, this is what the page looks like when it's working properly:
And this is what happens when it's not working:

(Full disclosure - #BobHorn typed up this answer based on my comments)
There are different firewall settings on your machine, depending on the type of network you are attached to.
Have you ever noticed when you connect to a new network for the very first time, it asks you to define it as "work, private, public, home, etc"?
Whatever you choose for that network is sticky, and the related firewall policy is applied when you are on that type of network.
To check this, you can either completely turn off your firewall, or make sure inbound port 8080 is open for all firewall profiles, especially "home" networks and/or the "home" firewall profile.
ahh.. so you say your windows firewall is off. Are you sure it's not OFF only for the WORK network profile, but enabled for the HOME network profile ?
To troubleshoot this, I would put wireshark on one of the client computers trying to connect to port 8080 at home, and see how/why it's being blocked or cannot connect.
You can also try to telnet to port 8080 from the client machine at home (if the Telnet Client feature is enabled). You'll know right away if the port is open and connection succeeds, or a big fat denied. Quick and easy test. type TELNET ServerIPAddress PortNumber from a command line. If Telnet command not found, you need to turn on the Telnet Client in Windows Features/Components.

Does your network use IP security? You might try using HTTPS instead to see if that alleviates anything.

It looks like your work network is blocking your IP. Try whitelisting your home IP on your work network.

I believe the issue here is that your laptop's IP address changes from one network (i.e. work) to another (i.e. home), but you may not be updating the value of the signalRUrl variable in your javascript file accordingly, thus the client side of your app is looking for a hub at an IP address that doesn't exist on your home network.
When you change networks, you need to update the value of signalRUrl in javascript to reflect the current IP address of the host machine.

Related

'No connection could be made because the target machine actively refused it'

I am working on a 'Smart Device Project' using .Net Framework 3.5. I am trying to connect to some Java SOAP services on a remote server.
In order to do that, I added 'Web References' to my project.
When I try to call my web service I get a WebException 'Unable to connect to the remote server' with the inner exception being 'No connection could be made because the target machine actively refused it'.
I searched quite a lot on the Web and StackOverflow and found a lot of ASP configuration and 'Unavaliable port' answers, but as I have another application using the exact same Service successfully, I can't get why the new one isn't getting through (It did sometimes through my tests so I suppose my client implementation isn't that bad)
I tried to look if there was some connection issue on the port by using some TcpClient:
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
try
{
client.Connect("myServerName", 8087);
MessageBox.Show("Success");
} catch (Exception ex)
{
MessageBox.Show("Failure");
}
finally
{
client.Close();
}
This connection succeed.
Here is a sample on how I call my WebService:
WSServiceExtended srv = new WSServiceExtended();
srv.Proxy = new System.Net.WebProxy();
ServeurWSI wsi = new ServeurWSI();
srv.Url = "http://myServerName:8087/myServerApp/services/myService";
wsr = srv.login(wsi);
The service is called 'Extended' because I overrided the auto-generated one in order to add Cookie managment since I am using the Compact Framework. Following the sample in this thread:
https://social.msdn.microsoft.com/Forums/en-US/34d88228-0b68-4fda-a8cd-58efe6b47958/no-cookies-sessionstate-in-compact-framework?forum=vssmartdevicesvbcs
EDIT:
I made some new tests with the Web references and got it to work.
When I add the Web Reference, I have to put some Url to the Web Service. When I set it with the actual hostname instead of the 'localhost' everything is fine.
But then, since I set it manually to the real address just before the call, it shouldn't matter
srv.Url = "http://myServerName:8087/myServerApp/services/myService";
EDIT2:
I might have forgotten some specifics about my environnement.
The Web Services are exposed on my computer on some Tomcat Server.
The application I am working on is also developped on this computer (That's why I can add Web References by putting 'localhost' in the address)
The application is then deployed on a distant device (Windows CE) that will make calls the Web Services through WIFI (There, localhost wouldn't work then)
I tried calling the Web services from other computers successfully.
I'm beginning to think that there might be some differential between the called Url and the one that is set, otherwise, how would I have a difference in behaviour such as the one described in the first edit?
EDIT3:
Well..Seems like it's not a network issue but a .Net compact framework (usage?) issue...
The Url property of the Web Service implementation is simply ignored and the one in the Reference.cs is used in place.
If someone had some idea on how I could troubleshot this, I would really appreciate it.
That error means that you reached a server and the server said "no way". So you're either hitting the wrong server or the wrong port.
I find the telnet client is useful for testing stuff like this. From the command line, you can do:
telnet [servername] [port]
So something like:
telnet myServerName 8087
If it goes to a blank screen, then it connected successfully. If it does not connect, it'll tell you.
The telnet client is no longer installed by default in Windows 7+, so you'll have to install it. See here for instructions: https://technet.microsoft.com/en-ca/library/cc771275
If the connection does open, you could paste in an actual HTTP request to see what happens. A simple GET would look something like this:
GET /myServerApp/services/myService HTTP/1.1
Host: myServerName:8087
One reason for this error can be that the service binds to only a certain IP address. It could well be that the service only listens on the IP that is assigned to the host name, but not on the localhost IP (127.0.0.1).
For example:
If the host myServerName has the public IP 192.168.0.1, your service can choose to listen on all IPs assigned to the host (sometimes specifying 0.0.0.0), or it can specifically listen on 192.168.0.1 only. In that case you will not be able to connect through 127.0.0.1, because the service simply doesn't listen on that IP.
You can "use" this inverse of this feature to make a service accessible only to local clients, not on the public IP-Address, by listening on 127.0.0.1 only, but not on the public IP. This is sometimes used on Linux for example to make MySQL only accessible on the host itself.
I was starting to forget this post but I finally found the problem that was messing things up and it has nothing to do with programmation.
I was doing the calls while the device was connected to the computer via the 'Windows Mobile Device Center' allowing to access the device from Windows.
While connected, the host provided is ignored and all calls on the specified port are handled by the connected computer.
Disconnecting the device allows to communicate properly...

Why HttpWebRequest.GetResponse() fails after connecting to a VPN?

I am trying to control a computer in another room over a local LAN and not the internet which is an important distinction in this case. This is done by sending HttpWebRequests with query strings that command the remote computer to do certain things. One of commands is to tell the remote computer to connect to a VPN using Cisco's AnyConnect client. When it connects, it should send back a reply to the controlling computer that it successfully connected.
For some reason, the reply which is a HttpWebRequest will time out. The strange thing is that I can send HttpWebRequests over the internet with no problem, so it seems to be only a local LAN issue. To get around this issue, I have created a separate console app that is called just to send the replys. I am not sure what is different about it, but it works.
Here is the code from the console app and those are two same lines used in the main program that fail to work.
static void Main(string[] args)
{
// args[0] contains query string ie ?reply=VPNSuccess
HttpWebRequest httpWebReq = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.1.11:1000" + args[0]));
HttpWebResponse httpWebResp = (HttpWebResponse)httpWebReq.GetResponse();
}
Once the vpn is connected the remote machine likely has a new IP address on a different subnet that cannot route back to you.
If I connect a vpn to my company HQ from my house, once the vpn connects I can no longer ping machines on my local network by default. This can be changed via various settings.

using httpListener to create custom website with URL as my IP-Address

I want to create a website with URL as my IP-address[ex: 192.X.X.X]
That website would respond with a "HELLO THERE" message to any user who accesses my URL.
I use the following code to do this![its just a basic code with no threading]
class listenToHTTP
{
HttpListener _listner;
public void start()
{
_listner = new HttpListener();
_listner.Prefixes.Add("http://localhost/");//default port 80
_listner.Start();
}
public void process()
{
while (true)
{
HttpListenerContext context = _listner.GetContext();
byte[] output = Encoding.ASCII.GetBytes("HELLO THERE");
context.Response.ContentEncoding = Encoding.ASCII;
context.Response.ContentLength64 = output.Length;
context.Response.OutputStream.Write(output, 0, output.Length);
}
}
}
The problem is that i dont know the IP-address through which anyone would access.
It perfectly shows the response "HELLO THERE" when I use http://localhost/ as URL.
But what IP-address would other people use so that they can access my simple website.
I have tried my IP-address in the browser but it doesnt work.
There are two things to look out for when doing this;
If you listen to a localhost address, only localhost will be able to connect to your HttpListener. You'll need to add a prefix with http://192.X.X.X/ (where the 192.X.X.X is your local IP of course) and listen to that. That may (depending on your operating system) require you to run as admin, at least if you want to do it on a port < 1024. You can test if this works by connecting to your IP# from your local machine instead of a localhost address.
If you're running Windows, the firewall may get in the way. If it seems to (ie you can connect to your IP# from the local machine but nothing else can connect) you'll need to open the port manually. There are plenty of guides how to do this on Google.
#Joachim reply is already good enough. I will like to add a bit more...
You need to expose the above mentioned IP address publicly to get the URL accessible to others.
In case of Exposing the URL to your domain only (i.e. in case of Intranet), check with your System Administrator to configure the IP Address on Intranet.
Localhost settings accessibility is confined to your machine only.
Make sure to check the firewall constraint for the URL accessibility to implement Point 1 or 2
For more information check the reference for HTTPListener
HTTPListener
HTTPListener
HTTPListener
The problem was that I was referring to a private network address which are local to the network and cannot be accessed by anyone outside that private network..
These are the range of ip-address that are used in private network and so systems with this address cannot be a server or host a website..
10.0.0.0 to 10.255.255.255
172.16.0.0 to 172.31.255.255
192.168.0.0 to 192.168.255.255
You should use a public address..

Running Fleck (or any) Websocket server on Windows Azure

I would like to run a WebSocket server off a worker role in Azure.
This works fine locally on the emulator, but there is a windows firewall prompt the first time the socket server runs.
I'm wondering if anyone would know how to overcome the connection issues with regards to sockets on Azure.
My socket server implementation: OnStart
var server = new WebSocketServer("ws://theappname.cloudapp.net:8080/");
server.Start(socket =>
{
socket.OnOpen = () =>
{
Trace.WriteLine("Connected to " + socket.ConnectionInfo.ClientIpAddress,"Information");
_sockets.Add(socket);
};
});
.... etc
The client implementation:
var socket = new WebSocket("ws://theappname.cloudapp.net:8080");
socket.onopen = function () {
status.html("Connection Opened");
};
socket.onclose = function () {
status.html("Connection Closed");
}
The status changes to closed a few seconds after loading the page.
My endpoint for the worker role below:
WebSocket Input http 8080 <Not Set>
I have now tried to bind to the internal IP address using the following:
RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WebSocket"].IPEndpoint.ToString();
SOLUTION
For the sake of anyone else facing this when implementing websockets on Azure;
Your firewall probably will deny your connection if not on port 80 or 8080 so create a separate deployment for it.
Endpoint must be set to TCP and not HTTP for the correct firewall rules to be created. (see image)
Just for the sake of trial, why don't you change your Input Endpoit from "http" to "tcp" protocol. And explicitly set the local port to 8080 (which in your case is ). Also you have to keep in mind that Windows Azure Load Balancer would kill any connection that is idleing for more than 60 seconds, so you might want to implement some kind of "ping" solution to keep the connection open.
You might want to take a look at this introductory video that Steve Marx (#smarx) put together on how to run node.js on Windows Azure.

No response from sever on external IP in client-server IM app

I'm following a tutorial # http://www.geekpedia.com/tutorial239_Csharp-Chat-Part-1---Building-the-Chat-Client.html to try and gather the basics of networking. For those not wanting to hit the jump, it's a quick tut demonstrating how to program a simple client-server-model chat application.
When I try and run the code in the tut, it works fine as long as both the client and the server are on the same network, but the second I try and do it externally (getting a mate to run the client app, and running the server app my side), it all goes to pot. The fact that the code works when in the same network leads me to believe that it's not a coding issue, but an issue with the way my network is set up.
I'm trying to run the server on my IP address at port 21719, which I have opened, but still other people can't connect to my server, not able to get any form of response at all.
The code (from the tut) that is being used for the server to listen to connections is:
public void StartListening()
{
IPAddress ipaLocal = ipAddress; //ipAddress is parsed from txtIP
tlsClient = new TcpListener(ipaLocal, 21719);
tlsClient.Start();
ServRunning = true; //for the running loop
// Start the new tread that hosts the listener
thrListener = new Thread(KeepListening);
thrListener.Start();
}
Now, the tutorial does actually point out that
IPAddress ipaLocal = ipAddress;
Will cause issues on some configurations, and I'm beginning to fear that my configuration may be included in that.
So, does anyone have any solution for me?
Thanks,
Sam
What is the local IP address that you're using? (ipAddress) If it's 127.0.0.1, that's not correct (I don't know how it would work internally either, but Windows seems to use magic from time to time). Also, if you have multiple NICs in your local machine, maybe the port forwarding is only set up to forward to one of them, and you're using the IP of the other?
If that's not the problem, here are a few generic suggestions:
Grab a copy of netcat. It's a small network testing util whose only job is to form a simple TCP connection. That will allow you to eliminate your code as a variable in all this. If netcat can form a connection, then you know the problem is your code. If not, you've confirmed that it's your router.
You can use WireShark (or TShark) to look for ICMP packets. Capture ICMP packets on the remote machine. If you get "Destination Unreachable" from the router, you've again proved that it's your router.
As Spencer said you need to make sure Port Forwarding is setup on your router, to forward all packets that come in on port 21719 to your internal machine. As for exactly how to do that, it's hard to say without knowing what type of router.
Are you having people use your external (internet) IP address? (See yours here.)
Have you pinholed your router to forward all communications from port 21719 to your server?
Some tips:
What kind of operating system are you using? Please check the Scope and/or Profiles (under Advanced tab) of your firewall rule.
While your friend is trying to telnet to the port (connect to the im server) monitor the traffic using Wireshark or Network Monitor (Wireshark have problems with Vista and Win 7). If you don't see anything hitting your machine the problem is probably on the router side. Double check the settings - you said you set the forward rule (NAT) but did it also set the rule on firewall of your router?

Categories