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

mch885 2005-08-01 10:18:50
我想写一个截获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());
}
...全文
96 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mch885 2005-08-02
  • 打赏
  • 举报
回复
up
mch885 2005-08-02
  • 打赏
  • 举报
回复
这样的算全吗?可是也是出错,不知道毛病出在哪里。我只是想知道怎样才能收到任何传进来的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 Module
sx_lxh 2005-08-02
  • 打赏
  • 举报
回复
msdn提供的这个程序不是一个完整的工程,只是一部分示例。
mch885 2005-08-02
  • 打赏
  • 举报
回复
自己顶
mch885 2005-08-02
  • 打赏
  • 举报
回复
为什么没有人回答我呢?555

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧