I wish to write a windows app which does something when I become disconnected from the internet. I was thinking of writing a very simple C#/Delphi app which simply polls every 20 seconds to see if I'm still connected.
If I have to poll I'd really like a solution other than trying to download a web page from the net. I can't assume that a download attempt failing means "not online" since there may be other apps eating up the internet bandwidth. Plus I'm sure constantly connecting/downloading from a particular site is going to get my IP blocked.
I'm sure there's a way to tell if you're online without downloading/connecting to a remote server but I'm not sure how.
Beware that connected to the Internet does not really mean anything: what if you are connected to your ISP, but the backbone is down, or all the sites you want to access are in a country that went off the grid like recently? Having a connection does not mean you can do what you want.
Anyway, as stated before you can use the InternetGetConnectedState API to test that you have a valid Internet connection configured.
As an example, the following routine told me correctly I had a LAN Connection, but failed to detect that I had my ZoneAlarm firewall set to block "All Internet Activity", which means that you effectively lost all Internet connectivity.
Delphi routine:
procedure IsConnected;
var
dwFlags: DWORD;
begin
if InternetGetConnectedState(#dwFlags, 0) then
begin
if (dwFlags and INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM then
ShowMessage('Modem Connection')
else
if (dwFlags and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then
ShowMessage('LAN Connection')
else
if (dwFlags and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then
ShowMessage('Connection thru Proxy')
else
if (dwFlags and INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE then
ShowMessage('Local system in offline mode')
else
if (dwFlags and INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED then
ShowMessage('Valid connection exists, but might or might not be connected')
end
else
ShowMessage('Not Connected. Try to connect and risk of being prompted to dial into another Internet Service Provider.');
end;
Call the InternetGetConnectedState function. This knowledgebase article explains how to do it.
It looks like it can be done by using the method described here: http://www.csharphelp.com/archives3/archive499.html
Related
I have a component that (by part) uses an internet connection. I wrote some UnitTests to ensure that to component is working. However, I would like to test the behaviour of the component without internet connections.
So, my goal is to somehow temporary disable internet, or the whole internet connection, and reactivate after test.
I would disable\enable like here local are connection in test initialization
[ClassInitialize]
SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
if (((string)item["NetConnectionId"]) == "Local Network Connection")
{
item.InvokeMethod("Disable", null);
}
}
[ClassCleanup()]
// Enable local area connetcion
There are many ways in which the system could have "No Internet" and the answer really depends on what you mean.
As the accepted other answer suggests, you could simply disable the network interface. That guarantees you have no internet, but the computer also will know it has no network either.
A couple other options are
To remove your Default Gateway (this may require setting static IP settings in the control panel, though I'm sure you could do it programmatically as well)
This way, the computer still thinks it's connected, but it won't have any network access except on the local subnet
Remove DNS server settings, see above link.
This way, the computer has direct IP based access but to a regular user it would appear as if there was "no internet."
Whilst not a direct answer to your question I believe you may find some use in this tool - https://jagt.github.io/clumsy/download
I've used it at work to simulate different network conditions for an mobile app that I'm currently working on. It is possible to completely disable the network connection by setting packet drop to 100%.
I am running an asp.net mvc website, and i want to block every user that reaches my site through TOR. By now i have two solutions:
Download list of TOR exit nodes once every hour, store that list in
memory, and check every request IP address with that list.
Try to block TOR exit nodes with windows firewall - i think that this would
be better, but i don't know how to do that.
Is there any other possible solution? Have any of you maybe had a similar problem to mine? How did you solve it?
The answer is absolutely the second option you listed. You will have to download a list of known exit node IP's every so often regardless of which solution you use, but using the firewall that already exists is much more simple than rolling your own primitive replica.
How the IP's can be added to the firewall depends on your version of Windows. A previous StackOverflow question whose answer includes links that explain how to programmatically block IP addresses via the Windows Server 2008 firewall can be found here.
Here(https://github.com/RD17/DeTor) is a simple REST API which use TorDNSEl to determine whether a request was made from TOR network or not. I think it will be pretty simple to use it from C# with RESTSharp for example.
The request is:
curl -X GET http://detor.ambar.cloud/.
The response is
{
"sourceIp": "104.200.20.46",
"destIp": "89.207.89.82",
"destPort": "8080",
"found": true
}
As a bonus you can add a badge to your site to detect whether a user comes from TOR or not:
<img src='http://detor.ambar.cloud/badge' />
So i have a lack of knowledge issue with this.
I'm currently streaming my webcam and trying to do a small conferencing application on WPF. I can easily see the streamed video by doing <MediaElement Name="VideoControl" Source="http://localhost:8080"/> on my computer.
But i don't know what to write instead of http://localhost:8080 on an external computer because of my lack of knowledge. I've tried to write my external ip address, i've tried to write my local network ip with a computer on the same network. None of them has worked.
To sum up, i need to know how to access my stream from anywhere around the world (wow that sounded extremely like IP).
Here is the code i wrote to broadcast my stream;
_job = new LiveJob();
EncoderDevice videoDev = null;
foreach (EncoderDevice ved in EncoderDevices.FindDevices(EncoderDeviceType.Video))
if (ved.Name == VideoDevices.SelectedItem.ToString())
videoDev = ved;
EncoderDevice audioDev = null;
foreach (EncoderDevice aed in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
if (aed.Name == AudioDevices.SelectedItem.ToString())
audioDev = aed;
// preset, editting etc..
_job.ApplyPreset(LivePresets.VC1HighSpeedBroadband16x9);
LiveDeviceSource deviceSource = _job.AddDeviceSource(videoDev, audioDev);
_job.ActivateSource(deviceSource);
PullBroadcastPublishFormat outputFormat = new PullBroadcastPublishFormat();
outputFormat.BroadcastPort = 8080;
outputFormat.MaximumNumberOfConnections = 10;
_job.PublishFormats.Add(outputFormat);
_job.StartEncoding();
I'm sorry for my bad explanation, Thanks for the help!
Noone ever answered this?
Did you ever get it to work? If you could not open the stream from another computer in your LAN with VLC player, I would think your firewall is the blocker.
Allways when having issues with networking, turn off EVERYTHING so your stuff works, then turn back one and one. That way you get to find out which firewall/setting that is blocking you.
Once you get it to work in LAN, try from a machine outside your home network. Not all routers/modems support rerouting you back into your network if you try to access the external IP of your modem/router from inside your LAN. Your external IP can be found at whatismyip.com or similar sites.
So you will probably have to rely on help from someone else or try out some VPN solution to test conenction to your external address from outside your network.
is there any way to check internet connection status in linux using mono
If it's desktop app, you could query NetworkManager (which is the network connection manager on most Linux desktops) over d-bus, using the NDesk.DBus library.
See Banshee for an example: http://git.gnome.org/cgit/banshee/tree/src/Core/Banshee.Services/Banshee.Networking/NetworkManager.cs
Apart from what Michael already suggested for a desktop application, you can also do something like:
foreach (NetworkInterface ni in NetworkInformation.GetAllNetworkInterfaces ()) {
// Check that any or all of:
// -ni.OperationalStatus == OperationalStatus.Up
// -that ni.NetworkInterfaceType is ethernet or wireless80211
// -ni.GetIPProperties() has a gateway and a DNS server
// ...
}
No matter what you end up using, it won't be reliable.
I see it all the time with Windows Vista and 7 at home. I use a home network, so my computers are always "connected." However, they are not always connected to the Internet.
That said, I would recommend checking the network interfaces as Gonzalo said. It is your best bet.
I would not rely on NetworkManager being present. I hate that thing and turn it off whenever I can. It is huge, ungainly, has an ugly name, relies on junk like HAL and DBUS. Early versions permanently put me off because they didn't work unless you were logged in to a GUI. It also collected bug work-arounds for wifi that were completely ridiculous in an open-source operating system that should have just fixed the original bugs. That led to other wifi managers and the command-line not being able to work properly and people being told to use NetworkManager, only because no one ever bothered to fix the actual bug!
You could try to open your connection as it is needed. If that fails display an error message.
Alternatively, if you really need a general check (e.g. at application start) you could try to make HTTP requests to one or more omnipresent websites like google.com. (Or what ever protocol you mean by "internet").
Check out HttpWebRequest.
The title pretty much says it all. Some caveats are:
I need to be able to do it in C#
It needs to be able to be done from a remote server (ie, running on one server, checking IIS on another)
Needs to be close to real-time (within 1 second)
Can use WMI calls
I've tried watching the log file, but it turns out that isn't nearly close enough to real-time.
Thanks!
EDIT: I put this in a comment on Tom's answer, but it's more visible here:
I was able to look for changes using this counter:
var perf = new PerformanceCounter("ASP.NET Apps v2.0.50727", "Requests Total", "_LM_W3SVC_[IIS-Site-ID]_ROOT", "[Server-Name]");
How about reading the ASP.NET requests/sec performance counter on the remote machine?
The System.Diagnostics.PerformanceCounter class has a constructor which takes a machine name.