“取消关联"来自完成端口的套接字 [英] "Un-associate" socket from completion port

查看:31
本文介绍了“取消关联"来自完成端口的套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CreateIoCompletionPort() 用于将套接字与完成端口相关联.但是,当这个套接字关闭时,我需要从完成端口取消关联"它.我该怎么做?

CreateIoCompletionPort() is used to associate a socket with a completion port. However, when this socket is closed, then I need to "un-associate" it from the completion port. How can I do that?

推荐答案

与 I/O 完成端口关联的句柄在关闭句柄时从端口中删除.在网络套接字的情况下,通过调用 closesocket().

A handle that is associated with an I/O Completion Port is removed from the port when the handle is closed. In case of a network socket, the handle is closed by calling closesocket().

CreateIoCompletionPort 的文档包含对资源的说明处理:

The documentation for CreateIoCompletionPort contains remarks on resource handling:

FileHandle 参数中传递的句柄可以是任何支持重叠 I/O 的句柄.最常见的是,这是由 CreateFile<打开的句柄/strong> 函数使用 FILE_FLAG_OVERLAPPED 标志(例如,文件、邮件槽和管道).由其他函数创建的对象,例如 socket也可以与 I/O 完成端口相关联.有关使用套接字的示例,请参阅 AcceptEx.一个句柄只能与一个 I/O 完成端口相关联,并且在建立关联后,该句柄一直与该 I/O 完成端口相关联,直到它关闭.

The handle passed in the FileHandle parameter can be any handle that supports overlapped I/O. Most commonly, this is a handle opened by the CreateFile function using the FILE_FLAG_OVERLAPPED flag (for example, files, mail slots, and pipes). Objects created by other functions such as socket can also be associated with an I/O completion port. For an example using sockets, see AcceptEx. A handle can be associated with only one I/O completion port, and after the association is made, the handle remains associated with that I/O completion port until it is closed.

...

I/O 完成端口句柄以及与该特定 I/O 完成端口关联的每个文件句柄都称为对 I/O 完成端口的引用.当没有更多的引用时,I/O 完成端口被释放.因此,必须正确关闭所有这些句柄以释放 I/O 完成端口及其相关的系统资源.满足这些条件后,通过调用CloseHandle 函数.

The I/O completion port handle and every file handle associated with that particular I/O completion port are known as references to the I/O completion port. The I/O completion port is released when there are no more references to it. Therefore, all of these handles must be properly closed to release the I/O completion port and its associated system resources. After these conditions are satisfied, close the I/O completion port handle by calling the CloseHandle function.

这篇关于“取消关联"来自完成端口的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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