Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to receive UDP Broadcasts IN A LAN
This is my code:

private void timer1_Tick(object sender, EventArgs e)
        {
            EndPoint iep = new IPEndPoint(IPAddress.Any, 0);
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 11000);
            listener.Bind(localEndPoint);
            EndPoint ep = (EndPoint)iep;
            this.Text = "Ready to receive…";
            byte[] data = new byte[1024];
            int recv = listener.ReceiveFrom(data, ref ep);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            
            listBox1.Items.Add(stringData);
            listener.Close();
        }


Now what happens is due to the Timer, my application freezes
though it receives the broadcasts and adds the data to the list box.
Is there any way that i can make sure that the UDP broadcasts are still received, at the same time doesn't freeze the application?
Posted
Updated 20-Oct-10 5:26am
v2

1 solution

Perhaps you could create a separate thread for the listener/broadcast handler code so that it's not blocking the UI thread?
 
Share this answer
 
Comments
Sandeep Mewara 20-Oct-10 13:07pm    
Comment from OP: Ya i tried creating a separate thread, but seems its not working.
Can u give me an example.
Because the Listener code should be able to pick up
broadcasts whenever there is a broadcast sent.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900