C# Program to Implement UDP

This C# Program Implements UDP. Here name of the employee is entered in the client side window and the job the specified employee is displayed.

Here is source code of the C# Program to Implement UDP. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2.  * C# Program to Implement UDP
  3.  */
  4.  
  5. //SERVER SIDE PROGRAM
  6.  
  7. using System;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Configuration;
  12.  
  13. class EmployeeUDPServer
  14. {
  15.     public static void Main()
  16.     {
  17.         UdpClient udpc = new UdpClient(2055);
  18.         Console.WriteLine("Server started, servicing on port 2055");
  19.         IPEndPoint ep = null;
  20.         while (true)
  21.         {
  22.             byte[] rdata = udpc.Receive(ref ep);
  23.             string name = Encoding.ASCII.GetString(rdata);
  24.             string job = ConfigurationSettings.AppSettings[name];
  25.             if (job == null) job = "No such employee";
  26.             byte[] sdata = Encoding.ASCII.GetBytes(job);
  27.             udpc.Send(sdata, sdata.Length, ep);
  28.         }
  29.     }
  30. }//XML CODING
  31.  
  32. <?xml version="1.0" encoding="utf-8" ?>
  33. <configuration>
  34.   <appSettings>
  35.     <add key = "mickey" value="manager"/>
  36.     <add key = "bob" value="tester"/>
  37.     <add key = "tom" value="clerk"/>
  38.     <add key = "jerry" value="manager"/>
  39.   </appSettings>
  40. </configuration>
  41.  
  42. //CLIENT SIDE PROGRAM
  43.  
  44. using System;
  45. using System.Net;
  46. using System.Net.Sockets;
  47. using System.Text;
  48. class EmployeeUDPClient
  49. {
  50.     public static void Main(string[] args)
  51.     {
  52.         UdpClient udpc = new UdpClient("Win7-PC", 2055);
  53.         IPEndPoint ep = null;
  54.         while (true)
  55.         {
  56.             Console.Write("Name: ");
  57.             string name = Console.ReadLine();
  58.             if (name == "") break;
  59.             byte[] sdata = Encoding.ASCII.GetBytes(name);
  60.             udpc.Send(sdata, sdata.Length);
  61.             byte[] rdata = udpc.Receive(ref ep);
  62.             string job = Encoding.ASCII.GetString(rdata);
  63.             Console.WriteLine(job);
  64.         }
  65.     }
  66. }

Here is the output of the C# Program:
output
Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.