关闭/清理“混合”的文件描述符/套接字 [英] Closing/cleaning up "mixed" file descriptors / sockets
问题描述
当我使用accept()创建套接字并使用fdopen()从中创建一个FILE时,我需要做些什么来清理所有内容?我需要在套接字上的FILE,shutdown()和close()上执行fclose(),还是只需要shutdown()和/或close()或fclose()?如果我不做fclose(),我是否必须手动释放()FILE指针?
When I create a socket using accept() and make a FILE out of it using fdopen(), what do I have to do to clean everything up? Do I need to do fclose() on the FILE, shutdown() and close() on the socket, or only the shutdown() and or close() or fclose()? If I don't do fclose(), do I have to free() the FILE pointer manually?
推荐答案
来自man fdopen :
From man fdopen:
文件描述符未被复制,并且当fdopen()创建的流关闭时将关闭文件描述符
The file descriptor is not dup’ed, and will be closed when the stream created by fdopen() is closed
所以我只使用fclose(),它也会关闭底层文件描述符。我不知道是否需要shutdown()。
So I would just use fclose(), which also closes the underlying file descriptor. I don't know whether shutdown() is needed, either.
这篇关于关闭/清理“混合”的文件描述符/套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!