Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


UDP Date Time Client/Server

Add Comment
 

 
<div align="center"> <table width="90%" class="outline"> <tr> <td width="50%" class="outline"><b>Download</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"> <a class="wbox" href="../../file/udpdatetime.zip">udpdatetime.zip</a> (8kb)</td> <td width="50%" class="outline">Beta2</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> There have been a lot of requests lately to create a networking client/server example that uses the <b>UDP protocol</b> for data transfer. So finally I have put together an example for you people. Making nothing very fancy this is a simple client/server example, where multiple clients can connect to the server and request the current Date and Time. The multi-threaded server waits for each client and responds to them with the current date and time.</p> <p><span class=wboxheado>UDP Protocol</span><br> As many of you might be knowing that the UDP protocol is very different from the TCP protocol. In the TCP protocol the server and the client have a flowing constant connection between them and they communicate in the form for byte streams. The TCP protocol is a bit more robust and guarantees that your message will be delivered to the other end. <br> UDP protocol is different from this, here there is no connection between the two endpoints and the communication takes place in the form of packets of data called Datagram. Also UDP does not guarantee the delivery of your message to the endpoint (so think carefully before you use this protocol). Since there is no connection between the two end points, its very difficult for the server to know from where the Datagram came,&nbsp; hence you have to explicitly tell the server the address of the client so that it can send its response at the client mentioned address (IP Address and Port Number).</p> <p><span class=wboxheado>Screen Shots</span></p> <table width="100%" class="outline"> <tr> <td width="50%" class="outline" align="center"><b>UDP Date Time Server</b></td> <td width="50%" class="outline" align="center"><b>UDP Date Time Client</b></td> </tr> <tr> <td width="50%" class="outline" align="center"> <img border="0" src="../../img/udpdatetimeserver.gif" width="300" height="300"></td> <td width="50%" class="outline" align="center"> <img border="0" src="../../img/udpdatetimeclient.gif" width="300" height="300"></td> </tr> </table> <p><span class=wboxheado>Code</span><br> <b>1) udpServer.cs -</b> The UDP Date Time Server<table width="100%" class="code"> <tr> <td width="100%"><pre>using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Windows.Forms; using System.ComponentModel; namespace MasterCSharp.WebSite.Saurabh.Networking { public class UdpDateTimeServer:Form { private UdpClient server; private IPEndPoint receivePoint; private int port = 6767; <span class=cmt>//Port for the Server to use</span> private int ip = 127001;<span class=cmt>//IP Address 127.0.0.1</span> private TextBox logBox; [STAThread] public static void Main() { Application.Run(new UdpDateTimeServer()); } public override void Dispose() { base.Dispose(); } <span class=cmt>//Method called when the Windows Form is Closed</span> public void UdpDateTimeServer_Closed(object sender, EventArgs e) { <span class=cmt>//Close the Socket - Never forget this or your //Server will remain in memory even if the //Application is terminated</span> server.Close(); } <span class=cmt>//Constructor</span> public UdpDateTimeServer() { <span class=cmt>//TextBox settings</span> logBox = new TextBox(); logBox.Multiline = true; logBox.ScrollBars = ScrollBars.Vertical; logBox.ReadOnly=true; logBox.Dock = DockStyle.Fill; this.Controls.Add(logBox); this.Text =&quot;Udp Date/Time Server&quot;; <span class=cmt>//Wire-up the EventHandler</span> this.Closed+=new System.EventHandler(UdpDateTimeServer_Closed); <span class=cmt>//Create the UdpClient</span> server = new UdpClient(port); <span class=cmt>//Define a Receive point</span> receivePoint = new IPEndPoint(new IPAddress(ip),port); <span class=cmt>//Declare a Thread</span> Thread startServer = new Thread(new ThreadStart(start_server)); <span class=cmt>//Start the Thread</span> startServer.Start(); } public void start_server() { logBox.Text+=&quot;Server Started \r\n&quot;; <span class=cmt>//Infinite loop</span> while(true) { <span class=cmt>//Receive DataGram</span> byte[] recData = server.Receive(ref receivePoint); logBox.Text+=&quot;Packet Received!!\r\n&quot;; System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding(); <span class=cmt>//Split it up</span> string[] temp = encode.GetString(recData).Split(new Char[] {'@'}); logBox.Text+=&quot;From :&quot;+temp[0]; logBox.Text+=&quot;\r\nPort :&quot;+temp[1]; logBox.Text+=&quot;\r\nContent :&quot;+temp[2]; <span class=cmt>//Re-send the DataGram</span> byte[] sendData =encode.GetBytes(System.DateTime.Now.ToString()); logBox.Text+=&quot;\r\nSending current Date Time...\r\n&quot;; <span class=cmt>//We use the IP and Port sent by the user to send the DataGram back</span> server.Send(sendData,sendData.Length,temp[0],Int32.Parse(temp[1])); } } } }</pre></td> </tr> </table> <p>&nbsp;<p><b>2) udpClient.cs </b>- The UDP Date Time Client<table border="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" class="code"> <tr> <td width="100%"><pre>using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Windows.Forms; namespace MasterCSharp.WebSite.Saurabh.Networking { public class UdpDateTimeClient:Form { private UdpClient client; private IPEndPoint receivePoint; private int port = 6060; <span class=cmt>//Port for the Client to use</span> private int ip = 127001;<span class=cmt>//IP Address 127.0.0.1</span> private TextBox logBox; [STAThread] public static void Main() { Application.Run(new UdpDateTimeClient()); } public UdpDateTimeClient() { <span class=cmt>//TextBox Properties</span> logBox = new TextBox(); logBox.Multiline = true; logBox.ScrollBars = ScrollBars.Vertical; logBox.ReadOnly=true; logBox.Dock = DockStyle.Fill; this.Controls.Add(logBox); this.Text =&quot;Udp Date/Time Client&quot;; <span class=cmt>//Create the UdpClient</span> client = new UdpClient(port); receivePoint = new IPEndPoint(new IPAddress(ip),port); Thread startClient = new Thread(new ThreadStart(start_client)); <span class=cmt>//Start the Thread</span> startClient.Start(); } public override void Dispose() { base.Dispose(); } public void start_client() { logBox.Text+=&quot;Connecting to Server\r\n&quot;; <span class=cmt>//Loop Flag</span> bool continueLoop =true; while(continueLoop) { <span class=cmt>//Send DataGram //Format: Hostname@Post@Message</span> System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding(); string sendString=&quot;localhost@&quot;+port.ToString()+&quot;@Send Date Time&quot;; byte[] sendData =encode.GetBytes(sendString); logBox.Text+=&quot;\r\nRetriving current Date Time...&quot;; <span class=cmt>//Send to Server </span> client.Send(sendData,sendData.Length,&quot;localhost&quot;,6767); <span class=cmt>//Receive DataGram from Server</span> byte[] recData = client.Receive(ref receivePoint); logBox.Text+=&quot;Packet Received!!\r\n&quot;; logBox.Text+=&quot;DateTime :&quot;+encode.GetString(recData); <span class=cmt>//Close Connection</span> client.Close(); logBox.Text+=&quot;\r\nConnection Closed!!&quot; ; <span class=cmt>//End Loop</span> continueLoop=false; } } } }</pre></td> </tr> </table> <p><span class=wboxheado>Conclusion</span><br> Programming using UDP is a bit different then programming using TCP, but then there are scenario's where you just don't care if the data has been received or not; This is where UDP comes into picture. Another notable gain is that since a constant connection is not maintained a lot of bandwidth and overhead is saved!

Comments

Add Comment