C#: A multi-threaded UDP server with BackgroundWorker.

The following code shows you how to build a UDP server in C# using the UUdpClient class for the networking and BackgroundWorker class for reporting data back to the application. This particular UDP server class is designed to be used with a Windows Forms or GUI. When using the UDP server you pass a BackgroundWorker class to the constructor. You should also make sure reporting is enable on that BackgroundWorker class so that they UDP server can report data and error back to your ReportProgress even.

/*
 * Created by Jimmy Burnett (jimmyburnett.com)
 * User: Jim
 * Date: 1/1/2010
 * Time: 9:19 AM
 */
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace JimmyBurnett
{
	public class udpListener
	{
		private System.ComponentModel.BackgroundWorker workerUDP;
		private int port=5121;
		public udpListener(System.ComponentModel.BackgroundWorker workerUDP,int port)
		{
			this.workerUDP = workerUDP;
		}

		/// <summary>
		/// Start the listener.
		/// </summary>
		public void udpListenerStart()
		{
		  try{
			this.workerUDP.ReportProgress(30,"UDP Server Starting...");
			byte[] data = new byte[1024];
			//IPAddress addy = IPAddress.Parse("192.168.1.1");
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, this.port);
            UdpClient newsock = new UdpClient(ipep);
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            data = newsock.Receive(ref sender);
				//send a status back to our background worker class.
	        this.workerUDP.ReportProgress(30,"UDP Server Listening...");

			while(true)
            {
             this.workerUDP.ReportProgress(30,"Received Data..");
             this.workerUDP.ReportProgress(50,data.Length);
              data = newsock.Receive(ref sender);
         	}       

         	}catch(Exception ee){
				this.workerUDP.ReportProgress(666,ee.Message);
				Console.WriteLine(ee.Message) ;
     	 	}
	  }
    }
}


Digg: DIGG ME

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Sponsor & Advertise
Tech Buzz

Zero-Day VBScript plagues Windows XP / 2000.

Apparently there is a new zero-day flaw that affects Windows XP and 2000 computers utilizing VBScript. An attacker can trick someone into visiting a website that binds the F1 key to a VBScript event which ultimately installs malicious code on your machine. Microsoft’s fix: Don’t press the F1 key when windows pop up. LOL. Ok [...]

Read More »

Has Verizon been hacked? Security certificates revoked!

Has Verizon been hacked? Google Chrome seems to think so. Just a few minutes ago I tried to log into Verizon to see why my phone isn’t making any calls and to also see why I can’t make any text messages. I’m going to have to probably assume they haven’t been hacked, but how does [...]

Read More »

My first blocked number in Google Voice.

Today I received my first piece of spam in Google Voice. At first I was really PISSED-OFF but then a feeling of serenity passed over my whole body as I noticed the “block” button. Slowly and cautiously I clicked it, making my day THAT MUCH better. Just knowing that I will no longer be getting [...]

Read More »

3500 Netflix on Linux petitions.

Currently you can not watch Netflix if you are a Linux user and all those new Ubuntu Netbook owners will not be watching Netflix anytime soon either. Watching movies online through Netflix is an awesome service, but worthless to Linux users. Netflix has chosen to only allow Windows and MAC users access to their online [...]

Read More »

Apple bans “android” from apps store.

Apples waving the ban stick around again, this time rejecting an educational iPhone app because it contained the word “Android”. The application? Flash of Genius: SAT Vocab 2.2, an iPhone app developed by Tim Novikof. The app did really well in the Android Developer Challenge that Google puts on and decided to mention that [...]

Read More »

My .02 on Apple’s anti-flash, anti-freedom movement.

As many of you iPhone consumers may know, Flash isn’t supported and will probably never be supported. The new up and coming iPad (AppleĀ Tablet) will be running a version of the iPhone operating system and will probably have the same exact restrictions. The reason Apple doesn’t want to support Flash is because it allows third [...]

Read More »