• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Internet connection test

I need easy and fast way to detect that the computer running my vc#.net application can connect to specific server via internet?
0
f_asmaa
Asked:
f_asmaa
1 Solution
 
Bob LearnedCommented:
public bool CanConnect(string uri, ref string error)
{
 try
  {
     System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
     System.Net.WebResponse resp = req.GetResponse;
     return true;
 }
   catch (Exception ex)
  {
     error = ex.Message;
     return false;
  }
}

Bob
0
 
f_asmaaAuthor Commented:
You assumed that I want to connect to Web server. But what if I want to connect to database server, file server,.. and so on?
What I need is to PING this server from within my vc#.net application.
0
 
Mohammed NasmanSoftware DeveloperCommented:
look at this article
Ping Utility in C#
http://www.c-sharpcorner.com/network/ping.asp
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
_TAD_Commented:


The link above basically recreates the ping utility from scratch (makes for a good read!).  However, there is an easier way.



               
               Process myCmd = null;
               ProcessStartInfo qOptions = new ProcessStartInfo(@"cmd.exe", @"/C ping 127.0.0.1");
               qOptions.WindowStyle = ProcessWindowStyle.Hidden;

               qOptions.RedirectStandardOutput = true;  // set to "true" to enable logging
               qOptions.UseShellExecute = false;  // set to true to make silent/background
               
               myCmd = Process.Start(qOptions);

               StreamReader str = myCmd.StandardOutput;
               Console.WriteLine(str.ReadToEnd());
               
               myCmd.WaitForExit();


This essentially runs the ping command and returns the results to a string.  You will now have a string field with all of the contents that were returned.  You can do a simple query to determine if the results are a success or failure.
0
 
sun4sundayCommented:
You could use P/Invoke layer and call the InternetAutoDial function.
The declaration would be:

public enum InternetAutodialFlags
{
ForceOnline = 1,
ForceUnattended = 2,
FailIfSecurityCheck = 4
}

[DllImport("WinInet.dll")]
public static extern bool InternetAutoDial(
[MarshalAs(UnmanagedType.U4)] ItnernetAutodialFlags flags,
IntPtr parentWindowHandle);

from http://www.dotnet247.com/247reference/msgs/7/37382.aspx


Also check Internet dialup program(source code included)  http://www.codeproject.com/internet/afdialer.asp


Hope this helps

sun4sunday


0
 
_TAD_Commented:


oh, I must mention (if you haven't tried it yet)...

                  qOptions.UseShellExecute = false;  // set to true to make silent/background


The line of code above... if you set it to true to make the process "silent" you won't be able to get a screen re-direct text.  


However, if you prefer have it run silently, you can... But you'll need to change the parameters to write out to a file.  Then you'll need to read the file.



               ProcessStartInfo qOptions = new ProcessStartInfo(@"cmd.exe", @"/C ping 127.0.0.1>C:\temp\ping.dat");


cheers!  (and thanks for the points!)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now