4 Replies - 993 Views - Last Post: 24 March 2009 - 12:15 AM Rate Topic: -----

#1 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

socket error

Posted 23 March 2009 - 06:04 PM


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


Is This A Good Question/Topic? 0
  • +

Replies To: socket error

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: socket error

Posted 23 March 2009 - 06:22 PM

I dunno 'bout the forced closings. :)

But I do see some problems with your string handling. Strings in .NET are immutable - once created they can not be modified. To modify a string you must create a new string.
For example
string link = message.Substring(message.IndexOf("/"), message.IndexOf("H") - 4);
link.Trim();


does not trim() the string link. string.Trim() returns a trimmed string. To use it you must either assign the new trimmed string to a new string variable, or assign it to the old string reference:
string link = message.Substring(message.IndexOf("/"), message.IndexOf("H") - 4);
link = link.Trim();


All of your trim() calls need to be modifed, as well as link.Replace("\r\n", " ");
Was This Post Helpful? 0
  • +
  • -

#3 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: socket error

Posted 23 March 2009 - 06:39 PM

and that also something intresting my pc closing mine connection lol
first i taught it was google that was closing connection therefore i check by getting response by creating another program that worked i dont why this one i not working i dont know why mine pc is closing mine socket lol

hey i found out the problem when i tried to print the message in the console the reason i saw it was sending same 32 bit to the stream and not the next one any ways to overcome that
this is going to be irritating

it was not that problem i guess its a missread

Edit:it was not that problem i guess its a missread

This post has been edited by prajayshetty: 23 March 2009 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: socket error

Posted 23 March 2009 - 07:19 PM


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,300);//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)
	{
		Socket s = serversocket;
		try
		{
			
			string str1 = hosturl(s);
			WebRequest w = (WebRequest)WebRequest.Create("http://www.jproject.ucoz.net");
			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)
			{
				Console.WriteLine(reading);
				
				clientsend.Append(Encoding.ASCII.GetString(b, 0, reading));
				Console.WriteLine(clientsend);
			  
				s.Send(b,reading,0);
			   
				sending += reading;

				reading = clientstream.Read(b,0, 32);
			}
		}
		catch (SocketException e)
		{
			Console.WriteLine(e.Message);
		}
		finally
		{

			if (s != null)
			{
				if (s.Connected)
				{
					serversocket.Close();
				}

			}
		}
	}
	

	 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();
	 }
}


This post has been edited by prajayshetty: 23 March 2009 - 07:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: socket error

Posted 24 March 2009 - 12:15 AM

can i implement a block to a particular user connecting to the server ?? any way any suggestion any pin points
it doesnt work with chrome l

This post has been edited by prajayshetty: 24 March 2009 - 01:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1