Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Program to Accept an Employee Name from Client and Sends back the Employee Job using XML
C# Program to Accept an Employee Name from Client and Sends back the Employee Job using XML
This is a C# Program to accept an employee name from client and sends back the employee job using xml.
This C# Program Accepts 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.
/* * C# Program to Accept an Employee Name from Client and Sends back the Employee * Job using XML */ //SERVER SIDE PROGRAM using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO; using System.Net; using System.Net.Sockets; using System.Configuration; namespace ServerSocket { class Program { static TcpListener listener; const int LIMIT = 5; public static void Service() { while (true) { Socket soc = listener.AcceptSocket(); Console.WriteLine("Connected: {0}", soc.RemoteEndPoint); try { Stream s = new NetworkStream(soc); StreamReader sr = new StreamReader(s); StreamWriter sw = new StreamWriter(s); sw.AutoFlush = true; // enable automatic flushing sw.WriteLine("{0} Employees available", ConfigurationSettings.AppSettings.Count); while (true) { string name = sr.ReadLine(); if (name == "" || name == null) break; string job = ConfigurationSettings.AppSettings[name]; if (job == null) job = "No such employee"; sw.WriteLine(job); } s.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("Disconnected: {0}", soc.RemoteEndPoint); soc.Close(); } } static void Main(string[] args) { listener = new TcpListener(2055); listener.Start(); Console.WriteLine("Server mounted, listening to port 2055"); for (int i = 0; i < LIMIT; i++) { Thread t = new Thread(new ThreadStart(Service)); t.Start(); } } } } //XML CODING <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key = "mickey" value="manager"/> <add key = "bob" value="tester"/> <add key = "tom" value="clerk"/> <add key = "jerry" value="manager"/> </appSettings> </configuration> //CLIENT SIDE PROGRAM using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net.Sockets; namespace ClientSocket { class Program { static void Main(string[] args) { TcpClient client = new TcpClient("win7-PC", 2055); try { Stream s = client.GetStream(); StreamReader sr = new StreamReader(s); StreamWriter sw = new StreamWriter(s); sw.AutoFlush = true; Console.WriteLine(sr.ReadLine()); while (true) { Console.Write("Name: "); string name = Console.ReadLine(); sw.WriteLine(name); if (name == "") break; Console.WriteLine(sr.ReadLine()); } s.Close(); } finally { client.Close(); } } } }
This C# program is used to accept an employee name from client and sends back the employee job using XML. In client side using while loop create an value for ‘object’ variable of TcpClient() method. It is used to provide client connections for TCP network services. Then GetStream() function is used to return the NetworkStream used to send and receive data.
Assign the value of AutoFlush() function as true. It is used to get or set a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to StreamWriter.Write. Using while loop we are reading the name of the employee.
The close() method is used to close the current stream and release any resources (such as sockets and file handles) associated with the current stream. In server side program the AcceptSocket() function is used to accept a pending connection request. Using while loop we are reading the name of the employee.
If condition statement is used to check the value of ‘name’ variable is equal to empty or equal to null using Logical OR operator, if the condition is true then execute the statement. The AppSettings is used to get the AppSettingsSection data for the current application’s default configuration.
Another if condition statement is used to check the value of ‘job’ variable is equal to null. If the condition is true then execute the statement and assign the value of ‘job’ variable as “No such employee”. Using WriteLine() method write the specified data, followed by the current line terminator, to the standard output stream. Hence the name of the employee is entered in the client side window and the specified employee job is printed.