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

No comments:

Post a Comment