【问题标题】:select returns 0 although packets returned, i did reset the readset every time before calling select尽管数据包返回,但选择返回 0,但我在每次调用 select 之前都重置了 readset
【发布时间】:2013-04-28 21:33:26
【问题描述】:

我编写了一个 ping 程序,将 syn 数据包发送到目标 IP 地址 1.1.1.1 端口 0。我使用了 wireshark,我看到返回的数据包带有 RST/ACK 标志。但问题是当我使用 select() 尝试读取套接字时,select() 总是返回 0。所以我不知道如何调试程序。我确定我每次都重置 fd_set 读取集。有什么我错过的吗? 我发现 1.1.1.1 的回复 在 0.0003 秒内返回 使用 wireshark 我设置的 timeout1 秒(1000 毫秒) 这是代码

void readloop() {
    ....
    while(nsent < 4) {
        send_v4();
        wait_for_reply(1000);    // wait for 1 second;
    }
}

int wait_for_reply(long wait_time) {
....
    result = recving_time(...);
    if(result < 0)  // because time out
        return 0;
....
}

int recving_time(...) {
......
    fd_set readset;
select_again:
    set timeout value to structure *to*

    FD_ZERO(&readset);
    FD_SET(sockfd, &readset);
    readable = select(sockfd+1, &readset, NULL, NULL, &to);
#ifdef DEBUG
    fprintf(stderr, "readable is %d\n",readable); // **Why readable always be 0 although packets returned.** 
#endif
    if(readable < 0) {
        if(errno == EINTR)
            goto select_again;
    else {
        perror("select() error");
        exit(1);
    }   

    if(readable == 0)  {
        return -1;
    }
    ......
}

我不确定这里是否有足够的代码来理解我要说的内容。如果你能帮助我,我将非常感激

【问题讨论】:

  • 你实际上不能使用端口 0 -- 如果你绑定到端口 0,内核会为你分配一个未使用的端口,如果内核看到数据包发送到一个不存在的端口(例如为 0),它将以 RST/ACK 响应,告诉发送它的人没有这样的端口...
  • @ChrisDodd 其实我要写的程序类似命令: hping3 -S 1.1.1.1 hping send syn packet to port 0. and get rst/ack reply to determine the host is在线

标签: c select


【解决方案1】:

Select() 返回描述符集中包含的就绪描述符的数量,如果发生错误,则返回 -1。 如果超过时间限制,select() 返回 0。

您的超时(您没有显示设置)似乎正在过去。

【讨论】:

  • 感谢您的回复,但我发现使用 wireshark 在 0.0003 秒内返回了来自 1.1.1.1 的回复
  • @MadFrog 因为只有一次 select 返回 0 是超时,所以它似乎在更短的时间内超时。
猜你喜欢
  • 2022-08-17
  • 1970-01-01
  • 2021-03-18
  • 2016-10-19
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多