0% found this document useful (0 votes)
130 views

Creating Webserver in C#

The document describes a C# program that creates a web server on port 80. The server listens for connections from clients like Google Chrome. When a client connects, the server reads the request, identifies the requested file, sends the file contents back if found, or sends a default page. The code is then extended to print the user name to the console when a form is submitted, and return a personalized greeting page with the user's first and last name.

Uploaded by

Eysha qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Creating Webserver in C#

The document describes a C# program that creates a web server on port 80. The server listens for connections from clients like Google Chrome. When a client connects, the server reads the request, identifies the requested file, sends the file contents back if found, or sends a default page. The code is then extended to print the user name to the console when a form is submitted, and return a personalized greeting page with the user's first and last name.

Uploaded by

Eysha qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Create a web server in C# using port 80.

Google Chrome shall connect to this port which will receive


messages from the server.

using System.Text;
using System;
using System.IO;
using System.Net.Sockets;
using System.Net;

public class NetworkIOServer


{
public static void Main()
{
Console.WriteLine("Web Server Started");
TcpListener tcpListener = new TcpListener(80);
tcpListener.Start();

for (; ; )
{
Socket socketForClient = tcpListener.AcceptSocket();
if (socketForClient.Connected)
{
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);

string tmp = streamReader.ReadLine();


Console.WriteLine("Connected. Recvd: {0}", tmp);

string[] p = tmp.Split(" ".ToCharArray());


tmp = p[1];
if (tmp == "/") tmp = "a.htm";

tmp = "D:" + tmp;


byte[] buffer;

if (File.Exists(tmp) == true)
{
buffer = File.ReadAllBytes(tmp);

int bytesRead = buffer.Length;


networkStream.BeginWrite(buffer, 0, bytesRead, null, null);
}
}
socketForClient.Close();
}
}
}

Write a server code so that the name of user is printed on console screen.

using System.Text;
using System;
using System.IO;
using System.Net.Sockets;
using System.Net;

public class NetworkIOServer


{
public static void Main()
{
Console.WriteLine("Web Server Started");
TcpListener tcpListener = new TcpListener(80);
tcpListener.Start();

for (; ; )
{
Socket socketForClient = tcpListener.AcceptSocket();
if (socketForClient.Connected)
{
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);

string tmp = streamReader.ReadLine();


string a = streamReader.ReadLine();
string b = streamReader.ReadLine();
Console.WriteLine("Connected. Recvd: {0}", tmp);

string[] p = tmp.Split(" ".ToCharArray());


tmp = p[1];
if (tmp == "/") tmp = "a.htm";

tmp = "D:" + tmp;


byte[] buffer;

if (File.Exists(tmp) == true)
{
buffer = File.ReadAllBytes(tmp);

int bytesRead = buffer.Length;


networkStream.BeginWrite(buffer, 0, bytesRead, null, null);
}
else
{
string[] p0 = tmp.Split("?".ToCharArray());
tmp = p0[1];
{
string[] p1 = tmp.Split(" ".ToCharArray());
tmp = p1[0];
{
string[] p2 = tmp.Split("&".ToCharArray());
a = tmp = p2[0];
b = tmp = p2[1];
{
string[] p3 = a.Split("=".ToCharArray());
string[] p4 = b.Split("=".ToCharArray());
a = tmp = p3[1];
b = tmp = p4[1];
}
}
}
}
}
socketForClient.Close();
}
}
}

Extend above task so that after clicking “submit”, user gets to a new page which says: “Hello,
[firstname lastname] !”, with names actually entered by the user earlier.

using System.Text;
using System;
using System.IO;
using System.Net.Sockets;
using System.Net;

public class NetworkIOServer


{
public static void Main()
{
Console.WriteLine("Web Server Started");
TcpListener tcpListener = new TcpListener(80);
tcpListener.Start();

for (; ; )
{
Socket socketForClient = tcpListener.AcceptSocket();
if (socketForClient.Connected)
{
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);

string tmp = streamReader.ReadLine();


string a = streamReader.ReadLine();
string b = streamReader.ReadLine();
Console.WriteLine("Connected. Recvd: {0}", tmp);

string[] p = tmp.Split(" ".ToCharArray());


tmp = p[1];
if (tmp == "/") tmp = "a.htm";

tmp = "D:" + tmp;


byte[] buffer;

if (File.Exists(tmp) == true)
{
buffer = File.ReadAllBytes(tmp);

int bytesRead = buffer.Length;


networkStream.BeginWrite(buffer, 0, bytesRead, null, null);
}
else
{
string[] p0 = tmp.Split("?".ToCharArray());
tmp = p0[1];
{
string[] p1 = tmp.Split(" ".ToCharArray());
tmp = p1[0];
{
string[] p2 = tmp.Split("&".ToCharArray());
a = tmp = p2[0];
b = tmp = p2[1];
{
string[] p3 = a.Split("=".ToCharArray());
string[] p4 = b.Split("=".ToCharArray());
a = tmp = p3[1];
b = tmp = p4[1];

string z = "<html><head><title>EE12 Web


Server</title></head><body><h1>Welcome to our Server.</h1><br><br>"+a+"
"+b+"></body></html>";
streamWriter.WriteLine(z);
Console.WriteLine(a);
streamWriter.Flush();
}
}
}
}
}
socketForClient.Close();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy