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.

Problem Description

This C# Program Accepts an Employee Name from Client and Sends back the Employee Job using XML.

Problem Solution

Here name of the employee is entered in the client side window and the job the specified employee is displayed.

Program/Source Code

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();
            } 
 
        }
    }
}
Program Explanation

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.

advertisement
advertisement

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.

Runtime Test Cases

output

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

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