UDP Date Time Client/Server
Add Comment| Download | SDK |
| udpdatetime.zip (8kb) | Beta2 |
Introduction
There have been a lot of requests lately to create a networking
client/server example that uses the UDP protocol 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.
UDP Protocol
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.
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, 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).
Screen Shots
| UDP Date Time Server | UDP Date Time Client |
![]() |
![]() |
Code
1) udpServer.cs - The UDP Date Time Server
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; //Port for the Server to use
private int ip = 127001;//IP Address 127.0.0.1
private TextBox logBox;
[STAThread]
public static void Main()
{
Application.Run(new UdpDateTimeServer());
}
public override void Dispose()
{
base.Dispose();
}
//Method called when the Windows Form is Closed
public void UdpDateTimeServer_Closed(object sender, EventArgs e)
{
//Close the Socket - Never forget this or your
//Server will remain in memory even if the
//Application is terminated
server.Close();
}
//Constructor
public UdpDateTimeServer()
{
//TextBox settings
logBox = new TextBox();
logBox.Multiline = true;
logBox.ScrollBars = ScrollBars.Vertical;
logBox.ReadOnly=true;
logBox.Dock = DockStyle.Fill;
this.Controls.Add(logBox);
this.Text ="Udp Date/Time Server";
//Wire-up the EventHandler
this.Closed+=new System.EventHandler(UdpDateTimeServer_Closed);
//Create the UdpClient
server = new UdpClient(port);
//Define a Receive point
receivePoint = new IPEndPoint(new IPAddress(ip),port);
//Declare a Thread
Thread startServer = new Thread(new ThreadStart(start_server));
//Start the Thread
startServer.Start();
}
public void start_server()
{
logBox.Text+="Server Started \r\n";
//Infinite loop
while(true)
{
//Receive DataGram
byte[] recData = server.Receive(ref receivePoint);
logBox.Text+="Packet Received!!\r\n";
System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
//Split it up
string[] temp = encode.GetString(recData).Split(new Char[] {'@'});
logBox.Text+="From :"+temp[0];
logBox.Text+="\r\nPort :"+temp[1];
logBox.Text+="\r\nContent :"+temp[2];
//Re-send the DataGram
byte[] sendData =encode.GetBytes(System.DateTime.Now.ToString());
logBox.Text+="\r\nSending current Date Time...\r\n";
//We use the IP and Port sent by the user to send the DataGram back
server.Send(sendData,sendData.Length,temp[0],Int32.Parse(temp[1]));
}
}
}
} |
2) udpClient.cs - The UDP Date Time Client
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; //Port for the Client to use
private int ip = 127001;//IP Address 127.0.0.1
private TextBox logBox;
[STAThread]
public static void Main()
{
Application.Run(new UdpDateTimeClient());
}
public UdpDateTimeClient()
{
//TextBox Properties
logBox = new TextBox();
logBox.Multiline = true;
logBox.ScrollBars = ScrollBars.Vertical;
logBox.ReadOnly=true;
logBox.Dock = DockStyle.Fill;
this.Controls.Add(logBox);
this.Text ="Udp Date/Time Client";
//Create the UdpClient
client = new UdpClient(port);
receivePoint = new IPEndPoint(new IPAddress(ip),port);
Thread startClient = new Thread(new ThreadStart(start_client));
//Start the Thread
startClient.Start();
}
public override void Dispose()
{
base.Dispose();
}
public void start_client()
{
logBox.Text+="Connecting to Server\r\n";
//Loop Flag
bool continueLoop =true;
while(continueLoop)
{
//Send DataGram
//Format: Hostname@Post@Message
System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
string sendString="localhost@"+port.ToString()+"@Send Date Time";
byte[] sendData =encode.GetBytes(sendString);
logBox.Text+="\r\nRetriving current Date Time...";
//Send to Server
client.Send(sendData,sendData.Length,"localhost",6767);
//Receive DataGram from Server
byte[] recData = client.Receive(ref receivePoint);
logBox.Text+="Packet Received!!\r\n";
logBox.Text+="DateTime :"+encode.GetString(recData);
//Close Connection
client.Close();
logBox.Text+="\r\nConnection Closed!!" ;
//End Loop
continueLoop=false;
}
}
}
} |
Conclusion
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!



