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/)可能會有幫助。 –