Question: To complete this exercise, you need to modify the source code of programs you used in the lab of Module 8, i.e., server_book.cs and client_book.cs

To complete this exercise, you need to modify the source code of programs you used in the lab of Module 8, i.e., server_book.cs and client_book.cs, to satisfy the following:

(1) Copy the two programs used in the lab to server_gpa.cs and client_gpa.cs. It's important that the server program name matches the name of the XML file.

(2) Each client allows user to enter a student ID like 'U101'. When server receives a student ID, it looks up the XML file to return the student's major like 'PHYSICS'.

(3) After a client received the major of the entered student and without any more input from user, it sends the returned major automatically back to the server. Because the server of this program only expects to receive either a student ID or a major. At this time, it gets a major rather than a student ID, so it looks up again the same XML file to search and return the major's average GPA to the client.

(4) After a student ID is submitted and the server responds, the client should display both the major of the input student and the average GPA of this major.

(5) The server accepts one or two but no more than two clients to connect at any time.

When completed, please save carefully your program code and data file. But, for grading purpose, you only need to submit a screenshot image file that contains one server's windows and two client windows. Each client window must show a different student ID is entered, and the returned major with its average GPA are displayed. Be sure your screenshot is large enough to clearly show its contents. Otherwise, you will be required to meet our TA to present and run your programs before receiving a grade.

server

/*

* 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 = 5;

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

sw.AutoFlush = true; // enable automatic flushing

sw.WriteLine("{0} books 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 such book!";

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

}

}

}

}

client

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

sw.AutoFlush = true;

Console.WriteLine(sr.ReadLine());

while (true)

{

Console.Write("Enter a book title: ");

string title = Console.ReadLine();

sw.WriteLine(title);

if (title == "")

break;

Console.WriteLine(sr.ReadLine());

}

s.Close();

}

finally

{

client.Close();

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!