C# UDP Socket 在发送数据之前不会接收数据

发布于 2024-10-19 01:15:29 字数 1131 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2024-10-26 01:15:29

没关系,已修复

这个故事的寓意是——始终检查防火墙是否已启用......

Nevermind, fixed

The moral of the story is --- always check to see if the firewall is enabled...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文