版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、網(wǎng)絡(luò)編程 Socket 之 UDP( 三 )超時設(shè)置和非阻塞前面遺留的兩個問題:1. 一個已連接 UDP 套接字能且僅能與一個對端交換數(shù)據(jù)報, 那么客戶端發(fā)送廣播的時候如何防止 recvfrom 方法阻塞;2. 服務(wù)端忙的時候,已連接的 UDP 套接字也會被阻塞。 方法一:設(shè)置超時UNP 14.2There are three ways to place a timeout on an I/Ooperation involving a socket:1.Callalarm, which generates theSIGALRMsignal when the specified time has
2、expired. This involves signal handling, which can differ from oneimplementation to the next, and it may interfere with other existing calls toalarmin the process.2.Block waiting for I/O inselect, which has a time limit built-in, instead ofblocking in a call toread orwrite.3. Use the newerSO_RCVTIMEO
3、 andSO_SNDTIMEO socket options. The problemwith this approach is that not all implementations support these two socket options.第一種方法我們可以用 ios 提供的定時器來實現(xiàn); 第二種方法在 recvfrom 方法調(diào)用前添加代碼如下, 這里是設(shè) 置 3 秒超時:cpp view plain copy <span style=font-size:12px;> fd_set read_fdset;struct timeval timeout;FD_ZERO(&
4、amp;read_fdset);FD_SET(socketFileDescriptor, &read_fdset); timeout.tv_sec = 3;timeout.tv_usec = 0;int ret; do ret = select(socketFileDescriptor + 1, &read_fdset, NULL,NULL, &timeout); while (ret <if0 && errno = EINTR);(ret = 0) printf(time outn);return -1; </span>第三種方法代碼
5、如下:cpp view plain copy <span style=font-size:12px;> struct timeval timeout = 3,0;setsockopt(socketFileDescriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(structtimeval);</span> 設(shè)置 3 秒超時,如果 3 秒以后無法 讀到數(shù)據(jù), recvfrom 方法返回 -1 ,而且 errno 返回 EWOULDBLOCK 錯誤cpp view plain copy <spa
6、n style=font-size:12px;> n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); if (n < 0) if (errno = EWOULDBLOCK) printf(time outn); return -1; else err_sys(recvfrom error); </span> 方法二: 使 用非阻塞式 IOUNP 16.1With a nonblocking socket, if the input operation cannot be satisfied (at leasto
7、ne byte of data for a TCP socket or a complete datagram for a UDP socket),we see animmediate return with an error of EWOULDBLOCK. 首先 設(shè)置非阻塞套接字:cpp view plain copy <span style=font-size:12px;> int ret; int flags = fcntl(socketFileDescriptor,F_GETFL);if (flags = -1)printf(fcntl error);return -1;f
8、lags |= O_NONBLOCK;ret =fcntl(socketFileDescriptor, F_SETFL, flags);if (ret =-1)printf(fcntl error);return-1; </span> 然后運行, 打印信息如下: >> send success>> recvfrom fail-errno = 35>> send success>> recvfrom fail-errno = 35>> send success>> recvfrom fail-errno = 35
9、>> send success>> recvfrom fail-errno = 35>> send success>> server said:01&c0:5e:79:fc:0e:0c&192.168.0.1&iS hare-R1B011D2199>> send success>> recvfrom fail-errno = 35>> send success>> recvfrom fail-errno = 35>> send success>> re
10、cvfrom fail-errno = 35 會發(fā)現(xiàn) recvfrom 方法返 回失敗的概率很大, 而且 errno 返回 EWOULDBLOCK 錯誤, 這是因為在非阻塞的情況下有可能因為服務(wù)器阻塞導(dǎo)致客 戶端這邊的 UDP 套接字的接收緩沖區(qū)為空, 因此為了解決 這個問題我們在 recvfrom 方法之前添加如下代碼:cpp view plain copy <span style=font-size:12px;> fd_set read_fdset;FD_ZERO(&read_fdset);FD_SET(socketFileDescriptor, &read_f
11、dset);int ret; do ret = select(socketFileDescriptor + 1, &read_fdset, NULL, NULL, NULL); while (ret < 0 && errno = EINTR);if (ret= 0) printf(time outn);return -1; </span> 然后運行,打印信息如 下: >> send success>> server said:01&c0:5e:79:fc:0e:0c&192.168.0.1&iS hare
12、-R1B011D2199>> send success>> serversaid:01&c0:5e:79:fc:0e:0c&192.168.0.1&iS hare-R1B011D2199>> send success>> serversaid:01&c0:5e:79:fc:0e:0c&192.168.0.1&iS hare-R1B011D2199>> send success>> serversaid:01&c0:5e:79:fc:0e:0c&192.168.0.1&iS hare-R1B011D
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 宿舍樓房出租合同
- 商標轉(zhuǎn)讓合同樣本
- 房地產(chǎn)交易經(jīng)紀合同
- 股份質(zhì)押合同
- 個人抵押借款合同
- 商品房裝修工程合同范本
- STEAM理念下初中數(shù)學(xué)項目式學(xué)習(xí)的設(shè)計研究
- 面向小行星探測的著陸器附著鉆進錨固力學(xué)特性研究
- 2025年安陽道路貨運駕駛員從業(yè)資格證考試題庫完整
- 高速光通信系統(tǒng)中信號識別方法研究
- 《教科版》二年級科學(xué)下冊全冊課件(完整版)
- (2024年)《處方管理辦法》培訓(xùn)課件
- 人工智能在化工生產(chǎn)安全中的應(yīng)用
- 2023年6月浙江高考政治試卷真題解讀及答案解析(課件)
- 銷售部廉政培訓(xùn)課件
- 三年級計算題三位數(shù)乘一位數(shù)練習(xí)300題帶答案
- 商務(wù)服務(wù)業(yè)的市場細分和定位策略
- 財政學(xué)論文我國財政支出存在的問題及改革建議
- 2022年湖南高速鐵路職業(yè)技術(shù)學(xué)院單招數(shù)學(xué)模擬試題及答案解析
- 小學(xué)生必備古詩
- 人教版英語八年級上冊單詞默寫表
評論
0/150
提交評論