-
Ping Program
Hey guys what's up, okay I need to make a simple application.. That will ping like 10 ip address and This is what I have so far..
1 Textbox - Which contains an ip address
1 labelbox - Which will turn either green or red depending on if the connection
is alive or dead.
1 Textbox - Which will display the "ms" time it took for that packet of data to get back and fourth.
And that's it..
I will have 10 of these with different ip's and I would like it to cycle through each ip address and give me the information above..
Does anyone know how I can do this.
Thanks In Advance.
ScarEye
P.S. I am a total n00b in C# so please explain in laymens terms. :bigyello:
Oh yea just to make sure you have this right.. All I did so far was just create the textboxes and the labels that's it. Still learning. :D
-
Re: Ping Program
Hey guys, I downloaded a free ping utility that works with C#. Now I was wondering if I add multiple text boxes with ip's and when I click on the ping button I would like it to cycle through all the text boxes and give me my results. If anyone can take a look at it and help me out that would be great. You can download the program here
Thanks In Advance
ScarEye
Hopefully this thread will help others..
-
Re: Ping Program
Code:
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
namespace Ping
{
/// <summary>
/// Zusammenfassung für Class1.
/// </summary>
internal class Start
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
private static void Main()
{
string address = "192.168.0.1";
PingPC.PingResult pingResult = PingPC.Ping(address, 1000);
switch (pingResult)
{
case PingPC.PingResult.OK:
Console.WriteLine("Ping an {0} war erfolgreich", address);
break;
case PingPC.PingResult.TimeOut:
Console.WriteLine("TimeOut beim Ping an {0}", address);
break;
case PingPC.PingResult.ChecksumError:
Console.WriteLine("Daten wurden empfangen, aber es ist ein Prüfsummenfehler aufgetreten {0}", address);
break;
}
Console.Read();
}
}
internal class PingPC
{
public enum PingResult
{
OK,
TimeOut,
ChecksumError
}
// Methode zum Pingen
public static PingResult Ping(string address, int timeout)
{
//IPHostEntry ipHostEntry = Dns.Resolve(address);
//IPAddress ipAddress = ipHostEntry.AddressList[0];
IPAddress ipAddress = IPAddress.Parse(address);
IPEndPoint epHost = new IPEndPoint(ipAddress, 7);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
byte[] sendBuffer = {8, 0, 0xF7, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int send = socket.SendTo(sendBuffer, sendBuffer.Length, SocketFlags.None, epHost);
ArrayList checkReadSockets = new ArrayList();
checkReadSockets.Add(socket);
Socket.Select(checkReadSockets, null, null, timeout*1000);
byte[] receiveBuffer = new byte[200];
int receivedBytes = 0;
if (checkReadSockets.Count > 0)
{
receivedBytes = socket.Receive(receiveBuffer, receiveBuffer.Length, SocketFlags.None);
if (GetTCPCheckSum(receiveBuffer) == 0)
return PingResult.OK;
else
return PingResult.ChecksumError;
}
else
return PingResult.TimeOut;
}
private static ushort GetTCPCheckSum(byte[] tcpData)
{
byte[] buffer;
if ((tcpData.Length%2) > 0)
{
buffer = new byte[tcpData.Length + 1];
tcpData.CopyTo(buffer, 0);
buffer[buffer.Length - 1] = 0;
}
else
{
buffer = new byte[tcpData.Length];
tcpData.CopyTo(buffer, 0);
}
int checkSum = 0;
for (int i = 0; i < buffer.Length; i += 2)
{
byte lowByte = buffer[i + 1];
byte highByte = buffer[i];
ushort wordValue = highByte;
wordValue = (ushort) ((wordValue << 8) + lowByte);
checkSum += wordValue;
}
checkSum = (checkSum >> 16) + (checkSum & 0xFFFF);
checkSum += (checkSum >> 16);
return (ushort) (~checkSum);
}
}
}