proxy ServerSocket, ClientSocket


I'm trying to create a simple proxy server for educational purpose. By connecting a client browser trough the proxy to the internet, the data to and from te internet can be made visible for students. I've tried some to create a program based on the connect, read and write events but the sockets generate a lot of sync errors, so i've tried to keep the proxy code in one ServerSocket onread event. When i Connect to the ServerSocket the data will be read into a AnsiString buffer, but when I try to connect to the remote host with the ClientSocket the connection will not be established. Is this because I'm holding the socket with the onread event preventing the ClientSocket to connect or prevent to be set active.. or..? The used code is:

void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{ bool Data=0;
int a=0, i;
long len;
unsigned char* pCHAR,*pHOST;
unsigned short PORT=80;
char HOST[256]; //->init with while loop to ''
AnsiString S_HOST;
AnsiString HOST_IP = {""};
int integervar;
struct in_addr DNS_LOOKUP;
ClientSocket1->Active = FALSE;

Sleep((unsigned int) 1);
//Retrieve cocket information from client browser

//on serversocket read event, read local host data from socket
while(Socket->ReceiveLength()>0 && Socket->Connected)
{ BufToInet += (Socket->ReceiveText() ) ;

if(BufToInet.Length()) //if received data from local host
Memo1->Lines->Add("Local Host:, "+BufToInet);

pCHAR = BufToInet.c_str();
//create pointer to array
if( ( *(pCHAR)=='G') && (*(pCHAR+1)=='E') && (*(pCHAR+2)=='T') ) //if packet begins with "GET"

if( ( *(pCHAR)=='P') && (*(pCHAR+1)=='O') && (*(pCHAR+2)=='S') && (*(pCHAR+3)=='T')) //if packet begins with "POST"
{ //search for http, https, smtp..etc


while( ((*(pCHAR)!='H') || (*(pCHAR+1)!='o') || (*(pCHAR+2)!='s') || (*(pCHAR+3)!='t') || (*(pCHAR+4)!=':') ) && (i<(len-5)) ) //Copy adres
{ pCHAR++;


while( (*pCHAR != '
') && (*pCHAR != '
') )
{ HOST[i++] = *pCHAR;

//First resolve host server IP to prevent communication timeouts from DNS servers

DNS_LOOKUP = ClientSocket1->Socket->LookupName(S_HOST); // <--ERROR, needs ansistring:c_str pointer to ansistring.
integervar = DNS_LOOKUP.S_un.S_addr;

pHOST = (char*)&integervar;
HOST_IP= IntToStr(*(pHOST))+"."+IntToStr(*(pHOST+1))+"."+IntToStr(*(pHOST+2))+"."+IntToStr(*(pHOST+3));

if(HOST_IP != "") //if remote host found
{ Memo1->Lines->Add("Resolving IP: "+HOST_IP +", "); //

ClientSocket1->Host = HOST_IP;
ClientSocket1->Port = 80;
ClientSocket1->Active = TRUE; //Connect to remote host
Memo1->Lines->Add("Proxy connecting to: "+S_HOST+", ");

while(!ClientSocket1->Socket->Connected); //wait for connection to be established or time out
if(ClientSocket1->Socket->Connected) //if connection has been established
while( BufToInet.Length() - ClientSocket1->Socket->SendText(BufToInet) );//send all data to Remote host

while(ClientSocket1->Socket->ReceiveLength() ==0); //wait for data or time out

while(ClientSocket1->Socket->ReceiveLength()>0 && ClientSocket1->Socket->Connected) //read data from remote host
{ InetToBuf += (Socket->ReceiveText() ) ; //add data to string

if(InetToBuf.Length()); //if data received
while(InetToBuf.Length() - ServerSocket1->Socket->Connections[0]->SendText(InetToBuf) ); //send all data to local host
Memo1->Lines->Add("Remote Host: , "+InetToBuf); //add data to memofield
while( ( 15 + S_HOST.Length() ) - ServerSocket1->Socket->SendText("Host not found " + S_HOST));
Memo1->Lines->Add("Host "+S_HOST + " not found. Dicsonnected, ");

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!