您的位置:程序门 -> .net技术 -> c#



用c#写了一个图形化ping,但执行后输入ip点ping按钮后就没有任何反映,好像程序死了一样,急急急!!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用c#写了一个图形化ping,但执行后输入ip点ping按钮后就没有任何反映,好像程序死了一样,急急急!![已结贴,结贴人:myplace]
发表于:2007-01-12 21:41:12 楼主
我的图形是这样的: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);
                }
        }
发表于:2007-01-12 21:56:071楼 得分:0
这是我们课程设计的题目,达人救命啊!!!
发表于:2007-01-12 22:35:402楼 得分:0
原因很简单,你没有使用线程,thread应该去看看。当你点击按钮时应该新建一个线程来进行ping,要结束时直接结束线程即可。

发表于:2007-01-12 22:47:503楼 得分:0
我没有看过进程的,应该怎么加一下啊!
谢谢提醒!
发表于:2007-01-12 22:52:064楼 得分:0
我没有看过进程的,应该怎么加一下啊!
谢谢提醒!
==================================
线程!请参考thread类..
发表于:2007-01-12 23:33:585楼 得分:0
用自己用icmp报文么,直接调用cmd.exe   ping就可以
用process对象
发表于:2007-01-13 09:23:156楼 得分:0
这是我们计算机网络的课程设计,自己重写ping命令,不能用系统的ping.怎么在那个程序里加线程啊??
发表于:2007-01-13 09:28:427楼 得分:0
greennetboy(我的老婆叫静静)   (   )   信誉:100         blog     2007-1-12   23:33:59     得分:   0    
 
 
     
用自己用icmp报文么,直接调用cmd.exe   ping就可以
用process对象
---------------
这个家伙说的对!
   
 
发表于:2007-01-13 10:00:088楼 得分:0
老师不让这么干的,老师就是会折磨人的啊!!
发表于:2007-01-13 10:54:339楼 得分:0
多线程\backworkthread解决
发表于:2007-01-13 10:59:3610楼 得分:0
能说详细一点吗??
发表于:2007-01-13 11:24:0511楼 得分:0
应该使用线程
发表于:2007-01-13 12:07:4512楼 得分:0
1)使用线程
2)设置发送、接受超时值
发表于:2007-01-13 13:03:1013楼 得分:0
使用backgroundwalker解决问题
发表于:2007-01-13 13:18:2314楼 得分:0
使用线程.
thread类
发表于:2007-01-13 14:25:2515楼 得分:0
我不知道怎么加啊!!
发表于:2007-01-13 14:48:0316楼 得分:0
.net类库中实现了一个ping   好像是2.0中加的   呵呵
发表于:2007-01-13 15:00:5217楼 得分:0
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();

                        }
                }
我这样加了线程还是不行的啊
发表于:2007-01-13 15:01:2318楼 得分:0
是不是程序本身调用就有问题啊??
发表于:2007-01-13 16:35:0419楼 得分:0
up
发表于:2007-01-14 11:05:3020楼 得分:20
楼主,我按你的程序改写了一下,可以基本实现功能.希望对你有帮助.(界面设计应该跟你的一样,一个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   " ";
                }
              未完……见下贴
发表于:2007-01-14 11:06:2321楼 得分:0
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);
                }
             
        }
}

发表于:2007-01-14 15:39:1422楼 得分:0
sign
发表于:2007-01-14 17:48:2923楼 得分:0
blessyou312()  
.............
谢谢了,你的思路真好。将线程的问题避免了。不过这样就不能连续的ping了。
很谢谢
发表于:2007-01-14 19:39:0624楼 得分:0
比对了半天,终于发现lz问题所在了.
你的checksum函数:
cksum   =   (cksum   > >   16)   +   (cksum   &   0xfff);     //0xfff?   少了一个f呢!!!.
偶程序看不懂,单步调试出来的.   :(
发表于:2007-01-14 21:39:5725楼 得分:0
jf1840()简直是太帅了,真的是这样!!!
可是我的分已经给了,谢谢你啊!!你真是拽呆了!!!


快速检索

最新资讯