Hi everyone;
I need some help
Im want to write a WIndows service C# app which will check some urls and choose the one with the lowest ping (the one with high data transfer speed) stream data from that server, and alse check all other servers and write in log file if any of servers is OFF or can't be accesed. And also if current server speed's down, it should switch to other server automaticly.
Please help people, i'm new in C# and moreover has never written any app like this,

Recommended Answers

All 9 Replies

I had done something similar a few years ago.. there might be something out there which is way simpler to implement today. However, I had used the Process class, opened a cmd, literally pinged the server through ping command, and parsed the return time in ms or set the server as down if return timed out

I had done something similar a few years ago.. there might be something out there which is way simpler to implement today. However, I had used the Process class, opened a cmd, literally pinged the server through ping command, and parsed the return time in ms or set the server as down if return timed out

I've also thought of this, but how i can ping a server in windows C# and, i guess i need to do every 30 or 60 seconds.

The below sample opens a command line application.. replace the exe filename with cmd.exe.. and paramaters add the ping command as one would do in command prompt to ping a server... then parse the results:

// Use ProcessStartInfo class
	ProcessStartInfo startInfo = new ProcessStartInfo();
	startInfo.CreateNoWindow = false;
	startInfo.UseShellExecute = false;
	startInfo.FileName = "dcm2jpg.exe";
	startInfo.WindowStyle = ProcessWindowStyle.Hidden;
	startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

	try
	{
	    // Start the process with the info we specified.
	    // Call WaitForExit and then the using statement will close.
	    using (Process exeProcess = Process.Start(startInfo))
	    {
		exeProcess.WaitForExit();
	    }
	}
	catch
	{
	    // Log error.
	}

.Net 4.0 or lower?

.Net 4.0 or lower?

.Net 4.0, but I don't understand your codes.

Forget about my first post.. i had used that code in 1.0 framework.. post that framework Microsoft created a Ping class.. the below code is synchronous, so the app will await a reply before proceeding. The link i sent in an earlier post has a code snippet to ping asynchronously

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace Examples.System.Net.NetworkInformation.PingTest
{
    public class PingExample
    {
        // args[0] can be an IPaddress or host name.
        public static void Main (string[] args)
        {
            Ping pingSender = new Ping ();
            PingOptions options = new PingOptions ();

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
            }
        }
    }
}

Forget about my first post.. i had used that code in 1.0 framework.. post that framework Microsoft created a Ping class.. the below code is synchronous, so the app will await a reply before proceeding. The link i sent in an earlier post has a code snippet to ping asynchronously

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace Examples.System.Net.NetworkInformation.PingTest
{
    public class PingExample
    {
        // args[0] can be an IPaddress or host name.
        public static void Main (string[] args)
        {
            Ping pingSender = new Ping ();
            PingOptions options = new PingOptions ();

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
            }
        }
    }
}

thank you very much
i guess ping round trip time and are exactly what i need,
so as i understood u send your string data and it will respond to it...
nice :)

yep.. too bad this did not exist in Framework 1.0 :)

Please mark this post as solved if its what you need.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.