Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi friends,

I have built a simple port forward tool using both VC++ (any)

Actually I am also creating a http proxy server and for viewing its traffic I use this program. It logs all HTTP request in a file and help me to debug.

it listen to port 6000 and sits between browser and HTTP proxy server.
in browser proxy settings i enter 127.0.0.1 6000
my port forward program redirect all traffic to http proxy server port 8080
and logs all http request.

Problem is when i open a page using a browser ..It keeps on loading forever...
from port forward log i can see that full HTML content are received from proxy and sent to browser..
but browser somehow not rendering it..
I checked my code and i perfectly closing all socket and used shutdown() before
But when i kill my port forward program at that very moment browser display the whole page.

I tried so_linger option also....but no luck....

Can you pls give some light on it.
Here is the details:
===================
MSIL
   http://sourceforge.net/projects/dnsserversimple/files/PORT-Forword.zip/download
1. Above link is my  runnable vc++ code for port forward program . (transport.cpp) download it and run in your vc++
It is a zip file.
2. It listen to local port 6000  and forward traffic  to local HTTP proxy port  8080
3.
For testing purpose if you dont have small  HTTP proxy ..please you can use following tiny HTTP proxy server.
http://www.eternallines.com/httpproxy/elHTTPProxy100.zip
Run it ..Start it -by default it will run on port 8080.
4. Now point your browser to 127.0.0.1  6000  for all protocol.
5. Now try to open following web pages ( though you can try any web page....I found very few web site opens)
http://torrentz.eu/b60c09b9924908badb50036c8387dc2b2b180e76
http://yahoo.com
https://cms.paypal.com
https://www.alertpay.com
https://www.clickbank.com
etc.
6. Pages will keep on loading for ever ...though browser has received full response....Now suddenly kill transport.exe..
   you will see 95% HTML contents are rendered by browser...
but few simple  website like - google.com opens

I have used it with FTP client/server..it worked fine.
Please give some light. many Thanks in advance..

pada
Posted
Updated 23-Mar-11 7:37am
v2

I know you said you closed the socket propeley, but this is exactly the behaviour you would see you weren't.

Get a copy of wireshark and make sure that a FIN/ACK and FlN are getting sent. wireshark cannot see what is sent to 127.0.0.1 tho, so you will need another computer.
 
Share this answer
 
Comments
pada123 24-Mar-11 1:31am    
Brock - Thanks for your reply. I have mentioned very clearly how to replicate the issue.And the code base is having a single small file. If you have some time you can run that in your machine and check it will be very help full....I dont know if there is other mistake in code i have done...and if it socket closing problem as you mention in TCP level...what step i should take to resove that - thanks for your time - pada
pada123 wrote:

If you have some time you can run that in your machine and check it will be very help full....I dont know if there is other mistake in code i have done...and if it socket closing problem as you mention in TCP level...what step i should take to resove that

Like I said. There is no call to shutdown() in transport.cpp.

I'm not running your code on my computer (its nothing personal) but there are some things that I can see without running it.
Firstly check out the MSDN page for recv[^] and also send[^], in particular check out the return values:
If the connection has been gracefully closed, the return value is zero.

In the function CThread() where you are receiving from the proxy, you need to check the return value of recv() for 0 because that means the proxy has closed the connection, you then need to pass that onto the web browser with a call to shutdown()[^]
if(FD_ISSET(*(con->csock),&fd)) {
	char buf[1024];
	int len=recv(*(con->csock),buf,1024,0);
	if (len == 0) shutdown(*(con->csock), SD_BOTH);
	if(len<=0) return 0; //This is correct
	//buf[len]='\0';
	printf("remot->local    %d B \r\n",len);
	send(*(con->lsock),buf,len,0);			
}
if(FD_ISSET(*(con->lsock),&fd)) {
	char buf[1024];
	int len=recv(*(con->lsock),buf,1024,0);
	if (len == 0) shutdown(*(con->lsock), SD_BOTH);
	if(len==-1) return 0; //This is NOT correct. Should be if(len<=0) as it is in the previous IF
	//buf[len]='\0';
	printf("local->remote    %d B\r\n",len);
	send(*(con->csock),buf,len,0);
}


This is just a start, you should be checking the return from send() as well.

Lastly, you have
HANDLE hand=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)CThread,(LPVOID)&tsock,0,NULL);
WaitForSingleObject(hand,INFINITE);

Unless you plan on expanding this later, it is pointless.
 
Share this answer
 
Comments
pada123 24-Mar-11 2:29am    
Thanks Brock for your time and knowledge. Though I used shutdown ( not present in this code) i will check that as per your suggestion and will let you know..Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900