Web ProxyServer in C# and VB

Introduction

Web Proxy Server is HTTP proxy server written in C#. It is Multithreaded so many clients can access the web through this WebProxy Server.

Technology Used

System.NET, System.IO ,System.Threading and C#

About the sample

This sample is a console application which acts as a multithreaded WebProxy server.If you want to use any specific port to Listen then use that as a command line parameter other wise default 8089 port is used. It is multithreade Proxy Server that means multiple client can communicate with Proxy server.  

Good luck and Enjoy C#

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
namespace WebProxy2
{
class WebProxy2
{
Socket clientSocket;
Byte[] read =
new byte[1024];
Byte [] Buffer =
null;
Encoding ASCII = Encoding.ASCII;
const string HTTP_VERSION = "HTTP/1.0";
const string CRLF = "\r\n";
Byte[] RecvBytes =
new Byte[4096];
public WebProxy2(Socket socket)
{
this.clientSocket = socket;
}
public void run()
{
String clientmessage = " ", sURL = " ";
int bytes = readmessage(read, ref clientSocket, ref clientmessage);
if(bytes == 0)
{
return;
}
int index1 = clientmessage.IndexOf(' ');
int index2 = clientmessage.IndexOf(' ', index1 + 1);
if((index1 == -1) || (index2 == -1))
{
throw new IOException();
}
Console.WriteLine("Connecting to Site: {0}", clientmessage.Substring(index1 + 1, index2 - index1));
Console.WriteLine("Connection from {0}", clientSocket.RemoteEndPoint);
string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
int index3 = part1.IndexOf('/', index1 + 8);
int index4 = part1.IndexOf(' ', index1 + 8);
int index5 = index4 - index3;
sURL = part1.Substring(index1 + 4, (part1.Length - index5) - 8);
try
{
IPHostEntry IPHost = Dns.Resolve(sURL);
Console.WriteLine("Request resolved: ", IPHost.HostName);
string [] aliases = IPHost.Aliases;
IPAddress[] address = IPHost.AddressList;
Console.WriteLine(address[0]);
IPEndPoint sEndpoint =
new IPEndPoint(address[0], 80);
Socket IPsocket =
new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
IPsocket.Connect(sEndpoint);
if(IPsocket.Connected)
Console.WriteLine("Socket connect OK");
string GET = clientmessage;
Byte[] ByteGet = ASCII.GetBytes(GET);
IPsocket.Send(ByteGet, ByteGet.Length, 0);
Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
Console.WriteLine("Recieved {0}", + rBytes);
//Buffer = RecvBytes;
String strRetPage = null;
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
while (rBytes > 0)
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
}
IPsocket.Shutdown(SocketShutdown.Both);
IPsocket.Close();
sendmessage(clientSocket, strRetPage);
}
catch (Exception exc2)
{
Console.WriteLine(exc2.ToString());
}
}
private int readmessage(byte [] ByteArray, ref Socket s, ref String clientmessage)
{
int bytes = s.Receive(ByteArray, 1024, 0);
string messagefromclient = Encoding.ASCII.GetString(ByteArray);
clientmessage = (String)messagefromclient;
return bytes;
}
private void sendmessage(Socket s, string message)
{
Buffer =
new Byte[message.Length + 1];
int length = ASCII.GetBytes(message, 0, message.Length, Buffer, 0);
s.Send(Buffer, length, 0);
}
static void Main(string[] args)
{
const int port = 8889;
TcpListener tcplistener =
new TcpListener(port);
Console.WriteLine("Listening on port {0}", + port);
tcplistener.Start();
while(true)
{
Socket socket = tcplistener.AcceptSocket();
WebProxy2 webproxy =
new WebProxy2(socket);
Thread thread =
new Thread(new ThreadStart(webproxy.run));
thread.Start();
}
}
}
}

VB.NET source code updated by Russ Green.


Similar Articles