Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program Using C#. Modify the programs given in Lab 8, server_book.cs and client_

ID: 3596294 • Letter: P

Question

Program Using C#.

Modify the programs given in Lab 8, server_book.cs and client_book.cs, such that a client can enter a student ID like 'U101' and the server returns his/her major 'PHYSICS'. The client will automatically send the returned major back to the server in order to get the major's average GPA using the same XML file. Then, the client displays the major of the input student and the major's average GPA.

My main issue is that I don't know how to save the first output from the server then send it back for the second response.

server_book.cs

/*

* C# program to accept a book title from clients and sends back

* its price using XML

*/

//SERVER SIDE PROGRAM

using System;

using System.Collections.Generic;

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 = 2;

public static void Query()

{

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

StreamReader sr2 = new StreamReader(s);

StreamWriter sw2 = new StreamWriter(s);

sw.AutoFlush = true; // enable automatic flushing

sw.WriteLine("{0} students available", ConfigurationManager.AppSettings.Count);

while (true)

{

string bookTitle = sr.ReadLine();

if (bookTitle == "" || bookTitle == null)

                           break;

string price = ConfigurationManager.AppSettings[bookTitle];

if (price == null)

                           price = "Sorry, No student ID!";

sw.WriteLine(price);

}

s.Close();

}

catch (Exception e)

{

Console.WriteLine(e.Message);

}

Console.WriteLine("Disconnected: {0}", soc.RemoteEndPoint);

soc.Close();

}

}

static void Main(string[] args)

{

           IPAddress ipAd = IPAddress.Parse("127.0.0.1");

listener = new TcpListener(ipAd, 2055);

listener.Start();

Console.WriteLine("Server started, listening to port 2055");

for (int i = 0; i < LIMIT; i++)

{

Thread t = new Thread(new ThreadStart(Query));

t.Start();

               Console.WriteLine("Server thread {0} started....", i + 1);

}

}

}

}

client_book.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Net.Sockets;

namespace ClientSocket

{

class Program

{

static void Main(string[] args)

{

TcpClient client = new TcpClient("127.0.0.1", 2055);

try

{

Stream s = client.GetStream();

StreamReader sr = new StreamReader(s);

StreamWriter sw = new StreamWriter(s);

StreamReader sr2 = new StreamReader(s);

StreamWriter sw2 = new StreamWriter(s);

sw.AutoFlush = true;

Console.WriteLine(sr.ReadLine());

while (true)

{

Console.Write("Enter a student ID: ");

string title = Console.ReadLine();

sw.WriteLine(title);

if (title == "")

                       break;

Console.WriteLine(sr.ReadLine());

//

}

s.Close();

}

finally

{

client.Close();

}

}

}

}

xml file

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key = "U101" value="PHYSICS"/>

<add key = "U102" value="EDU"/>

<add key = "U103" value="EDU"/>

<add key = "PHYSICS" value="3.11"/>

   <add key = "EDU" value="2.96"/>

   <add key = "U104" value="EE"/>

   <add key = "U105" value="PHYSICS"/>

   <add key = "EE" value="3.02"/>  

   <add key = "U106" value="EE"/>

   <add key = "U107" value="EDU"/>

   <add key = "U108" value="EE"/>

</appSettings>

</configuration>

Explanation / Answer

/SERVER PROGRAM

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Configuration;
using System.Xml;

namespace ServerSocket
{
class Program
{

//List to keep record of connected clients(count)
static TcpListener listener; List<TcpClient> ConnectedClients = null;
Program()
{
IPAddress ipAd = IPAddress.Parse("192.168.233.1"); //Change to you IP or localhost
listener = new TcpListener(ipAd, 2055);
listener.Start();
Console.WriteLine("Server started, listening to port: 2055 !!");
ConnectedClients = new List<TcpClient>();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
ConnectedClients.Add(client);
if (ConnectedClients.Count < 3) //maximum 2 clients will be served
{
Thread t = new Thread(new ParameterizedThreadStart(clientHandler));
t.Start(client); Console.WriteLine("Client Count: " + ConnectedClients.Count);
}
else { //If can not connect then ignore
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true };
writer.WriteLine("Disconnected");
}
}
}

void clientHandler(object clientDetail) //Client thread to handle each client's requests
{
TcpClient client = (TcpClient)clientDetail;
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true };
StreamReader reader = new StreamReader(stream, Encoding.ASCII);
string message = "";
while (true)
{
while (message != null)
{
message = reader.ReadLine();
Console.WriteLine("Message from client: " + message);
Console.WriteLine("Reading XML file to look up...");
String xmlData = readXML(message);
writer.WriteLine(xmlData);
Console.WriteLine("Message to client: " + xmlData);
}
Console.WriteLine("Disconnected client: "+client);
}
}

static String readXML(String message) //Read XML file to get the required details
{
//make sure to replace the XML file path correctly, use a valid xml file
XmlReader xmlReader = XmlReader.Create("D://server_gpa.xml");

String keyvalue = "";
while (xmlReader.Read())
{
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "add"))
{
if (xmlReader.HasAttributes)
{
if (xmlReader.GetAttribute("key").Equals(message))
{
keyvalue = xmlReader.GetAttribute("value");
}
}

}
}
return keyvalue;
}

static void Main(string[] args)
{
Program _server = new Program();
}
}
}

//CLIENT PROGRAM

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
namespace ClientSocket
{
class Program
{
static void Main(string[] args)
{
TcpClient client = new TcpClient("192.168.233.1", 2055);
NetworkStream stream = client.GetStream();
try
{
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream) { AutoFlush = true };
Console.Write("Connected to server...");
while (true)
{
Console.Write(" Enter your message: ");
string message = Console.ReadLine();
writer.WriteLine(message);
string lineReceived = reader.ReadLine();
Console.WriteLine("Received from server: " + lineReceived);
if (lineReceived.Equals("Disconnected"))
{
Console.WriteLine("**Server Busy: can not connect more then 2 clients. **Disconnected from server, try again later !");
break;
}
else //this is to send the reply back automatically
{
Console.WriteLine("[Automatically sending it back to server]");
writer.WriteLine(lineReceived);
string receivedGPA = reader.ReadLine();
Console.WriteLine("Major: " + lineReceived + " Average GPA: " + receivedGPA);
}
}

}
finally
{
stream.Close();
client.Close();
Console.ReadKey();
}

}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote