tcp ip seletc模式詳解

2021-08-18 16:38:05 字數 1447 閱讀 6520

前述:

linux下的i/o復用模型目前很多都已經不用select函式了,而是用epoll,但是為什麼還需要了解select程式設計呢,其實是從兩個方面考慮的:一是為了通過select去理解epoll,而是非阻塞的connect函式也會用到select函式。

函式原型:

[cpp]view plain

copy

select(2)                  linux programmer's manual                 select(2)  

name  

select,  pselect,  fd_clr,  fd_isset, fd_set, fd_zero - synchronous i/o  

multiplexing  

synopsis  

/* according to posix.1-2001 */

#include 

/* according to earlier standards */

#include 

#include 

#include 

intselect(

intnfds, fd_set *readfds, fd_set *writefds,  

fd_set *exceptfds, struct

timeval *timeout);  

void

fd_clr(

intfd, fd_set *set);  

intfd_isset(

intfd, fd_set *set);  

void

fd_set(

intfd, fd_set *set);  

void

fd_zero(fd_set *set);  

相關的函式比較多,先來說說select函式的引數:

(1) timeout:超時時間

struct timeval     

/* 檢查可讀或可寫條件,呼叫getsockopt取得套接字的待處理錯誤,如果建立成功,該值將為0 */

if(fd_isset(sockfd, &rset) || fd_isset(sockfd, &wset))  else

/* 恢復套接字的檔案狀態標誌並返回 */

done:    

fcntl(sockfd, f_setfl, flags); /* restore file status flags */

if(error)     

return

(0);    

}    

intmain()    

printf("connect success!\n"

);    

return

0;    

}    

參考:《unix網路程式設計》·卷1

詳解設計模式 外觀模式

外觀模式 facade 為子系統中的一組介面提供乙個一致的介面,facade 模式定義了乙個高層介面,這個介面使得這一子系統更加容易使用。隱藏系統的複雜性,並向客戶端提供了乙個客戶端可以訪問系統的介面。降低訪問複雜系統的內部子系統時的複雜度。類圖 在客戶端和複雜系統之間再加一層,將呼叫順序 依賴關係...

設計模式 Builder模式詳解

builder模式也叫做建造者模式,是設計模式的一種,就是將複雜物件的建立變得簡單明瞭,使物件與他的表示進行分離,使得同樣的建立過程,可以建立不同的物件.我們這裡講變種 builder模式 更加簡單,明了 並非真正意義上的builder模式 這種模式的目的用於簡化 不可變 物件的構造 比如 goog...

photoshop模式詳解

一 正常 normal 模式 正常 normal 模式是內定的狀態,其最終色和繪圖色相同。可改變畫筆工具選項欄中的 不透明度 設定的不同透明度。當影象的顏色模式是 位圖 或 索引顏色 時,正常 模式就變成 閥值 threshold 模式。二 溶解 dissolve 模式 溶解 dissolve 模式...