名为 fd 的 WSAPoll :: 需要如何重写它的建议
我有一些 POSIX
C
代码,我正在将其移植到 Windows (WinSocks 2.2),并且我在 MS 实现方面遇到问题 (不仅)poll()
。
我对 POSIX sockets
有一些经验,但我对 WinSock2 还很陌生,我在 MSDN 上没有找到任何有用的线索,所以我在这里问:“如何制作与此示例代码在 Windows 上的行为相同吗?”
static int connect_to_addr(char *address, char *port)
{
struct addrinfo hints;
struct addrinfo *addr;
int fd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(address, port, &hints, &addr) != 0) return -1;
fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (fd < 0) return -1;
if (connect(fd, addr->ai_addr, addr->ai_addrlen) < 0) return -1;
freeaddrinfo(addr);
return fd;
}
函数 connect_to_addr()
只是为了演示第二个字段上的 fd
的样子。
WSAStartup(...)
...
pollfd cinfd[2];
fds[0].fd = _fileno(stdin); //THIS is probably not supported on win32
fds[0].events = POLLIN;
fds[1].fd = f_connect(some_addr, some_port); //OK
fds[1].events = POLLIN;
while (1) {
res = WSAPoll(fds, 2, -1); //returns 1
if (fds[0].revents & (POLLIN | POLLHUP)) { //fds[0].revents == POLLNVAL !! problem
char buf[1024];
int n, w, i;
n = read(fds[0].fd, buf, 1024);
...
}
if (fds[1].revents & POLLIN) {
char buf[1024];
int n, w, i;
n = recv(fds[1].fd, buf, 1024, 0);
...
}
}
如何在WinSocks下实现这个常见的习惯用法?感谢您的建议。
更好的是,从 Vista 开始,WSAPoll() 就位于 ws2_32.dll 中;如何让它在XP下运行?
I've some POSIX
C
code that I'm porting to windows (WinSocks 2.2) and I'm having problems with MS implementation of (not only) poll()
.
I have some experience with POSIX sockets
, but I'm quite new to WinSock2, I haven't found any helpful leads on MSDN, so I ask here: "How to make equivalent behaviour to this sample code on windows?"
static int connect_to_addr(char *address, char *port)
{
struct addrinfo hints;
struct addrinfo *addr;
int fd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(address, port, &hints, &addr) != 0) return -1;
fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (fd < 0) return -1;
if (connect(fd, addr->ai_addr, addr->ai_addrlen) < 0) return -1;
freeaddrinfo(addr);
return fd;
}
Function connect_to_addr()
is just for demonstration what fd
on second field looks like.
WSAStartup(...)
...
pollfd cinfd[2];
fds[0].fd = _fileno(stdin); //THIS is probably not supported on win32
fds[0].events = POLLIN;
fds[1].fd = f_connect(some_addr, some_port); //OK
fds[1].events = POLLIN;
while (1) {
res = WSAPoll(fds, 2, -1); //returns 1
if (fds[0].revents & (POLLIN | POLLHUP)) { //fds[0].revents == POLLNVAL !! problem
char buf[1024];
int n, w, i;
n = read(fds[0].fd, buf, 1024);
...
}
if (fds[1].revents & POLLIN) {
char buf[1024];
int n, w, i;
n = recv(fds[1].fd, buf, 1024, 0);
...
}
}
How to implement this common idiom under WinSocks? Thanks for suggestions.
Better yet, WSAPoll() is in ws2_32.dll since Vista; how to make it work under XP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最好使用 WaitForMultipleObjects() 来等待 stdin/sock。
You have better to use WaitForMultipleObjects() for waiting stdin/sock.