Socket編程小結(續)

來源:互聯網
上載者:User

font-family:宋體;mso-ascii-theme-font:minor-fareast;mso-fareast-font-family:宋體;
mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">1.
宋體;mso-ascii-theme-font:minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:
minor-fareast;mso-hansi-theme-font:minor-fareast">重用已使用的地址

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">問題描述:在剛剛關閉了測試程式後,再啟動伺服器時提示bind失敗,返回錯誤EADDRINUSE。

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">原因分析:通訊端(主動關閉一端)在關閉通訊端後會停留在TIME_WAIT狀態一端時間,由於我在同一機器上同時運行用戶端與伺服器,故伺服器在重新啟動執行bind時,可能上次關閉串連還沒有完成,串連依然存在,故bind失敗。通過設定套介面的SO_REUSEADDR可重用已綁定的地址,通常所有的TCP伺服器都應該指定本套介面選項。具體方法為:

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast">int flag = 1;

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast">setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag,
sizeof(flag));

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">2
mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">.IO地址複用

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">直接調用read/write讀寫套介面和先調用select/poll在調用read/write都屬於阻塞IO,只不過前者阻塞在讀寫系統調用上,而者阻塞在select/poll上。由於select需要兩個系統調用,IO複用還稍有劣勢,使用select/poll的優勢在於我們可以等待多個描述字就緒。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast">IOmso-ascii-theme-font:minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:
minor-fareast;mso-hansi-theme-font:minor-fareast">複用的編程模型通常為:(以poll為例,應用執行個體請參考UNP第158頁)

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">1.  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">建立一個pollfd結構數組,數組長度為進程可能開啟的最大描述符個數,可簡單的使用OPEN_MAX
<limits.h>。

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">2.  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">置數組的第一個元素為監聽通訊端的就緒條件,並將其它的元素都清空。

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">3.  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">調用poll,等待poll返回。

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">4.  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">對於每一個已就緒的描述字:

mso-bidi-font-family:Wingdings">l  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">如果是監聽描述字,則調用accept,得到串連描述字,並在pollfd數組第一個空位中加入串連描述字的就緒條件,並將就緒描述字數目減1,當減到0時轉到3。

mso-bidi-font-family:Wingdings">l  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">如果是串連描述字,則接受來自該描述字的請求資訊,並發送響應資訊,將該描述字從pollfd數組中移除,並將就緒描述字數目減1,當減到0時轉到3。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

font-family:宋體;mso-ascii-theme-font:minor-fareast;mso-fareast-font-family:宋體;
mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">3.
宋體;mso-ascii-theme-font:minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:
minor-fareast;mso-hansi-theme-font:minor-fareast">同一地址啟動TCP與UDP服務

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">1. 宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">建立TCP通訊端,並綁定地址。

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">2. 宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">建立UDP通訊端,並綁定地址。

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">3. 宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">調用select/poll檢查TCP、UDP通訊端是否就緒。

mso-bidi-font-family:Wingdings">l  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">如果TCP通訊端可讀,則調用accept擷取串連通訊端,讀取並響應請求。

mso-bidi-font-family:Wingdings">l  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">如果UDP通訊端可讀,則直接讀取請求,並發送響應。

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">具體應用執行個體參見UNP第223頁。

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.