Wednesday, January 5, 2011

 hi folks !!
this is C# about sending ping request.
 
private void HostPing(string host, int timeout)
    {
//host can be an ip 
         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 = "dsfgsdfgsdfgsdfgdsfg ";
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        PingReply reply = pingSender.Send(host, timeout, buffer, options);
 // check  reply.Status  if ya want ...

      //  return (reply.Status == IPStatus.Success);

    }

 //this is to send a request with the port defined
private void checkHostAndPortAvailability(string host, int port)
    {
        IPHostEntry IPHost = new IPHostEntry();
        IPHost = Dns.GetHostEntry(host);

        IPAddress IPAddr = IPHost.AddressList[0];

        try
        {

            TcpClient TcpCli = new TcpClient();
            TcpCli.Connect(IPAddr, port);
            TcpCli.Close();
//if succussful reach here,  else go to catch
        }
        catch (Exception ex)
        {
            // error handling code
        }
    }


N.B.



using System.Net.NetworkInformation;
using System.Net.Sockets; note to add these usings

No comments:

Post a Comment