Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想等待一段固定的时间(比如 3 秒)让 recvfrom() 接收一些数据,如果它没有收到任何东西(在这 3 秒内)我想退出我的程序怎么做?
调用选项setsockopt()以SO_RCVTIMEO在套接字上设置读取超时。如果超时触发,recvfrom()将返回 -1 和errno == EAGAIN/EWOULDBLOCK.
setsockopt()
SO_RCVTIMEO
recvfrom()
errno == EAGAIN/EWOULDBLOCK