我正在使用UDP服務器和客戶端,並且我正在將數據從客戶端發送到服務器,反之亦然。但正如你們知道UDP的「發送和祈禱」,並希望它到達。從for循環超時
我想要一個for循環,如果時間超過'X'秒即使尚未完成也會退出。因爲你不能保證一切都會到來。
有沒有簡單的方法做到這一點,而不會增加我的for循環?
for (int i = 0; i < split ; i++){
iResult = recvfrom(RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR *)& SenderAddr, &SenderAddrSize);
cout << "Receiving " << BufLen << endl;
sum += BufLen;
//IF X seconds goes cancel loop with
//timeout something something
}
獲取時間,檢查時間差,從循環中斷 –
'select/poll' ..? –
它是操作系統特定的。標準C++ 11不知道套接字。因此,如果編碼爲Linux,請閱讀[高級Linux編程](http://www.makelinux.net/alp/)。參見[poll(2)](http://man7.org/linux/man-pages/man2/poll.2.html),[syscalls(2)](http://man7.org/linux/man- pages/man2/syscalls.2.html),[time(7)](http://man7.org/linux/man-pages/man7/time.7.html),[socket(7)](http: //man7.org/linux/man-pages/man7/socket.7.html),[udp(7)](http://man7.org/linux/man-pages/man7/udp.7.html) ,等... –