我的图形是这样的: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);
        }
    }

解决方案 »

  1.   

    原因很简单,你没有使用线程,Thread应该去看看。当你点击按钮时应该新建一个线程来进行ping,要结束时直接结束线程即可。
      

  2.   

    我没有看过进程的,应该怎么加一下啊!
    谢谢提醒!
    ==================================
    线程!请参考thread类..
      

  3.   

    用自己用Icmp报文么,直接调用cmd.exe ping就可以
    用Process对象
      

  4.   

    这是我们计算机网络的课程设计,自己重写ping命令,不能用系统的ping.怎么在那个程序里加线程啊??
      

  5.   

    greennetboy(我的老婆叫静静) ( ) 信誉:100    Blog  2007-1-12 23:33:59  得分: 0  
     
     
       
    用自己用Icmp报文么,直接调用cmd.exe ping就可以
    用Process对象
    ---------------
    这个家伙说的对!
      
     
      

  6.   

    .NET类库中实现了一个Ping 好像是2.0中加的 呵呵
      

  7.   

    private void button1_Click(object sender, EventArgs e)
            {            Thread thdProcess = new Thread(new ThreadStart(ThreadFun));            thdProcess.Start();        }
     private void ThreadFun()
            {            //Create invoke method by specific function            MethodInvoker mi = new MethodInvoker(this.InvokeFun);            for (int i = 0; i < 3; i++)
                {                this.BeginInvoke(mi);                Thread.Sleep(1000);            }        } private void InvokeFun()
            {
                listBox1.Items.Add("ri");
                string Hostclicent = textBox1.Text;
                
                            Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
                IPHostEntry Hostinfo;
                EndPoint Hostpoint;            try
                {
                    Hostinfo = Dns.GetHostEntry(Hostclicent);
                    Hostpoint = (EndPoint)new IPEndPoint(Hostinfo.AddressList[0], 0);            }
                catch (Exception)
                {
                    listBox1.Items.Add("没有发现此主机!");
                    return;
                }
                Hostpoint = (EndPoint)new IPEndPoint(Hostinfo.AddressList[0], 0);            IPHostEntry Clientinfo;
                EndPoint Clientpoint;
                try
                {                Clientinfo = Dns.GetHostEntry(Hostclicent);
                    Clientpoint = (EndPoint)new IPEndPoint(Clientinfo.AddressList[0], 139);
                }
                catch
                {
                    listBox1.Items.Add("lajiwanyiu");
                    return;
                }
                Clientpoint = (EndPoint)new IPEndPoint(Clientinfo.AddressList[0], 139);
                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 " + Hostpoint.ToString() + "In" + Timeconsume + "ms:bytes Received" + Nbytes);
                        break;
                    }
                    Timeconsume = Environment.TickCount - Starttime;
                    if (Timeout > 1000)
                    {
                        listBox1.Items.Add("time Out!");                    break;
                    }                Socket.Close();            }
            }
    我这样加了线程还是不行的啊
      

  8.   

    楼主,我按你的程序改写了一下,可以基本实现功能.希望对你有帮助.(界面设计应该跟你的一样,一个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 "";
            }
           未完……见下贴
      

  9.   

    public static Int32 Serialize(IcmpPacket packet, Byte[] Buffer, Int32 PacketSize, Int32 PingData)
            {
                Int32 cbReturn = 0;
                // serialize the struct into the array
                int Index = 0;
                Byte[] b_type = new Byte[1];
                b_type[0] = (packet.Type);
                Byte[] b_code = new Byte[1];
                b_code[0] = (packet.SubCode);
                Byte[] b_cksum = BitConverter.GetBytes(packet.CheckSum);
                Byte[] b_id = BitConverter.GetBytes(packet.Identifier);
                Byte[] b_seq = BitConverter.GetBytes(packet.SequenceNumber);
                Array.Copy(b_type, 0, Buffer, Index, b_type.Length);
                Index += b_type.Length;
                Array.Copy(b_code, 0, Buffer, Index, b_code.Length);
                Index += b_code.Length;
                Array.Copy(b_cksum, 0, Buffer, Index, b_cksum.Length);
                Index += b_cksum.Length;
                Array.Copy(b_id, 0, Buffer, Index, b_id.Length);
                Index += b_id.Length;
                Array.Copy(b_seq, 0, Buffer, Index, b_seq.Length);
                Index += b_seq.Length;
                // copy the data
                Array.Copy(packet.Data, 0, Buffer, Index, PingData);
                Index += PingData;
                if (Index != PacketSize/* sizeof(IcmpPacket) */)
                {
                    cbReturn = -1;
                    return cbReturn;
                }
                cbReturn = Index;
                return cbReturn;        }        private void button1_Click(object sender, EventArgs e)
            {
                Form1 f=new Form1();
                string MyUrl = textBox1.Text;
                listBox1.Items.Add("正在 Ping " + MyUrl + " ……");
                for (int i = 0; i < 3; i++)
                {
                    listBox1.Items.Add(f.ping(MyUrl));
                }
            }    }
          
        public class IcmpPacket
        {        public Byte Type; // type of message
            public Byte SubCode; // type of sub code
            public UInt16 CheckSum; // ones complement checksum of struct
            public UInt16 Identifier; // identifier
            public UInt16 SequenceNumber; // sequence number
            public Byte[] Data;
            public  UInt16 checksum( UInt16[] buffer, int size )
            {
                Int32 cksum = 0;
                int counter=0;
                while ( size > 0 )
                {
                    UInt16 val = buffer[counter];
                    cksum += Convert.ToInt32( buffer[counter] );
                    counter += 1;
                    size -= 1;
                }
                cksum = (cksum >> 16) + (cksum & 0xffff);
                cksum += (cksum >> 16);
                return (UInt16)(~cksum);
            }
           
        }
    }
      

  10.   

    blessyou312() 
    .............
    谢谢了,你的思路真好。将线程的问题避免了。不过这样就不能连续的ping了。
    很谢谢
      

  11.   

    比对了半天,终于发现LZ问题所在了.
    你的checksum函数:
    cksum = (cksum >> 16) + (cksum & 0xfff);  //0xfff? 少了一个f呢!!!.
    偶程序看不懂,单步调试出来的. :(