WinAPI:InternetCloseHandle 函数关闭句柄但不关闭连接 [英] WinAPI: InternetCloseHandle function closes the handle but not the connection

查看:41
本文介绍了WinAPI:InternetCloseHandle 函数关闭句柄但不关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用 wininetInternetOpenUrlA,然后是 wininetInternetReadFile,完成后我调用 wininetInternetCloseHandle 返回 True.这意味着句柄已成功关闭,但连接仍处于已建立状态.

I call wininetInternetOpenUrlA, then wininetInternetReadFile and when I'm done I call wininetInternetCloseHandle which returns True. That means that the handle successfully closed, but the connection is still in established state.

为什么我调用 wininetInternetCloseHandle 时连接没有关闭?

Why doesn't the connection close when I call wininetInternetCloseHandle ?

推荐答案

WinInet 尝试尽可能重用套接字,因此即使您释放句柄,它也可以选择保持套接字处于活动状态,为下一次调用互联网开放.大多数情况下,这是一件好事,您无需担心.

WinInet tries to re-use sockets where it can, so even when you release the handle it can choose to keep the socket active, ready for the next call to InternetOpen. Most of the time this is a good thing and you don't need to worry about it.

如果您真的需要立即关闭它,您可以通过在最终 InternetCloseHandle 之后调用 InternetSetOption 来欺骗 WinInet 这样做:

If you really need it to be closed immediately, you can fool WinInet into doing this by calling InternetSetOption after your final InternetCloseHandle:

...
InternetCloseHandle(hInternet);
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);

这样做会告诉 WinInet 全局 WinInet 设置已更改(例如在注册表中),因此它别无选择,只能关闭所有套接字并自行重置.但是,这显然不是预期的用途,如果您与 WinInet 建立大量连接,则会对性能产生一些影响.

Doing this tells WinInet that global WinInet settings have changed (e.g. in the Registry) so it has no choice but to close all sockets and reset itself. However this obviously is not the intended usage and will have some performance impact if you are making lots of connections with WinInet.

这篇关于WinAPI:InternetCloseHandle 函数关闭句柄但不关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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