Skip to content
Related Articles

Related Articles

C# Program that Accepts an Employee Name From Client and Display the Job Title using XML

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 18 Feb, 2022

In this program, using client, employee name is being accepted and then it passes the employee job using XML. On the client-side, we are using a while loop and at each iteration, we are creating a value for the ‘object’ variable using the TcpClient() method. This is specifically used to provide client connections for TCP network services. Also, we have used the GetStream() method that returns the NetworkStream which is used for the transmission of data(send and receive).

We have assigned the value of the AutoFlush() method as true. This specifically marks the value that indicates whether the StreamWriter would flush the buffer to the specified stream after every call to StreamWriter.Write. We have used a while loop and at each step of the iteration, we are reading the name of the employee.

Here, the close() method terminates the current stream and free any resources (such as sockets and file handles) linked with the current stream. On the server side, we have used AcceptSocket() function that accepts the connection request that is pending. Again, we have used the while loop to read the name of the employee.

To check whether the name variable is empty or null we have used the If condition statement. To obtain the AppSettingsSection data we have used AppSettings for the current application’s default configuration.

To check whether the value of the ‘job’ variable is equal to null we are have used another if condition. If this condition evaluates true then we have marked the value of the ‘job’ variable as “Invalid Employee”. To write specified data we are using WriteLine() method. The current line terminator is then used to the standard output stream. Eventually, the name of the employee is given on the client side and the corresponding employee job has been printed.

Server-Side Program:

C#




// C# 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;
  
class GFG{
      
static TcpListener myListener;
const int MAX = 5;
  
public static void Service()
{
    while (true)
    {
        Socket mySocket = listener.AcceptSocket();
        Console.WriteLine("Connected to {0}",
                          mySocket.RemoteEndPoint);
        try 
        {
            Stream myStream = new NetworkStream(mySocket);
            StreamReader myStreamReader = new StreamReader(myStream);
            StreamWriter myStreamWriter = new StreamWriter(myStream);
              
            // enable automatic flushing
            myStreamWriter.AutoFlush = true
                                   
            myStreamWriter.WriteLine("{0} is the number of available Employees",
                                     ConfigurationSettings.AppSettings.Count);
            while (true
            {
                string myName = myStreamReader.ReadLine();
                if (myName == "" || myName == null)
                    break;
                      
                string myJob = ConfigurationSettings.AppSettings[name];
                  
                if (myJob == null)
                    myJob = "Invalid Employee";
                      
                myStreamWriter.WriteLine(myJob);
            }
            mySocket.Close();
        }
        catch (Exception exception) 
        {
            Console.WriteLine(exception.Message);
        }
        Console.WriteLine("Disconnecting: {0}",
                          mySocket.RemoteEndPoint);
        
          // Close the socket 
        mySocket.Close();
    }
}
  
// Driver code
static public void Main()
{
      
      // Listener
    listener = new TcpListener(2055);
    listener.Start();
  
    Console.WriteLine("Listening to the port 2055...");
    for(int i = 0; i < MAX; i++)
    {
          
          // Creating thread object
        Thread thread = new Thread(new ThreadStart(Service));
          
          // Start the thread
          thread.Start();
    }
}
}

XML Encoding:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key = "Bhuwanesh" value="Developer"/>
    <add key = "Harshit" value="Tester"/>
  </appSettings>
</configuration>

Client-Side Program:

C#




// C# program for Client Side 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
  
class GFG{
  
static public void Main()
{
    TcpClient client = new TcpClient("win7-PC", 2055);
    try 
    {
        Stream myStream = client.GetStream();
        StreamReader myStreamReader = new StreamReader(myStream);
        StreamWriter myStreamWriter = new StreamWriter(myStream);
        myStreamWriter.AutoFlush = true;
        Console.WriteLine(myStreamReader.ReadLine());
          
        while (true
        {
            Console.Write("Name of the employee: ");
            string employeeName = Console.ReadLine();
            myStreamWriter.WriteLine(employeeName);
              
            if (employeeName == "")
                break;
                  
            Console.WriteLine(myStreamReader.ReadLine());
        }
        myStream.Close();
    }
    finally 
    {
        client.Close();
    }
}
}

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!