【发布时间】:2016-10-26 19:39:07
【问题描述】:
我的管道有问题。我正在使用::_pipe 函数创建管道并希望在其上使用select 但WSAGetLastError 返回10038 并且kua 是-1。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <Winsock2.h>
#include <Windows.h>
#include <Winbase.h>
#include <FileAPI.h>
#include <io.h>
#include <wchar.h>
int main()
{
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
int fd[2];
_pipe(&fd[2], 65536, 0);
FD_SET set;
FD_SET((unsigned int) (fd[0]), (&set));
timeval time;
time.tv_sec = 1;
time.tv_usec = 0;
int kua = select(0, &set, NULL, NULL, &time);
printf("last ->%d\n", WSAGetLastError());
fflush(stdout);
}
请不要注意标题,我读过一些有相同错误的主题,但没有一个是这样的(管道以这种方式创建)。希望你能帮忙。谢谢。
编辑
我现在稍微更改了代码_pipe(fd, 65536, 0);,但在这种情况下它给了我段错误。
【问题讨论】:
-
来自文档:select 函数返回准备就绪并包含在 fd_set 结构中的套接字句柄的总数,如果时间限制过期,则返回零,如果发生错误,则返回 SOCKET_ERROR。如果返回值为 SOCKET_ERROR,则可以使用 WSAGetLastError 来检索特定的错误代码。 您没有检查返回值。为什么不呢?
-
对不起,我现在检查了,它的 -1 我会编辑
-
另外,
fd[2]不在数组末尾,因此您正在破坏堆栈。您甚至可以通过管道进行选择吗? -
您在调用
_pipe时也不会检查错误。真的不是一个很好的节目。没有错误检查、堆栈损坏和几乎无法更清晰的错误代码。系统告诉您您正在传递不是套接字的东西。显而易见的结论:管道不是套接字。你认为 10038 是什么意思? -
您是否考虑过查找 Winsock 错误 10038?
标签: c winapi error-handling pipe