为什么msdn提供的这个程序执行出错呢?
我想写一个截获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());
}