Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In c# complete the code in the TODO area of the code below. This can be done in any of the following ways. WebRequest and

In c# complete the code in the TODO area of the code below. This can be done in any of the following ways. WebRequest and HttpWebResponse classes, a WebClient object, or an HTTPClient object.

In this lab, you will be writing your own C# web client that communicates to a web server and downloads a short message. There are multiple ways to implement the web client, a few of them being to utilize the WebRequest and HttpWebResponse classes, a WebClient object, or an HTTPClient object. Your web client should use a download method to contact the web server and display the server's response to the user. The template code already creates a working web server for you, so you'll only need to handle the client-side code.

Download the assignment template hereimage text in transcribed. Submit the completed .cs file with your completed client code.

Network.cs

using System.IO;

using System.Net;

using System.Text;

using System;

using System.Threading;

namespace Networking

{

class Program

{

static void Main()

{

//Start the server

Server();

Console.WriteLine(" ---------------------------------------------------------------- Press any key to start the client...");

Console.ReadKey(true);

//Start the client

WebClient();

//Wrap up

Console.WriteLine(" -------Press Enter key when done.---------");

Console.ReadKey(true);

}

async static void Server()

{

//Prompt the user for the URI and port

Console.WriteLine(" What is the URI of the server? (http://localhost) ");

string serverURI = Console.ReadLine();

Console.WriteLine(" And what is the port number? (1300) ");

string portNum = Console.ReadLine();

try

{

//Creates the listener; this is the part that "listens" for a client to connect

HttpListener listener = new HttpListener();

listener.Prefixes.Add(serverURI + ":" + portNum + "/");

//Start the listener

Console.WriteLine(" Starting Server...");

listener.Start();

Console.WriteLine("Server Started ");

while (true)

{

//The server code will wait right here for a client to connect; in the meantime, control returns to the main method.

//When a client does connect, this code will resume until it loops back around to "await" again.

HttpListenerContext context = await listener.GetContextAsync();

//Create a response to send back to the client

string msg = " Message from Server: Congrats! You have started a server and accessed that server with your client!";

context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);

context.Response.StatusCode = (int)HttpStatusCode.OK;

//Convert that response into a stream and write it with a StreamWriter

using (Stream stream = context.Response.OutputStream)

{

using (StreamWriter writer = new StreamWriter(stream))

{

writer.Write(msg);

}

}

Console.WriteLine("Message sent to client");

}

}

catch (Exception err)

{

//If any error occurs, write its information to the console

//The server will then shut down

Console.WriteLine(err.ToString());

}

}

static void WebClient()

{

//TODO: Connect to the server you set up previously, send it a message, and output its response to the user.

}

}

}

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

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

2. How much time should be allocated to the focus group?

Answered: 1 week ago

Question

1. Where will you recommend that she hold the focus group?

Answered: 1 week ago