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

}
}
}

                                         


 





RateeshReddy

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment