1
了我在我移植到Windows(WinSocks 2.2)一些POSIX
C
代码和我在与MS执行情况(不仅)poll()
问题。WSAPoll名为FD ::需要建议如何改写
我有POSIX sockets
一些经验,但我很新的WinSock2,我还没有发现在MSDN上任何有用的线索,所以我在这里问:“如何使相当于行为此示例代码上视窗?”
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下工作?
[This](http://tangentsoft.net/wskfaq/)可能会有帮助。 –