简体   繁体   中英

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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