Client /Server Demonstration in C# : Part I

In this article, we will have a demonstration of Client/Server applications in C#.

Introduction

In this article, we will have a demonstration of Client/Server applications in C#.

In the first part, we will set up a client that sends a Web Request to a defined Web Server (http://google.com) and show you the response.

Client Server Demonstration

In the second part, we will set up a server (Local host) where multiple clients can send a request to the server and the server will process it.

Background

  • TCP: Is a Communication Protocol essential for network communication.
  • Client: Someone trying to communicate with a server.
  • Server: A program that waits for someone for communication.
  • IP Address: Each machine on an Internet or LAN is assigned a unique Identity Number in a special format (xxx.xxx.xxx.xxx), where X is in the range from 0-127.
  • Socket: A connecting point between a server and a client. There is a connecting point for both the server and the client.
  • Port Number: A positive number to identify connecting points. The first 1024 port numbers are used by system processes. So, you can use numbers other than that.

Client Demonstration

In this, we send a Web Request to Google.com (or any) and receive the request sent by the Web Server.

Actually, we are trying to do the same what a web browser does. Since a web browser sends a web request to a Web Server and process the HTML web response to be displayed in the browser window. An end-user only procedure.

Let’s start our project to set up a client.

Step 1

Include the desired namespace in your project.

Using Syste,IO;
Using System.System.Net.Sockets;

Step 2

So, you need a server and its port to connect.

Do it yourself or ask a user to provide that.

Here, I will ask the server’s name from user and assigned port number myself.

Console.WriteLine("Name of Server : ");
string
server = Console.ReadLine();


Step 3

I now have the server name and port number 80 (default HTTP port) from the user.

try
{
       // Connect To Server
        TcpClient client = new TcpClient(server, 80);
        //Setup the Stream to read/write data on Network
         StreamReader sr = new StreamReader(client.GetStream());
         StreamWriter sw = new StreamWriter(client.GetStream());

           //Send Web-Request
           sw.WriteLine("GET / HTTP/1.0 \n\n");
           sw.Flush(); // Clear the StreamWriter Stream

           string response = sr.ReadLine();
             while (response != null)
           {
                  Console.WriteLine(response);
                   response = sr.ReadLine();
            }
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("You may Have Some Error ..");

              }
     }

Explanation

First, we begin by creating an object of TcpClient() and within its constructor two arguments Server Name and its specified Port.

TcpClient client = new TcpClient(server, 80);

In the second and third statements, we will create a Read and Write Stream for the network so that we can write over the Network Stream.

StreamReader sr = new StreamReader(client.GetStream());
StreamWriter
sw = new StreamWriter(client.GetStream());


Within its constructor’s argument we the client.GetStream().

Next we send a web request (GET / HTTP/1.0 \n\n) using StreamWriter as in the following:

sw.WriteLine("GET / HTTP/1.0 \n\n");

After every request, we need to clear our Network Buffer. Hence forth, we call the Flush() method of StreamWriter.

sw.Flush();

We have now done our initial job by sending the web request and now we need to handle the response from the Web Server. As we know, the response of any Web Server is in HTML code. This can only be interpreted by a Web Browser.

See how we will handle the Web Response?

Earlier, we created an object of StreamReader to read data from the Network Buffer.

string response = sr.ReadLine();

while (response != null)
{
      Console.WriteLine(response);
       response = sr.ReadLine();
}


When we do this, we get NULL.

At last, we need to free the TcpClient by closing it as in the following:

client.Close();

Step 4

In your output console window, you will get some of this.

output console window

Actually, it is a Web Response that is in HTML. And it can only be interpreted by a Web Browser that understands the HTML.

Conclusion

Here, we have demonstrated a Client that sends a Web Request to the Server and it responds in their way. Using Console windows, we tried to show you the Web Response.