确定socket和fd之间 [英] Determine between socket and fd

查看:37
本文介绍了确定socket和fd之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在unix上一切都是文件函数read()write()的方法>close() 在 Win32 上不受支持.

我想模拟它,但不知道如何在 WinSocks2 上区分 socksocket 还是 fd.

//如果`sock`是网络套接字则返回1,//0 如果 `sock` 是文件描述符(包括 stdio、stderr、stdout),...//-1 在以上都不是int is_net_socket(int 袜子){//...?}

这应该像这样工作:

int mysock = socket(PF_INET, SOCK_STREAM, 0);int myfd = _open("my_file.txt", _O_RDONLY);printf("1: %d 2: %d 3: %d 4:%d\n",is_net_socket(mysock),//1is_net_socket(myfd),//0is_net_socket(stdin),//0is_net_socket(stderr));//0//应该打印 "1: 1 2: 0 3: 0 4:0"

<小时>

如何实现 is_net_socket 以使用它:

int my_close(int sock){#if ON_WINDOWS开关(is_net_socket(袜子)){情况1:返回closesocket(sock);案例0:返回_close(袜子);默认值://处理错误...}#别的返回关闭(袜子);#万一}

解决方案

不确定您是从哪里了解到 Windows 不允许您将 SOCKET 句柄用作文件的 - 正如明确说明的在 套接字句柄页面上:><块引用>

套接字句柄可以选择是 Windows 套接字 2 中的文件句柄.来自 Winsock 提供程序的套接字句柄可以与其他非 Winsock 函数一起使用,例如 ReadFile、WriteFile、ReadFileEx 和 WriteFileEx.

无论如何,关于如何在 Windows 上区分它们,请参阅函数 NtQueryObject,如果传递给它的句柄是一个打开的SOCKET,它将返回一个句柄名称\Device\Tcp.阅读此调用返回的结构的备注"部分.

请注意,此方法仅适用于 XP 及更高版本,并且会在 Windows 2000 上失败(我假设它已经足够老,不会影响您.)

On unix everything is a file approach of function read(), write(), close() is not supported on Win32.

I want to emulate it but have no idea how to distinguish when sock is socket or fd on WinSocks2.

//returns 1 if `sock` is network socket, 
//        0 if `sock` is file desriptor (including stdio, stderr, stdout), ...
//       -1 in none of above
int is_net_socket(int sock)
{
    // ...?
}

This should work as in :

int mysock  = socket(PF_INET, SOCK_STREAM, 0);
int myfd    = _open("my_file.txt", _O_RDONLY);

printf("1: %d    2: %d    3: %d    4:%d\n",
       is_net_socket(mysock),   //1
       is_net_socket(myfd),     //0
       is_net_socket(stdin),    //0
       is_net_socket(stderr));  //0

// should print "1: 1    2: 0    3: 0    4:0"


How to implement is_net_socket in order to use it as in:

int my_close(int sock)
{
#if ON_WINDOWS
    switch( is_net_socket(sock) ) {
        case 1: return closesocket(sock);
        case 0: return _close(sock);
        default: //handle error...
    }
#else
    return close(sock);
#endif
}

解决方案

Not sure where you're getting the idea that Windows won't allow you to use SOCKET handles as files - as clearly stated on the Socket Handles page:

A socket handle can optionally be a file handle in Windows Sockets 2. A socket handle from a Winsock provider can be used with other non-Winsock functions such as ReadFile, WriteFile, ReadFileEx, and WriteFileEx.

Anyways, as to how to distinguish between them on Windows, see the function NtQueryObject, which will return a handle name of \Device\Tcp if the handle passed to it is an open SOCKET. Read the "Remarks" section for the structure returned by this call.

Note that this approach only works XP and up, and will fail on Windows 2000 (which I'm assuming is old enough that it doesn't affect you.)

这篇关于确定socket和fd之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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