用select检测socket是否”ready”

    1. struct fd_set rdfs;
    2. struct timeval tv = {5 , 0}; // 设置5秒时间
    3. while
    4. {
    5. FD_ZERO(&rdfs);
    6. FD_SET(sockfd, &rdfs);
    7. if (select(sockfd+1, &rdfs, NULL, NULL, &tv) > 0) // socket就绪
    8. {
    9. recv() / recvfrom() // 从socket读取数据
    10. }
    11. }