Windows套接字无法正确关闭-浏览器的奇怪行为 [英] Windows Socket do not close properly - Strange Behaviour with browser

查看:71
本文介绍了Windows套接字无法正确关闭-浏览器的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我已经使用VC ++(任何一种)构建了一个简单的端口转发工具.

实际上,我也在创建一个http代理服务器,并且使用此程序来查看其流量.它将所有HTTP请求记录在一个文件中,并帮助我进行调试.

它侦听端口6000,位于浏览器和HTTP代理服务器之间.
在浏览器代理设置中,我输入127.0.0.1 6000
我的端口转发程序将所有流量重定向到http代理服务器端口8080
并记录所有http请求.

问题是当我使用浏览器打开页面时..它会永远加载下去...
从端口转发日志中,我可以看到已从代理接收到完整的HTML内容并将其发送到浏览器. 但是浏览器无法渲染它..
我检查了代码,并完美关闭了所有套接字,并在
之前使用了shutdown() 但是当我在那一刻杀死我的端口转发程序时,浏览器会显示整个页面.

我也尝试了so_linger选项....但是没有运气....

你能给点儿亮点吗?
详细信息如下:
===================

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:
===================

   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


我已经将它与FTP客户端/服务器一起使用了.它工作正常.
请给点灯.非常感谢..

pada


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

pada

推荐答案

我知道您说过您关闭了插座propeley,但这恰恰是您看到的行为.

获取wireshark的副本,并确保发送了FIN/ACK和FlN. wirehark无法看到发送到127.0.0.1的内容,因此您将需要另一台计算机.
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.


pada123写道:
pada123 wrote:


如果您有时间,可以在计算机上运行它,并检查它是否对您有很大帮助....我不知道我做过的代码中是否还有其他错误...以及是否插口您在TCP级别中提到的关闭问题...我应该采取什么步骤解决该


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


就像我说的.在transport.cpp中没有对shutdown()的调用.

我没有在我的计算机上运行您的代码(它不是私人的),但是有些东西即使不运行也可以看到.
首先,在MSDN页面上查看 recv [ ^ ]以及发送 [


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.


在从代理接收的函数CThread()中,您需要检查recv()的返回值是否为0,因为这意味着代理已关闭连接,然后需要通过调用将其传递到Web浏览器上到
关机() [


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



这只是一个开始,您还应该检查send()的返回值.

最后,你有



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.


这篇关于Windows套接字无法正确关闭-浏览器的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆