close() 和 closesocket() 可以互换吗? [英] Are close() and closesocket() interchangable?

查看:121
本文介绍了close() 和 closesocket() 可以互换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到很多答案都说使用 close() 来销毁套接字,但我使用的指南来自 msdn 让我使用 closesocket().我想知道是否有区别,是否有理由使用其中之一.

I've seen a lot of answers here that say to use close() to destroy a socket but the guide I used from msdn has me using closesocket(). I'm wondering if there is a difference and if there are reasons to use one or the other.

在这两种情况下,我都看到了使用 shutdown() 的建议,这样一切都很好.

In both cases, I am seeing the suggestion to use shutdown() so that's all well and good.

推荐答案

close() 是一个 *nix 函数.它适用于任何文件描述符,*nix 中的套接字是文件描述符的一个示例,因此它也可以正确关闭套接字.

close() is a *nix function. It will work on any file descriptor, and sockets in *nix are an example of a file descriptor, so it would correctly close sockets as well.

closesocket() 是一个特定于 Windows 的函数,它专门用于套接字.Windows 上的套接字不使用 *nix 样式的文件描述符,socket() 而是返回内核对象的句柄,因此必须使用 closesocket() 关闭它.

closesocket() is a Windows-specific function, which works specifically with sockets. Sockets on Windows do not use *nix-style file descriptors, socket() returns a handle to a kernel object instead, so it must be closed with closesocket().

我觉得 BSD 套接字没有包含与 socket 函数对应的特定对应物,这很可耻,它可以在任何地方使用 - 但这就是生活.

I find it rather shameful that BSD-sockets do not include specific counterpart to socket function, which could be used anywhere - but such is life.

最后但并非最不重要的一点是,不要将 shutdown 将套接字与关闭套接字混淆.shutdown() 停止套接字上的传输,但套接字仍保留在系统中,并且与其关联的所有资源都保留.关闭socket后仍需关闭socket.

The last, but not the least, do not confuse shutdown'ing a socket with closing the socket. shutdown() stops transmission on a socket, but the socket remains in the system and all resources associated with it remain. You still need to close the socket after shutting it down.

这篇关于close() 和 closesocket() 可以互换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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