2016-09-15 87 views
0

我想使用以下端口从下面的IP地址接收数据包。问题出在“client.Receive(ref localEp);”之后这行代码不运行,也无法调试。无法调试UDPclient客户端接收

UdpClient client = new UdpClient(); 

IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 17000); 
client.Client.Bind(localEp); 

IPAddress multicastaddress = IPAddress.Parse("224.0.0.10"); 
client.JoinMulticastGroup(multicastaddress); 

while (true) 
{ 
    Byte[] data = client.Receive(ref localEp); 
    string strData = Encoding.UTF8.GetString(data); 
    Console.WriteLine(strData); 
} 

我也得到这个异常ScopeId = 'localEp.Address.ScopeId' 在IPEndPoint localEp投掷型 'System.Net.Sockets.SocketException' 的一个例外。请帮助纠正我的代码

UPDATE解决方法是,在我的机器上安装了HYPER-V虚拟机安装程序,该安装程序限制了此udp数据的接收。我只是禁用它,并开始接收数据。说实话,我不知道它的行为。

+0

也许问题是端口17000尝试其他端口。 –

+0

我检查了一些其他值和默认值0,同样的问题! :( –

回答

1

首先尝试MSDN方式从本地主机从任何来源获得数据,然后从其他主机名:

https://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient(v=vs.110).aspx

// This constructor arbitrarily assigns the local port number. 
    UdpClient udpClient = new UdpClient(17000); 

    udpClient.Connect("127.0.0.1", 17000); 

    // Sends a message to the host to which you have connected. 
    Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?"); 

    udpClient.Send(sendBytes, sendBytes.Length); 

    //// Sends a message to a different host using optional hostname and port parameters. 
    //UdpClient udpClientB = new UdpClient(); 
    //udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 17000); 

    //IPEndPoint object will allow us to read datagrams sent from any source. 
    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); 

    // Blocks until a message returns on this socket from a remote host. 
    Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); 
    string returnData = Encoding.ASCII.GetString(receiveBytes); 
+0

工作正常与本地主机。我猜问题是与服务器,这是不发送任何数据。顺便说一句,感谢您的答案 –

+0

它与从本地主机服务器接收数据? –

+0

是的,工作,即使当Hyper -V已启用 –