繁体   English   中英

在发送数据之前,C#UDP套接字不会接收数据

[英]C# UDP Socket doesn't receive data until after data is sent

我在使用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(...);

没关系,修好了

故事的寓意是---总是检查防火墙是否启用...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM