C# - PROGRAM to use XML

C# Program to Accept an Employee Name from Client and Sends back the Employee Job using XML

This C# Program to Accept an Employee Name from Client and Sends back the Employee Job using XML. 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 Accept an Employee Name from Client and Sends back the Employee Job using XML. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
  1. /*
  2.  * C# Program to Accept an Employee Name from Client and Sends back the Employee   
  3.  * Job using XML
  4.  */
  5.  
  6. //SERVER SIDE PROGRAM
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.IO;
  13. using System.Net;
  14. using System.Net.Sockets;
  15. using System.Configuration;
  16. namespace ServerSocket
  17. {
  18.     class Program
  19.     {
  20.         static TcpListener listener;
  21.         const int LIMIT = 5; 
  22.         public static void Service()
  23.         {
  24.             while (true)
  25.             {
  26.                 Socket soc = listener.AcceptSocket();
  27.                 Console.WriteLine("Connected: {0}", soc.RemoteEndPoint);
  28.                 try
  29.                 {
  30.                     Stream s = new NetworkStream(soc);
  31.                     StreamReader sr = new StreamReader(s);
  32.                     StreamWriter sw = new StreamWriter(s);
  33.                     sw.AutoFlush = true; // enable automatic flushing
  34.                     sw.WriteLine("{0} Employees available", ConfigurationSettings.AppSettings.Count);
  35.                     while (true)
  36.                     {
  37.                         string name = sr.ReadLine();
  38.                         if (name == "" || name == null) break;
  39.                         string job = ConfigurationSettings.AppSettings[name];
  40.                         if (job == null) job = "No such employee";
  41.                         sw.WriteLine(job);
  42.                     }
  43.                     s.Close();
  44.                 }
  45.                 catch (Exception e)
  46.                 {
  47.  
  48.                     Console.WriteLine(e.Message);
  49.                 }
  50.                 Console.WriteLine("Disconnected: {0}", soc.RemoteEndPoint);
  51.                 soc.Close();
  52.             }
  53.         }
  54.        static void Main(string[] args)
  55.         {
  56.             listener = new TcpListener(2055);
  57.             listener.Start();
  58.  
  59.             Console.WriteLine("Server mounted, listening to port 2055");
  60.             for (int i = 0; i < LIMIT; i++)
  61.             {
  62.                 Thread t = new Thread(new ThreadStart(Service));
  63.                 t.Start();
  64.             }
  65.         }
  66.     }
  67. }
  68. //XML CODING
  69.  
  70. <?xml version="1.0" encoding="utf-8" ?>
  71. <configuration>
  72.   <appSettings>
  73.     <add key = "mickey" value="manager"/>
  74.     <add key = "bob" value="tester"/>
  75.     <add key = "tom" value="clerk"/>
  76.     <add key = "jerry" value="manager"/>
  77.   </appSettings>
  78. </configuration>
  79.  
  80. //CLIENT SIDE PROGRAM
  81.  
  82. using System;
  83. using System.Collections.Generic;
  84. using System.Linq;
  85. using System.Text;
  86. using System.IO;
  87. using System.Net.Sockets;
  88. namespace ClientSocket
  89. {
  90.     class Program
  91.     {
  92.         static void Main(string[] args)
  93.         {
  94.             TcpClient client = new TcpClient("win7-PC", 2055);
  95.             try
  96.             {
  97.                 Stream s = client.GetStream();
  98.                 StreamReader sr = new StreamReader(s);
  99.                 StreamWriter sw = new StreamWriter(s);
  100.                 sw.AutoFlush = true;
  101.                 Console.WriteLine(sr.ReadLine());
  102.                 while (true)
  103.                 {
  104.                     Console.Write("Name: ");
  105.                     string name = Console.ReadLine();
  106.                     sw.WriteLine(name);
  107.                     if (name == "") break;
  108.                     Console.WriteLine(sr.ReadLine());
  109.                 }
  110.                 s.Close();
  111.             }
  112.             finally
  113.             {
  114.                 client.Close();
  115.             } 
  116.  
  117.         }
  118.     }
  119. }

C# Program to Establish Client Server Relationship

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

  1. /*
  2.  * C# Program to Establish Client Server Relationship
  3.  */
  4. //SERVER PROGRAM
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. namespace Server336
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             try
  18.             {
  19.                 IPAddress ipAd = IPAddress.Parse("10.18.227.162");
  20.                 TcpListener myList = new TcpListener(ipAd, 8001);
  21.                 myList.Start();
  22.                 Console.WriteLine("The server is running at port 8001...");
  23.                 Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
  24.                 Console.WriteLine("Waiting for a connection.....");
  25.                 Socket s = myList.AcceptSocket();
  26.                 Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  27.                 byte[] b = new byte[100];
  28.                 int k = s.Receive(b);
  29.                 Console.WriteLine("Recieved...");
  30.                 for (int i = 0; i < k; i++)
  31.                 {
  32.                     Console.Write(Convert.ToChar(b[i]));
  33.                 }
  34.                 ASCIIEncoding asen = new ASCIIEncoding();
  35.                 s.Send(asen.GetBytes("The string was recieved by the server."));
  36.                 Console.WriteLine("\nSent Acknowledgement");
  37.                 s.Close();
  38.                 myList.Stop();
  39.             }
  40.             catch (Exception e)
  41.             {
  42.                 Console.WriteLine("Error..... " + e.StackTrace);
  43.             }
  44.             Console.ReadLine();
  45.         }
  46.     }
  47. }
  48.  
  49. //CLIENT PROGRAM
  50. using System;
  51. using System.Collections.Generic;
  52. using System.Linq;
  53. using System.Text;
  54. using System.IO;
  55. using System.Net;
  56. using System.Text;
  57. using System.Net.Sockets;
  58.  
  59. namespace Client336
  60. {
  61.     class Program
  62.     {
  63.         static void Main(string[] args)
  64.         {
  65.             try
  66.             {
  67.                 TcpClient tcpclnt = new TcpClient();
  68.                 Console.WriteLine("Connecting.....");
  69.                 tcpclnt.Connect("10.18.227.162", 8001);                
  70.                 Console.WriteLine("Connected");
  71.                 Console.Write("Enter the string to be transmitted : ");
  72.                 String str = Console.ReadLine();
  73.                 Stream stm = tcpclnt.GetStream();
  74.                 ASCIIEncoding asen = new ASCIIEncoding();
  75.                 byte[] ba = asen.GetBytes(str);
  76.                 Console.WriteLine("Transmitting.....");
  77.                 stm.Write(ba, 0, ba.Length);
  78.                 byte[] bb = new byte[100];
  79.                 int k = stm.Read(bb, 0, 100);
  80.                 for (int i = 0; i < k; i++)
  81.                     Console.Write(Convert.ToChar(bb[i]));
  82.                 tcpclnt.Close();
  83.                 Console.Read();
  84.             }
  85.             catch (Exception e)
  86.             {
  87.                 Console.WriteLine("Error..... " + e.StackTrace);
  88.             }
  89.         }
  90.     }
  91. }

How to Implement UDP in C#


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. }