开发者

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

开发者 https://www.devze.com 2023-02-13 02:44 出处:网络
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 s

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 
           开发者_Python百科 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...

0

精彩评论

暂无评论...
验证码 换一张
取 消