C# UDP Socket 在发送数据之前不会接收数据
我在使用 C# 中发送 UDP 数据的套接字时遇到问题 --- 基本上,在先通过套接字发送一些数据之前,套接字不会从网络上的其他计算机接收任何数据。
不太确定为什么它的行为很奇怪,我希望你们能给我指出正确的方向——代码示例如下。
谢谢
——丹
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(...);
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.
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(...);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,已修复
这个故事的寓意是——始终检查防火墙是否已启用......
Nevermind, fixed
The moral of the story is --- always check to see if the firewall is enabled...