using System.Net;
using System;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Web;
using System.Text;
using System.Collections;
using System.Collections.Generic;
class project
{
TcpListener t;
IPAddress ip;
string clientlink;
string serverlink;
Socket serversocket;
Socket clientsocket;
byte[] bmessage;
string message;
Dictionary<string,IPAddress> clients;
public project()
{
clients = new Dictionary<string ,IPAddress>(); //stpres clients link reqired to be searched and ipadress
IPAddress ip = IPAddress.Parse("127.0.0.1");
t = new TcpListener(ip,200);//intializes tcplistener
t.Start();//starts tcplistener
Thread s = new Thread(this.intiate); //intialize the sequence
bmessage = new byte[2096]; //message tcp packet is 1024 bytes;
}
public void intiate()
{
while (true)
{
Socket serversocket = t.AcceptSocket();
clientsend(serversocket);
}
}
private void clientsend(Socket serversocket)
{
string str1 = hosturl(serversocket);
WebRequest w = (WebRequest)WebRequest.Create("http://www.google.com");
WebResponse str = w.GetResponse();
int reading = 0; //keep track of no of byte recived from the server
StringBuilder strs = new StringBuilder();
int sending = 0; // keep track of no of byte sent to the client
//set it since message packet size is standardized to 32 byte
byte[] b = new byte[32];
Stream clientstream = str.GetResponseStream(); //start getting response
reading = clientstream.Read(b, 0, 32); //read only first 32 bits
StringBuilder clientsend = new StringBuilder(" "); //constrict a string builder
while (reading != 0)
{
clientsend.Append(Encoding.ASCII.GetString(b, 0, reading));
serversocket.Send(b, reading, 0);
sending = sending + reading;
clientstream.Read(b, 0, 32);
}
}
private string hosturl(Socket serversocket)
{
serversocket.Receive(bmessage);
message = Encoding.UTF8.GetString(bmessage);
string link = message.Substring(message.IndexOf("/"), message.IndexOf("H") - 4);
link.Trim();
Console.WriteLine(link);
Console.WriteLine(message);
//let us retrive the address of the server where the message was intending to go
//start of the function
//for retriving the host adress we use the host content present in the http packet
//for this please refrer rfc 2616
//so now lets jump to code in the message you will recive a content in the packet as
// Host:127.0.0.1
//so we will haqve to get only the ipadress from it then we can parse it so that
//we only get the ipaddress of the server
//the below line of code retrives only the host string of the message
//when we use this function we get the connection alive packet also of the http packet
//so to remove it we format the string
//first we recive the entir packet
string hostaddress = message.Substring(message.IndexOf("Host"));
//now we store the index of connection integer
int end = hostaddress.IndexOf("Connection:");
//retrive hostaddress with connection alive
Console.WriteLine(hostaddress);
hostaddress.Trim();
//now we retrive only about the content that we require as http:127.0.0.1 by removing unwanted data
string lhost = hostaddress.Substring(0, end - 1);
lhost.Trim();
string hostip = lhost.Substring(lhost.IndexOf(":") + 2);
string launchstring = "";
launchstring = "http://" + hostip;
link.Replace("\r\n", " ");
Console.WriteLine(hostip);
Console.WriteLine("reached");
string str = launchstring.Replace("\r\n", " "); //remove \r\n this u can see in debug console so i removed it
string str1 = str.Replace("\r", link);
launchstring.Trim();
Console.WriteLine("launchstring : " + str1);
//here we get the link that our proxy server needs to send to destination server for retriving data
Console.WriteLine(launchstring);
return str1;
}
public static void Main()
{
project p = new project();
p.intiate();
}
}
i got appropriate response once when i started again i got
following excpetion remote host forcibly closed the socket
why?
This post has been edited by prajayshetty: 23 March 2009 - 06:05 PM

New Topic/Question
Reply



MultiQuote



|