CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  VB.NET

为什么msdn提供的这个程序执行出错呢?

楼主mch885(叭叭呜)2005-08-01 22:18:50 在 .NET技术 / VB.NET 提问

我想写一个截获udp报的程序,可是下面的这个程序却总是出错,请帮我看看问题出在哪里  
  以下是msdn的源程序:  
  Visual   Basic]    
  'Creates   a   UdpClient   for   reading   incoming   data.  
  Dim   receivingUdpClient   As   New   UdpClient()  
   
  'Creates   an   IPEndPoint   to   record   the   IP   address   and   port   number   of   the   sender.    
  '   The   IPEndPoint   will   allow   you   to   read   datagrams   sent   from   any   source.  
  Dim   RemoteIpEndPoint   As   New   IPEndPoint(IPAddress.Any,   0)  
  Try  
         
        '   Blocks   until   a   message   returns   on   this   socket   from   a   remote   host.  
        Dim   receiveBytes   As   [Byte]()   =   receivingUdpClient.Receive(RemoteIpEndPoint)  
         
        Dim   returnData   As   String   =   Encoding.ASCII.GetString(receiveBytes)  
         
        Console.WriteLine(("This   is   the   message   you   received   "   +   returnData.ToString()))  
        Console.WriteLine(("This   message   was   sent   from   "   +   RemoteIpEndPoint.Address.ToString()   +   "   on   their   port   number   "   +   RemoteIpEndPoint.Port.ToString()))  
  Catch   e   As   Exception  
        Console.WriteLine(e.ToString())  
  End   Try  
        End   Sub   'MyUdpClientCommunicator  
   
  [C#]    
  //Creates   a   UdpClient   for   reading   incoming   data.  
  UdpClient   receivingUdpClient   =   new   UdpClient();  
   
  //Creates   an   IPEndPoint   to   record   the   IP   Address   and   port   number   of   the   sender.    
      //   The   IPEndPoint   will   allow   you   to   read   datagrams   sent   from   any   source.  
  IPEndPoint   RemoteIpEndPoint   =   new   IPEndPoint(IPAddress.Any,   0);  
  try{  
   
          //   Blocks   until   a   message   returns   on   this   socket   from   a   remote   host.  
          Byte[]   receiveBytes   =   receivingUdpClient.Receive(ref   RemoteIpEndPoint);    
   
          string   returnData   =   Encoding.ASCII.GetString(receiveBytes);  
   
          Console.WriteLine("This   is   the   message   you   received   "   +  
                                                                    returnData.ToString());  
          Console.WriteLine("This   message   was   sent   from   "   +  
                                                                  RemoteIpEndPoint.Address.ToString()   +  
                                                                  "   on   their   port   number   "   +  
                                                                  RemoteIpEndPoint.Port.ToString());  
  }  
  catch   (   Exception   e   ){  
          Console.WriteLine(e.ToString());    
  }  
  问题点数:20、回复次数:5Top

1 楼mch885(叭叭呜)回复于 2005-08-02 08:25:19 得分 0

为什么没有人回答我呢?555Top

2 楼mch885(叭叭呜)回复于 2005-08-02 08:37:00 得分 0

自己顶Top

3 楼sx_lxh(路漫漫)回复于 2005-08-02 08:43:26 得分 0

msdn提供的这个程序不是一个完整的工程,只是一部分示例。Top

4 楼mch885(叭叭呜)回复于 2005-08-02 08:51:58 得分 0

这样的算全吗?可是也是出错,不知道毛病出在哪里。我只是想知道怎样才能收到任何传进来的udp报?  
   
  Imports   System.Net  
  Imports   System.Threading  
  Imports   System.Text  
  Imports   System.Net.Sockets  
  Module   Module1  
   
          Sub   Main()  
                  'Creates   a   UdpClient   for   reading   incoming   data.  
                  Dim   receivingUdpClient   As   New   UdpClient  
   
                  'Creates   an   IPEndPoint   to   record   the   IP   address   and   port   number   of   the   sender.    
                  '   The   IPEndPoint   will   allow   you   to   read   datagrams   sent   from   any   source.  
                  Dim   RemoteIpEndPoint   As   New   IPEndPoint(IPAddress.Any,   0)  
                  Try  
   
                          '   Blocks   until   a   message   returns   on   this   socket   from   a   remote   host.  
                          Dim   receiveBytes   As   [Byte]()   =   receivingUdpClient.Receive(RemoteIpEndPoint)  
   
                          Dim   returnData   As   String   =   Encoding.ASCII.GetString(receiveBytes)  
   
                          Console.WriteLine(("This   is   the   message   you   received   "   +   returnData.ToString()))  
                          Console.WriteLine(("This   message   was   sent   from   "   +   RemoteIpEndPoint.Address.ToString()   +   "   on   their   port   number   "   +   RemoteIpEndPoint.Port.ToString()))  
                  Catch   e   As   Exception  
                          Console.WriteLine(e.ToString())  
                  End   Try  
   
          End   Sub  
   
  End   ModuleTop

5 楼mch885(叭叭呜)回复于 2005-08-02 11:01:27 得分 0

upTop

相关问题

  • 编译后的程序执行出错。
  • 程序执行出错,求助!!!
  • 为什么我创建可执行程序时出错?
  • 程序打包以后,再执行出错
  • 程序执行出错!!!!我不知道为什么???
  • 程序执行总出错,帮我看看哪有问题啊
  • 安装asp.net环境后执行aspx程序出错的问题
  • 程序执行时出现某一地址访问出错
  • 请问下这段程序为什么回执行时出错
  • 一个C++程序执行出错,请帮看一下!

关键词

  • .net
  • msdn
  • remoteipendpoint
  • receivebytes
  • receivingudpclient
  • 出错
  • returndata
  • ipendpoint
  • udpclient
  • 程序

得分解答快速导航

  • 帖主:mch885

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo