Posted in General

how to ping some machine

Ok i was answering questions at http://www.dotnetjunkies.com forums. Someguy had this question. so here is the answer…

Ping in .NET using C# or VB.NET? In other words, how can you find is some host (may be web site) is alive?


#region PING Example
private void button1_Click(object sender, System.EventArgs e)
{
CheckConnection("localhost");
CheckConnection("www.thiswebsitenamedoesnotexistsatontheplanet.com");
}

public void CheckConnection(string server)
{
//Set up variables and String to write to the server.
Encoding ASCII = Encoding.ASCII;
string Get = string.Format("GET / HTTP/1.1\r\nHost: {0}\r\nConnection: Close\r\n\r\n", server);
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;

// IPAddress and IPEndPoint represent the endpoint that will
// receive the request.
// Get first IPAddress in list return by DNS.
try
{
// Define those variables to be evaluated in the next for loop and
// then used to connect to the server. These variables are defined
// outside the for loop to make them accessible there after.
Socket s = null;
IPEndPoint hostEndPoint;
IPAddress hostAddress = null;
int conPort = 80;

// Get DNS host information.
IPHostEntry hostInfo = Dns.Resolve(server);
// Get the DNS IP addresses associated with the host.
IPAddress[] IPaddresses = hostInfo.AddressList;

// Evaluate the socket and receiving host IPAddress and IPEndPoint.
for (int index=0; index

VB.NET will have similar code.

Advertisements

One thought on “how to ping some machine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s