Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
fcntl()和F_GETFL:奇怪的结果 上下文_C_Sockets_File Descriptor - Fatal编程技术网

fcntl()和F_GETFL:奇怪的结果 上下文

fcntl()和F_GETFL:奇怪的结果 上下文,c,sockets,file-descriptor,C,Sockets,File Descriptor,我是一个自学成才的人,今天我得到了第一个使用c代码的套接字。 很高兴 问题 假设我们有sfd,一个套接字文件描述符 执行此代码时: flags = fcntl (sfd, F_GETFL, 0); printf("FD MODE 1 - %d\n",fcntl(sfd,F_GETFL)); flags |= O_NONBLOCK; s = fcntl (sfd, F_SETFL, flags); printf("FD MODE 2 - %d\n",fcntl(sfd,F_GETFL)); 它输

我是一个自学成才的人,今天我得到了第一个使用c代码的套接字。 很高兴

问题 假设我们有
sfd
,一个套接字文件描述符

执行此代码时:

flags = fcntl (sfd, F_GETFL, 0);
printf("FD MODE 1 - %d\n",fcntl(sfd,F_GETFL));
flags |= O_NONBLOCK;
s = fcntl (sfd, F_SETFL, flags);
printf("FD MODE 2 - %d\n",fcntl(sfd,F_GETFL));
它输出:

2
2050
但是我的fcntl linux.h说:

...
/* open/fcntl.  */
#define O_ACCMODE      0003
#define O_RDONLY         00
#define O_WRONLY         01
#define O_RDWR           02
#ifndef O_CREAT
# define O_CREAT       0100 /* Not fcntl.  */
#endif
#ifndef O_EXCL
# define O_EXCL        0200 /* Not fcntl.  */
#endif
#ifndef O_NOCTTY
# define O_NOCTTY      0400 /* Not fcntl.  */
#endif
#ifndef O_TRUNC
# define O_TRUNC      01000 /* Not fcntl.  */
#endif
#ifndef O_APPEND
# define O_APPEND     02000
#endif
#ifndef O_NONBLOCK
# define O_NONBLOCK   04000
....
问题: 即使使用逐位运算,我也无法得到最终得到2050或2的结果

有人帮我清除路径吗?

04000
(带前导零)是一个八进制整数文本,并且

   2 (decimal) =    2 (octal) = O_RDWR
2050 (decimal) = 4002 (octal) = O_RDWR | O_NONBLOCK
这意味着设置
O_NONBLOCK
标志可以正常工作

为便于与
O_XXX
定义进行比较,您可以将标志打印为 八进制数:

printf("FD MODE 2 - %#o\n", fcntl(sfd,F_GETFL));
// Output: FD MODE 2 - 04002

挑剔:不要称之为“脚本”,而只是“代码”。然而,一个精心设计的问题:1+谢谢alk!我现在就更正:)谢谢马丁,我错过了八进制符号!:)