名为 fd 的 WSAPoll :: 需要如何重写它的建议

发布于 2024-11-16 18:15:57 字数 1704 浏览 3 评论 0原文

我有一些 POSIXC 代码,我正在将其移植到 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 POSIXC 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神爱温柔 2024-11-23 18:15:57

您最好使用 WaitForMultipleObjects() 来等待 stdin/sock。

HANDLE h[2];
h[0] = GetStdHandle(STD_INPUT_HANDLE);
h[1] = sock;

while (1) {
    DWORD ret;
    ret = WaitForMultipleObjects(2, h, FALSE, 0 /* wait value you want */);

    if (ret == WAIT_OBJECT_0) {
        // munipulating stdin.
    }
    if (ret == WAIT_OBJECT_0 + 1) {
        // munipulating sock.
        // then call recv.
    }
}

You have better to use WaitForMultipleObjects() for waiting stdin/sock.

HANDLE h[2];
h[0] = GetStdHandle(STD_INPUT_HANDLE);
h[1] = sock;

while (1) {
    DWORD ret;
    ret = WaitForMultipleObjects(2, h, FALSE, 0 /* wait value you want */);

    if (ret == WAIT_OBJECT_0) {
        // munipulating stdin.
    }
    if (ret == WAIT_OBJECT_0 + 1) {
        // munipulating sock.
        // then call recv.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文