C#网络通信程序设计基于ICMP的活动主机探测程序设计如何在界面上显示PING的统计结果(成功,失败次数)
用的是visual studio 2010
下面是窗体程序
下面是代码
program
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace pingProgram
{
public class IcmPacket
{
private Byte my_type;
private Byte my_subCode;
private UInt16 my_checkSum;
private UInt16 my_identifier;
private UInt16 my_sequenceNumber;
private Byte[] my_data;
public IcmPacket(Byte type, Byte subCode, UInt16 checkSum, UInt16 identifier, UInt16 sequenceNumber, int dataSize)
{
my_type = type;
my_subCode = subCode;
my_checkSum = checkSum;
my_identifier = identifier;
my_sequenceNumber = sequenceNumber;
my_data = new Byte[dataSize];
for (int i = 0; i < dataSize; i++)
{
my_data[i] = (byte)'k';
}
}
public UInt16 CheckSum
{
get
{
return my_checkSum;
}
set
{
my_checkSum=value;
}
}
public int CountByte(Byte[] buffer)
{
Byte[] b_type = new Byte[1] { my_type };
Byte[] b_code = new Byte[1] { my_subCode };
Byte[] b_cksum = BitConverter.GetBytes(my_checkSum);
Byte[] b_id = BitConverter.GetBytes(my_identifier);
Byte[] b_seq = BitConverter.GetBytes(my_sequenceNumber);
int i = 0;
Array.Copy(b_type, 0, buffer, i, b_type.Length);
i += b_type.Length;
Array.Copy(b_code, 0, buffer, i, b_code.Length);
i += b_code.Length;
Array.Copy(b_cksum, 0, buffer, i, b_cksum.Length);
i += b_cksum.Length;
Array.Copy(b_id, 0, buffer, i, b_id.Length);
i += b_id.Length;
Array.Copy(b_seq, 0, buffer, i, b_seq.Length);
i += b_seq.Length;
Array.Copy(my_data, 0, buffer, i, my_data.Length);
i += my_data.Length;
return i;
}
public static UInt16 SumOfCheck(UInt16[] buffer)
{
int cksum = 0;
for (int i = 0; i < buffer.Length; i++)
cksum += (int)buffer[i];
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (UInt16)(~cksum);
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
form1
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace pingProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPing_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
if (textBox1.Text == "")
{
MessageBox.Show("IP地址不能为空!");
return;
}
string Hostclient = textBox1.Text;
Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
Socket.ReceiveTimeout = 1000;
IPHostEntry Hostinfo;
try
{
Hostinfo = Dns.GetHostEntry(Hostclient);
}
catch (Exception)
{
listBox1.Items.Add("无法辨识主机!");
return;
}
EndPoint Hostpoint = (EndPoint)new IPEndPoint(Hostinfo.AddressList[0], 0);
IPHostEntry Clientinfo;
Clientinfo = Dns.GetHostEntry(Hostclient);
EndPoint Clientpoint = (EndPoint)new IPEndPoint(Clientinfo.AddressList[0], 0);
int DataSize = 4;
int PacketSize = DataSize + 8;
const int Icmp_echo = 8;
IcmPacket Packet = new IcmPacket(Icmp_echo, 0, 0, 45, 0, DataSize);
Byte[] Buffer = new Byte[PacketSize];
int index = Packet.CountByte(Buffer);
if (index != PacketSize)
{
listBox1.Items.Add("报文出现错误!");
return;
}
int Cksum_buffer_length = (int)Math.Ceiling(((Double)index)/2);
UInt16[] Cksum_buffer = new UInt16[Cksum_buffer_length];
int Icmp_header_buffer_index = 0;
for (int I = 0; I < Cksum_buffer_length; I++)
{
Cksum_buffer[I] = BitConverter.ToUInt16(Buffer, Icmp_header_buffer_index);
Icmp_header_buffer_index += 2;
}
Packet.CheckSum = IcmPacket.SumOfCheck(Cksum_buffer);
Byte[] SendData = new Byte[PacketSize];
index = Packet.CountByte(SendData);
if (index != PacketSize)
{
listBox1.Items.Add("报文出现错误!");
return;
}
int pingNum=4;
for (int i = 0; i < 4; i++)
{
int Nbytes = 0;
int startTime = Environment.TickCount;
try
{
Nbytes = Socket.SendTo(SendData, PacketSize, SocketFlags.None, Hostpoint);
}
catch (Exception)
{
listBox1.Items.Add("无法传送报文!");
return;
}
Byte[] ReceiveData = new Byte[256];
Nbytes = 0;
int Timeconsume = 0;
while (true)
{
try
{
Nbytes = Socket.ReceiveFrom(ReceiveData, 256, SocketFlags.None, ref Clientpoint);
}
catch (Exception)
{
listBox1.Items.Add("超时无响应!");
break;
}
if (Nbytes > 0)
{
Timeconsume = System.Environment.TickCount - startTime;
if (Timeconsume < 1)
listBox1.Items.Add("reply from: " + Hostinfo.AddressList[0].ToString() + " Send: " + (PacketSize + 20).ToString() + " time<1ms " +
"bytes Received " + Nbytes.ToString());
else
listBox1.Items.Add("reply from: " + Hostinfo.AddressList[0].ToString() + " Send: " + (PacketSize + 20).ToString() + " In "
+ Timeconsume.ToString() + " ms;bytes Received " + Nbytes.ToString());
break;
}
}
}
Socket.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog savedlg = new SaveFileDialog();
savedlg.Filter = "文本文件|*.txt";
savedlg.Title = "保存记录";
savedlg.ShowDialog();
if (savedlg.FileName != "")
{
string localFilePath;
localFilePath = "";
localFilePath = savedlg.FileName.ToString();
string str = "";
for (int j = 0; j < listBox1.Items.Count; j++)
{
str += listBox1.Items[j].ToString() + "\r\n";
}
FileStream fs = new FileStream(localFilePath, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(str);
sw.Flush();
sw.Close();
fs.Close();
MessageBox.Show(" PING结果保存完毕。 ","PING程序设计",MessageBoxButtons.OK);
}
}
}
}