读书人

用C#写了一个图形化Ping但执行后输入

发布时间: 2012-01-31 21:28:41 作者: rapoo

用C#写了一个图形化Ping,但执行后输入Ip点Ping按钮后就没有任何反映,好像程序死了一样,急急急!!
我的图形是这样的:Ip是从textBox1中输入,在点击IP按钮,结果在listBox1中输出
这是Ip按钮的代码:
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();

string Hostclicent =textBox1.Text;

int K;
for (K = 0; K < 3; K++)
{
Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
IPHostEntry Hostinfo;

try
{
Hostinfo = Dns.GetHostEntry(Hostclicent);


}
catch (Exception)
{
listBox1.Items.Add( "没有发现此主机! ");
return;
}


EndPoint Hostpoint = (EndPoint )new IPEndPoint(Hostinfo.AddressList[0], 0);

IPHostEntry Clientinfo;
try
{



Clientinfo = Dns.GetHostEntry(Hostclicent);
}
catch
{
listBox1.Items.Add( "没有这样的主机 ");
}

EndPoint Clientpoint = (EndPoint)new IPEndPoint(Hostinfo.AddressList[0], 0);
int Datasize =32;
int Packetsize = Datasize + 8;
const int Icmp_echo = 8;

IcmpPacket Packet = new IcmpPacket(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 l = 0; l < Cksum_buffer_length; l++)
{

Cksum_buffer[l] = BitConverter.ToUInt16(Buffer, Icmp_header_buffer_index);
Icmp_header_buffer_index += 2;
}

Packet.Checksum = IcmpPacket.SumOfCheck(Cksum_buffer);
Byte[] Senddata = new Byte[Packetsize];
Index = Packet.CountByte(Senddata);
if (Index != Packetsize)
{
listBox1.Items.Add( "报文出现问题! ");
return;
}
int Nbytes = 0;
int Starttime = Environment.TickCount;

if ((Nbytes = Socket.SendTo(Senddata, Packetsize, SocketFlags.None, Hostpoint)) == -1)
{
listBox1.Items.Add( "无法传送报文! ");
}
Byte[] Receivedata = new Byte[256];
Nbytes = 0;
int Timeout = 0;


int Timeconsume = 0;
while (true)
{

Nbytes = Socket.ReceiveFrom(Receivedata, 256, SocketFlags.None, ref Clientpoint);

if (Nbytes == -1)
{
listBox1.Items.Add( "主机没有响应! ");
break;
}
else if (Nbytes > 0)
{
Timeconsume = System.Environment.TickCount-Starttime;
listBox1.Items.Add( "reply from " + Hostinfo.AddressList[0].ToString() + "In " + Timeconsume + "ms:bytes Received " + Nbytes);
break;
}
Timeout = Environment.TickCount - Starttime;
if (Timeout > 1000)
{
listBox1.Items.Add( "time Out! ");



break;
}

Socket.Close();
}
}

这是ICMP协议IcmpPacket类代码:
public class IcmpPacket
{

private Byte _type;
private Byte _subCode;
private UInt16 _checkSum;
private UInt16 _identifier;
private UInt16 _sequenceNumber;
private Byte[] _data;
public IcmpPacket(Byte type, Byte subCode, UInt16 checkSum, UInt16 identifier, UInt16 sequenceNumber, int dataSize)
{
_type = type;
_subCode = subCode;
_checkSum = checkSum;
_identifier = identifier;
_sequenceNumber = sequenceNumber;
_data = new Byte[dataSize];
for (int i = 0; i < dataSize; i++)
{
_data[i] = (byte) '# ';
}
}

public UInt16 Checksum
{
get
{


return _checkSum;
}
set
{
_checkSum = value;
}
}
public int CountByte(Byte[] buffer)
{
Byte[] b_type = new Byte[1] { _type };
Byte[] b_code = new Byte[1] { _subCode };
Byte[] b_cksum = BitConverter.GetBytes(_checkSum);
Byte[] b_id = BitConverter.GetBytes(_identifier);
Byte[] b_seq = BitConverter.GetBytes(_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(_data, 0, buffer, i, _data.Length);
i += _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 & 0xfff);
cksum += (cksum > > 16);
return (UInt16)(~cksum);
}
}

[解决办法]
楼主,我按你的程序改写了一下,可以基本实现功能.希望对你有帮助.(界面设计应该跟你的一样,一个listBox,一个TextBox,一个Button).
我觉得可以用线程,不过那是在连续PING时才有用,我想你在Button_Click事件里写的代码太多,以至于你无法分析.可以试着独立出来,这样有错比较容易看出来.
我的代码:
using System;
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 Ping
{
public partial class Form1 : Form
{
const int SOCKET_ERROR = -1;

const int ICMP_ECHO = 8;

public Form1()
{
InitializeComponent();
}
private string ping(string host)
{
// 声明 IPHostEntry
IPHostEntry serverHE, fromHE;
int nBytes = 0;
int dwStart = 0, dwStop = 0;
//初始化ICMP的Socket
Socket socket =new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
// 得到Server EndPoint
try
{
serverHE = Dns.GetHostByName(host);
}
catch(Exception)
{
return "没有发现主机 ";
}
// 把 Server IP_EndPoint转换成EndPoint
IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0);
EndPoint epServer = (ipepServer);
// 设定客户机的接收Endpoint
fromHE = Dns.GetHostByName(Dns.GetHostName());
IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0);
EndPoint EndPointFrom = (ipEndPointFrom);
int PacketSize = 0;
IcmpPacket packet = new IcmpPacket();
// 构建要发送的包
packet.Type = ICMP_ECHO; //8
packet.SubCode = 0;
packet.CheckSum = UInt16.Parse( "0 ");
packet.Identifier = UInt16.Parse( "45 ");
packet.SequenceNumber = UInt16.Parse( "0 ");
int PingData = 32; // sizeof(IcmpPacket) - 8;
packet.Data = new Byte[PingData];
// 初始化Packet.Data
for (int i = 0; i < PingData; i++)


{
packet.Data[i] = (byte) '# ';
}
//Variable to hold the total Packet size
PacketSize = PingData + 8;
Byte [] icmp_pkt_buffer = new Byte[ PacketSize ];
Int32 Index = 0;
//Call a Method Serialize which counts
//The total number of Bytes in the Packet
Index = Serialize(packet,icmp_pkt_buffer,PacketSize,PingData );
//Error in Packet Size
if( Index == -1 )
{
return "Error Creating Packet ";
}
// convert into a UInt16 array
//Get the Half size of the Packet
Double double_length = Convert.ToDouble(Index);
Double dtemp = Math.Ceiling( double_length / 2);
int cksum_buffer_length = Convert.ToInt32(dtemp);
//Create a Byte Array
UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length];
//Code to initialize the Uint16 array
int icmp_header_buffer_index = 0;
for( int i = 0; i < cksum_buffer_length; i++ )
{
cksum_buffer[i] = BitConverter.ToUInt16(icmp_pkt_buffer,icmp_header_buffer_index);
icmp_header_buffer_index += 2;
}
//Call a method which will return a checksum
UInt16 u_cksum =packet.checksum(cksum_buffer, cksum_buffer_length);
//Save the checksum to the Packet
packet.CheckSum = u_cksum;
// Now that we have the checksum, serialize the packet again
Byte [] sendbuf = new Byte[ PacketSize ];
//again check the packet size
Index = Serialize( packet, sendbuf, PacketSize,PingData );
//if there is a error report it
if( Index == -1 )
{
return "Error Creating Packet ";
}

dwStart = System.Environment.TickCount; // Start timing
//send the Packet over the socket

if ((nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) == SOCKET_ERROR)
{
return "Socket Error: cannot send Packet ";
}
// Initialize the buffers. The receive buffer is the size of the
// ICMP header plus the IP header (20 bytes)
Byte [] ReceiveBuffer = new Byte[256];
nBytes = 0;
//Receive the bytes
bool recd =false ;
int timeout=0 ;
//loop for checking the time of the server responding
while(!recd)
{
nBytes = socket.ReceiveFrom(ReceiveBuffer, 256, 0, ref EndPointFrom);
if (nBytes == SOCKET_ERROR)
{
return "主机没有响应 " ;
}
else if(nBytes> 0)
{
dwStop = System.Environment.TickCount - dwStart; // stop timing
return "Reply from "+epServer.ToString()+ " in "+dwStop+ "ms. Received: "+nBytes+ " Bytes. ";
}

timeout=System.Environment.TickCount - dwStart;

if(timeout> 1000)
{
return "超时 " ;
}
}
//close the socket
socket.Close();
return " ";
}
未完……见下贴

读书人网 >C#

热点推荐