Question

You must first compile both .cs file into .exe. Then, run the server in one command...

You must first compile both .cs file into .exe. Then, run the server in one command prompt window and run two cilents, each in a separate command prompt window. To quit a client, you may enter no book title but just hit the enter or enter Ctrl-C. The server always displays a message whenever a client is connected and disconnected.

<?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>

The above XML file contains two types of data: student ID with major, and major with average GPA.

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.

Also change the server program such that it allows no more than two clients to be connected at any point of time.

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

}
}
}

---------------------------------------- client_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, 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);
}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

server.cs

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 id = ConfigurationManager.AppSettings[bookTitle];
if (id == null)
                           id = "Sorry, no such id!";
sw.WriteLine(id);
}
s.Close();
}
catch (Exception e)
{

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

void HandleConnection() //to limit the client connection this must be done by queuing
    {
        listener.Start();

        while (true)
        {
            var client = listener.AcceptTcpClient();

            if (queue.Count <= 2)
            {
                queue.Enqueue(new Connection(client));
                reset.Set();
            }
            else
            {
                client.GetStream().Write(CONNECTION_REFUSED, 0, CONNECTION_REFUSED_LENGTH);
                client.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);
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
You must first compile both .cs file into .exe. Then, run the server in one command...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT