Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C programming please! In this assignment, you are required to modify the programs, i.e., server_book.cs and client_book.cs , to allow a client

Need help with C programming please!

In this assignment, you are required to modify the programs, i.e., server_book.cs and client_book.cs, to allow a client to inquiry a student's major and receive average GPA of the major of the student which are shown below.

The screenshot below demonstrates an execution of the server plus three clients, each runs in its own session/command prompt and each client sends only one student ID a time and receives from server the student's major and the GPA average of all students with that major.

image text in transcribed

Do the following to complete the assignment.

(1) Copy the above two programs and re-name them as server_gpa.cs and client_gpa.cs. It's important that the server program name matches the name of the below XML data file.

(2) Modify client_gpa.cs to allow user to enter a student ID like 'U101' and submit it to the server.

(3) Modify server_gpa.cs, so, when it receives a student ID from any client, it looks up the XML file to search and return the student's major like 'PHYSICS'.

(4) Once a client receives the major of the student, and it sends it immediately back to the server with no other input. When the server receives a major and not a student ID, it looks up again the same XML file to find the major's average GPA and return it to the client. Again, there is no calculation needed here because all average GPA are already stored in the data file. The server only needs to search and find one to answer its clients.

(5) The client should display both the returned major of the student and the average GPA of the student's major.

(6) The server accepts only up to three client connections at any time (as seen in the above sample screenshot). Hint: set the LIMIT in the server program to 3.

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 = 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, 2057);

listener.Start();

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

for (int i = 0; i

{

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

//The above statement can be simplified to next:

Thread t = new Thread(Query);

t.Start();

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

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

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

}

}

}

}

server_gpa.execonfig:

X Comme X c. Administrator: Command Prompt - server_gpa , Administrator. Command Prompt-client_gpa Server thread 3 started. made because the target machine actively refused it 127.0.0.1:2055 Server thread 4 started at System.Net.Sockets. TcpClient..ctor(String hostname, Int32 port) Server thread 5 started at Client Socket. Program. Main(String[] args) Server thread 6 started Server thread 7 started C:\CNT4104Lab\sp4\Lab8-Asseclient_gpa Server thread 8 started 11 students available Server thread 9 started. Enter student TD (o a 11104) to get major, or nothing to quit: 0102 Server thread 10 started. EDU Connected: 127.0.0.1:22759 Average GPA of all EDU major = 2.96 Disconnected: 127.0.0.1:22759 Enter student Iv te.g., U104) to get major, or nothing to quit: C C:\CNT4104Lab\sp&\Lab8-Ass> C:\CNT4104Lab\sp4 \Lab8-Asserver_gpa 66. Administrator: Command Prompt-client_gpa Server started, listening to port2055 size) Server thread 1 started.... at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) Server thread 2 started at System.IO.TextWriter.WriteLine(String value) Server thread 3 started at Client Socket. Program. Main(String[] args) Server thread 4 started Server thread 5 started. C:\CNT4104Lab\sp4\Lab8-Ass Client_gpa Server thread 6 started 11 students available Server thread 7 started Enter student ID LO, 01041_to_get_major, or nothing to quit: 0104 Server thread 8 started. EE Server thread 9 started. Average GPA of all EE major = 3.02 Server thread 10 started. Enter student IU Te.g., 0104] to get major, or nothing to quit: Connected: 127.0.0.1:61358 Connected: 127.0.0.1:61363 o Administrator: Command Prompt-client gpa Connected: 127.0.0.1:61371 C:\Users\fangl>cd \cnt41041 \sp4\lab8-ass The system cannot find the path specified. C:\Users\fang1%cd \cnt41041ab\sp4\lab8-ass C:\CNT4104Lab\sp4\Lab8-Ass Client_gpa 11 students available Entor student ID o.s., U104) to-got-major, or nothing to quit: u101 PHYSICS Average GPA of all PHYSICS major = 3.11 Enter student ID (e.g., 0104) to get major or nothing to quit: X Comme X c. Administrator: Command Prompt - server_gpa , Administrator. Command Prompt-client_gpa Server thread 3 started. made because the target machine actively refused it 127.0.0.1:2055 Server thread 4 started at System.Net.Sockets. TcpClient..ctor(String hostname, Int32 port) Server thread 5 started at Client Socket. Program. Main(String[] args) Server thread 6 started Server thread 7 started C:\CNT4104Lab\sp4\Lab8-Asseclient_gpa Server thread 8 started 11 students available Server thread 9 started. Enter student TD (o a 11104) to get major, or nothing to quit: 0102 Server thread 10 started. EDU Connected: 127.0.0.1:22759 Average GPA of all EDU major = 2.96 Disconnected: 127.0.0.1:22759 Enter student Iv te.g., U104) to get major, or nothing to quit: C C:\CNT4104Lab\sp&\Lab8-Ass> C:\CNT4104Lab\sp4 \Lab8-Asserver_gpa 66. Administrator: Command Prompt-client_gpa Server started, listening to port2055 size) Server thread 1 started.... at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) Server thread 2 started at System.IO.TextWriter.WriteLine(String value) Server thread 3 started at Client Socket. Program. Main(String[] args) Server thread 4 started Server thread 5 started. C:\CNT4104Lab\sp4\Lab8-Ass Client_gpa Server thread 6 started 11 students available Server thread 7 started Enter student ID LO, 01041_to_get_major, or nothing to quit: 0104 Server thread 8 started. EE Server thread 9 started. Average GPA of all EE major = 3.02 Server thread 10 started. Enter student IU Te.g., 0104] to get major, or nothing to quit: Connected: 127.0.0.1:61358 Connected: 127.0.0.1:61363 o Administrator: Command Prompt-client gpa Connected: 127.0.0.1:61371 C:\Users\fangl>cd \cnt41041 \sp4\lab8-ass The system cannot find the path specified. C:\Users\fang1%cd \cnt41041ab\sp4\lab8-ass C:\CNT4104Lab\sp4\Lab8-Ass Client_gpa 11 students available Entor student ID o.s., U104) to-got-major, or nothing to quit: u101 PHYSICS Average GPA of all PHYSICS major = 3.11 Enter student ID (e.g., 0104) to get major or nothing to quit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

More Books

Students also viewed these Databases questions

Question

demonstrate the importance of induction training.

Answered: 1 week ago