[英]C# UDP Socket doesn't receive data until after data is sent
I'm having trouble with a socket that I'm using to send UDP data in C# --- basically the socket isn't receiving any data from other computers on the network until some data has been sent over the socket first. 我在使用C#发送UDP数据的套接字时遇到问题 - 基本上套接字没有从网络上的其他计算机接收任何数据,直到首先通过套接字发送了一些数据。
Not really sure why it's behaving strangely, I was hoping you guys could point me in the right direction -- code sample is below. 不确定为什么它表现得很奇怪,我希望你们能指出我正确的方向 - 代码示例如下。
Thanks 谢谢
-- Dan - 丹
private Socket UDPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress bindIP = BindingIP;
if (bindIP == IPAddress.None)
throw new Exception("Error binding to network interface \"" + _mcinter + "\" - interface not found");
//recieve data from any source
IPEndPoint LocalHostIPEnd = new IPEndPoint(bindIP, Target_Port);
//init Socket properties:
UDPSocket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, 1);
//allow for loopback testing
UDPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
//extremly important to bind the Socket before joining multicast groups
UDPSocket.Bind(LocalHostIPEnd);
//get in waiting mode for data - always (this doesn't halt code execution)
UDPSocket.BeginReceiveFrom(...);
Nevermind, fixed 没关系,修好了
The moral of the story is --- always check to see if the firewall is enabled... 故事的寓意是---总是检查防火墙是否启用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.