Click here to Skip to main content
11,639,922 members (69,943 online)
Click here to Skip to main content

TinyUDP - Simple UDP Client/Server Components for .NET

, 7 Jun 2005 227.2K 8K 57
Rate this:
Please Sign up or sign in to vote.
Simple UDP client/server components for .NET 1.1.

Introduction

This article was written in the course of implementing a simple method for a Windows service to communicate status information to a front-end administration application. This article was written using the excellent article UDP send and receive using threads in VB.NET by Kumudu Gunasekara for inspiration and 'spiritual guidance', as well as some nifty ideas. Kumudu's article is definitely worth reading.

Background

I decided to implement a simple UDP client/server system for simple communication between my Windows service and the administration program for a variety of reasons:

  1. UDP is lightweight and fast compared to other protocols, such as TCP. This was important to me, as my Windows service needed to maintain high performance.
  2. UDP is connectionless, which means that it can fire off a message and immediately free server-side network resources. Incidentally, this also makes UDP one of the easiest protocols to write a client/server application for.
  3. UDP preserves message boundaries transmitting entire messages at once.

Some of the drawbacks of UDP are:

  1. UDP does not guarantee delivery or provide for acknowledgment of receipt of messages. In this instance, that was perfectly acceptable since I was simply passing the status data from the Windows service to the UI layer.
  2. UDP has practical limits on how much data can be sent in a single message - usually limited to 1500 Bytes per message or less. Again, this limitation worked well given my scenario - I would normally not be passing more than 100 bytes per message.

Using the code

The code is pretty straightforward and well documented. There are two components: Tiny.UDP.TinyServer and Tiny.UDP.TinyClient. They are .NET 1.1 components, so they can be added to your Toolbox and dropped right in your Windows Forms; or you can reference them (as I did in the sample applications) by creating objects in the code. The code to create a server looks like this:

' Initialize a TinyServer Object
Dim server As New TinyServer

' Set it for UDP and assign the destination end point
server.Protocol = ProtocolType.Udp
server.ClientAddress = IPAddress.Parse("127.0.0.1")
server.ClientPort = 8088

' Set the Encoding Type and Send the Message
server.Encode = EncodingType.ASCII

All we're doing here is:

  1. Creating the TinyServer object.
  2. Setting the communications protocol (currently only UDP is supported), client IP address and client port.
  3. And finally, setting the message encoding type (ASCII, UNICODE, UTF7, UTF8 and all others are supported).

Next we set up the TinyClient:

' Define a TinyClient object
Dim WithEvents client As TinyClient

' Create a client object
client = New TinyClient

' Set the inbound client port
client.ClientPort = 8088

' Set the encoding type and protocol
client.Encode = EncodingType.ASCII
client.Protocol = ProtocolType.Udp

Again, it's a simple matter to set up a client:

  1. Define and create a new TinyClient object. Notice it is declared WithEvents.
  2. Set the inbound client port number.
  3. Set the encoding type and protocol to match the TinyServer that we created.

That's a simple UDP client and server with just 10 lines of code! (Not counting comments of course.) Finally, we start the client running with the Start() method:

client.Start()

and send messages from the server with the SendMessage() method:

server.SendMessage ("This is a UDP Message sent by TinyServer.")

The TinyClient object was declared WithEvents because it implements two events: BeforeReceive and AfterReceive. BeforeReceive is fired immediately before receiving data off the wire. AfterReceive is fired immediately after the client finishes receiving data. You can set up your own subroutines to handle these events as you see fit. Other events may be added later; but for now I decided those two were the most important to get implemented.

How does it work?

If you look into the code, you will see that the TinyServer simply creates a System.Net.Sockets.UdpClient, opens a connection, and fires off your message when you call the SendMessage() method. Very simple. The client is where the fun begins.

On the client side, the Start() method actually fires off a worker thread that in turn starts its own System.Net.Sockets.UdpClient and waits for data. We set it off on a separate worker thread because the Receive() method blocks the current thread until it completes - and we don't need it locking up our UI. Once a message is received by the worker thread, the thread exits immediately, so we start a new worker thread to continue waiting for the next message.

Because these events are fired from the worker thread, you'll notice in the sample program that I check Me.InvokeRequired on the client form before I update it. This is necessary because of the nature of multi-threaded apps; never update the UI from a worker thread, always check InvokeRequired and use Invoke if it is True.

Finally, when we finish, fire the client.Stop() method to stop the worker thread and cleanly dispose off the UDPClient.

Points of interest

As mentioned previously, this works best on a local machine or on locally networked computers; for instance, when communicating non-critical information between layers of a locally installed application. It also has the potential for chat applications, which I'm sure has been done to death by now Smile | :) My main goal in designing this was to create a reusable, lightweight set of components that could be used to allow Windows services to easily communicate with the front-end client applications without jumping through hoops.

Enjoy!

History

  • June 8th, 2005 - version 0.8.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

tupacs01
Web Developer
United States United States
Database Admin (MCDBA) and Programmer with 15+ years' experience in data warehousing, application design and development, and systems integration.

You may also be interested in...

Comments and Discussions

 
QuestionColls Pin
Nattawutxp11-Sep-14 22:27
memberNattawutxp11-Sep-14 22:27 
QuestionClient Receiving Blank Message Pin
Member 977465013-Jul-13 13:09
memberMember 977465013-Jul-13 13:09 
Generalany trouble with VS2010 ?? Pin
SpaceWalker5-Jul-12 2:14
memberSpaceWalker5-Jul-12 2:14 
GeneralRe: any trouble with VS2010 ?? Pin
manekurt4-Sep-12 12:52
membermanekurt4-Sep-12 12:52 
QuestionCan't receive message Pin
Al Najjar21-Dec-11 15:14
memberAl Najjar21-Dec-11 15:14 
AnswerRe: Can't receive message Pin
Filip Mestric2-Feb-12 9:55
memberFilip Mestric2-Feb-12 9:55 
GeneralRe: Can't receive message Pin
SpaceWalker6-Jul-12 6:16
memberSpaceWalker6-Jul-12 6:16 
Questionmake receiver in asp.net Pin
davismu1-Jul-11 19:54
memberdavismu1-Jul-11 19:54 
Generalwhen to call the InitializeThread() Pin
KHelmi2-Jun-10 9:00
memberKHelmi2-Jun-10 9:00 
GeneralRe: when to call the InitializeThread() Pin
Member 84109234-Feb-13 11:35
memberMember 84109234-Feb-13 11:35 
QuestionWhat kind of license has this code? Pin
unican9-Dec-08 10:36
memberunican9-Dec-08 10:36 
Questionhow to receive msg when receiver is behind router Pin
Member 468623911-Feb-08 16:09
memberMember 468623911-Feb-08 16:09 
GeneralTinyUDP seriously flawed Pin
marantz14-Nov-07 6:41
membermarantz14-Nov-07 6:41 
Generaludp socket to accept data and write it to a db Pin
daver564-Jul-07 4:18
memberdaver564-Jul-07 4:18 
GeneralUDP over GPRS (push to client) Pin
djgann17-Apr-07 12:33
memberdjgann17-Apr-07 12:33 
GeneralRe: UDP over GPRS (push to client) Pin
Member 292441819-Mar-09 17:15
memberMember 292441819-Mar-09 17:15 
GeneralThanks for the complements Pin
Kumudu Gunasekara24-Mar-07 17:32
memberKumudu Gunasekara24-Mar-07 17:32 
GeneralI need a source code that view installed applications and run them. Pin
MjRazzaghi2-Jan-07 4:22
memberMjRazzaghi2-Jan-07 4:22 
Questionhelp Pin
Gigi Carlino26-Oct-06 6:14
memberGigi Carlino26-Oct-06 6:14 
GeneralHelp ... Create new Form from worker thread Pin
ryan@theglenns.co.za19-Sep-06 23:26
memberryan@theglenns.co.za19-Sep-06 23:26 
GeneralMemory Usage in Task Manager Pin
brembot2-Aug-06 5:41
memberbrembot2-Aug-06 5:41 
GeneralHELP I NEED A GOOD UDP SOURCE Pin
Fulmine23-Jan-06 4:50
memberFulmine23-Jan-06 4:50 
GeneralProgram not closing; tried lots of things... Pin
daniellus3-Jan-06 12:29
memberdaniellus3-Jan-06 12:29 
GeneralRe: Program not closing; tried lots of things... Pin
NairbNilpop1-Feb-06 16:30
memberNairbNilpop1-Feb-06 16:30 
GeneralRe: Program not closing; tried lots of things... Pin
awdrgftdesq19-Sep-06 5:32
memberawdrgftdesq19-Sep-06 5:32 
GeneralMultithreading start/stop question Pin
davidm991-Dec-05 10:09
memberdavidm991-Dec-05 10:09 
QuestionInvoke Pin
Lost-Ha[n]f-PHP24-Nov-05 1:31
memberLost-Ha[n]f-PHP24-Nov-05 1:31 
AnswerRe: Invoke Pin
tupacs0128-Nov-05 3:36
membertupacs0128-Nov-05 3:36 
GeneralConfusing naming convention Pin
meraydin15-Nov-05 10:25
membermeraydin15-Nov-05 10:25 
GeneralRe: Confusing naming convention Pin
tupacs0128-Nov-05 3:40
membertupacs0128-Nov-05 3:40 
GeneralRe: Fix for naming convention and exit bug Pin
Danilo Corallo20-Feb-07 21:45
memberDanilo Corallo20-Feb-07 21:45 
GeneralProgram Continues To Run After Close Pin
Duckdoo14-Nov-05 3:57
memberDuckdoo14-Nov-05 3:57 
GeneralRe: Program Continues To Run After Close Pin
davidm9915-Nov-05 9:57
memberdavidm9915-Nov-05 9:57 
GeneralRe: Program Continues To Run After Close Pin
tupacs0128-Nov-05 3:58
membertupacs0128-Nov-05 3:58 
GeneralSetting Origin Port Pin
davidm998-Nov-05 11:04
memberdavidm998-Nov-05 11:04 
GeneralRe: Setting Origin Port Pin
davidm9915-Nov-05 10:04
memberdavidm9915-Nov-05 10:04 
GeneralRe: Setting Origin Port Pin
davidm9915-Nov-05 10:05
memberdavidm9915-Nov-05 10:05 
GeneralRe: Setting Origin Port Pin
tupacs0128-Nov-05 3:43
membertupacs0128-Nov-05 3:43 
GeneralIP address of packet sender Pin
davidm995-Nov-05 6:21
memberdavidm995-Nov-05 6:21 
GeneralRe: IP address of packet sender Pin
davidm995-Nov-05 6:26
memberdavidm995-Nov-05 6:26 
QuestionMulticast Receiver... Pin
dcarlton28-Sep-05 1:56
sussdcarlton28-Sep-05 1:56 
AnswerRe: Multicast Receiver... Pin
tupacs0128-Nov-05 3:56
membertupacs0128-Nov-05 3:56 
Generalrunning an windows application as a clent server applicaion Pin
behzad608125-Sep-05 20:47
memberbehzad608125-Sep-05 20:47 
GeneralRe: running an windows application as a clent server applicaion Pin
tupacs0126-Sep-05 12:04
membertupacs0126-Sep-05 12:04 
GeneralA minor point that I found. Pin
peter82822-Jul-05 12:24
memberpeter82822-Jul-05 12:24 
AnswerRe: A minor point that I found. Pin
studiocharlotte17-Jul-07 1:32
memberstudiocharlotte17-Jul-07 1:32 
GeneralThanks Pin
peter82822-Jul-05 12:13
memberpeter82822-Jul-05 12:13 
GeneralThoughts about your chosen transport protocol Pin
BooGhost14-Jun-05 12:31
memberBooGhost14-Jun-05 12:31 
GeneralRe: Thoughts about your chosen transport protocol Pin
tupacs0114-Jun-05 15:16
membertupacs0114-Jun-05 15:16 
GeneralPerfect! Pin
angrydba13-Jun-05 8:46
memberangrydba13-Jun-05 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 8 Jun 2005
Article Copyright 2005 by tupacs01
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid