在Unix 一切文件的功能read()
,write()
方法,close()
不支持在Win32。确定和FD
我想效仿,但不知道如何当sock
是插座或WinSocks2 FD区分。
//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)
{
// ...?
}
这应该工作,如:
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"
如何实现is_net_socket
为了使用它作为:
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
}
人使用_fstat()? – DinGODzilla