Internet connection test

Posted on 2005-03-29
Medium Priority
Last Modified: 2010-04-16
I need easy and fast way to detect that the computer running my vc#.net application can connect to specific server via internet?
Question by:f_asmaa
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 96

Expert Comment

by:Bob Learned
ID: 13653740
public bool CanConnect(string uri, ref string error)
     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;


Author Comment

ID: 13653783
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.
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 13654199
look at this article
Ping Utility in C#
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Accepted Solution

_TAD_ earned 1000 total points
ID: 13656329

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");
               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;

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.

Expert Comment

ID: 13704521
You could use P/Invoke layer and call the InternetAutoDial function.
The declaration would be:

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

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


LVL 22

Expert Comment

ID: 13707290

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>C:\temp\ping.dat");

cheers!  (and thanks for the points!)

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question