Question

It was all working an hour ago and many days ago. The link i try to ping is:

Link to ping

This is the code in form1:

nc = new NetworkConnection();
bool bval = nc.PingConnection(satellite_address);

if (bval)
{
    label19.Visible = true;
    label19.Text = "Internet Access";
}
else
{
    label19.Visible = true;
    label19.Text = "No Internet Access";
}

When it's trying to execute this line:

bool bval = nc.PingConnection(satellite_address);

It's going to the nc class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.IO;
using System.Windows.Forms;

namespace mws
{
    class NetworkConnection
    {
        public NetworkConnection()
        {    
        }

        public bool PingConnection(string url)
        {
            bool Result = false;

            using (Ping pp = new Ping())
            {
                byte[] buffer = Encoding.ASCII.GetBytes("samplestring");
                int timeout = 120;

                try
                {
                    PingReply reply = pp.Send(url, timeout, buffer);
                    if (reply.Status == IPStatus.Success)
                        Result = true;
                }
                catch (Exception)
                {
                    Result = false;
                }
            }
            return Result;
        }
    }
}

In the nc class when trying to do the line:

PingReply reply = pp.Send(url, timeout, buffer);

It's jumping to the catch block and throws a PingException:

An exception occurred during a Ping request

And then in Form1 the result it return is that there is no internet access but there is internet and I can surf to the url no problems.

This is the complete exception message:

  System.Net.NetworkInformation.PingException was caught
  HResult=-2146233079
  Message=An exception occurred during a Ping request.
  Source=System
  StackTrace:
       at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
       at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer)
       at mws.NetworkConnection.PingConnection(String url) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\NetworkConnection.cs:line 33
  InnerException: System.Net.Sockets.SocketException
       HResult=-2147467259
       Message=No such host is known
       Source=System
       ErrorCode=11001
       NativeErrorCode=11001
       StackTrace:
            at System.Net.Dns.GetAddrInfo(String name)
            at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
            at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
            at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
       InnerException:

Line 33 is:

PingReply reply = pp.Send(url, timeout, buffer);

What could be the reason that this exception show up ? it didn't show up before ever my program is working for some yeras now.

And what or how should i handle it ?

Was it helpful?

Solution

You cannot pass a full URL to the Send method of the Ping class. The parameter string hostNameOrAddress needs to be

A String that identifies the computer that is the destination for the ICMP echo message. The value specified for this parameter can be a host name or a string representation of an IP address.

So you can only pass in www.sat24.com or the IP of the host 82.94.176.100 (taken from the CommandLine ping www.sat24.com).

If you want to pass a full URL to your method you need to extract the Host from that URL to perform your Ping. For this case you can take the Uri class

Uri uri = new Uri(url);
PingReply reply = pp.Send(uri.Host, timeout, buffer);

OTHER TIPS

PingReply reply = pp.Send(url, timeout, buffer);

"No such host is known"

My bet is that no such host is known.

You should ping "www.sat24.com" not "http://www.sat24.com/..."

Ping.Send doesn't say it accepts a URL

public PingReply Send(
    string hostNameOrAddress,
    int timeout,
    byte[] buffer
)

hostNameOrAddress   A String that identifies the computer that is the destination for the ICMP echo message. The value specified for this parameter can be a host name or a string representation of an IP address.

http://msdn.microsoft.com/en-us/library/ms144954(v=vs.110).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top