sendto函式不堵塞
udp不用等待確認,沒有實際的傳送緩衝區,所以udp協議中不存在傳送緩衝區滿的情況,在udp套接字上執行的寫操作永遠都不會阻塞。
以read函式為例:當開始建立乙個套接字描述符的時候,系統核心將其設定為阻塞io模式。程序呼叫read函式從套接字上讀取資料,當套接字的接收緩衝區中還沒有資料可讀,函式read將發生阻塞。它會一直阻塞下去,等待套接字的接收緩衝區中有資料可讀。經過一段時間後,緩衝區內接收到資料,於是核心便去喚醒該程序,通過read訪問這些資料。如果在程序阻塞過程中,對方發生故障,那這個程序將永遠阻塞下去。
可以使用函式fcntl()設定乙個套接字的標誌為o_nonblock 來實現非阻塞。
**實現;
1.fcntl( )函式
int fcntl(int fd, int cmd, long arg);
int flag;
flag = fcntl(sockfd, f_getfl, 0);
flag |= o_nonblock;
fcntl(sockfd, f_setfl, flag);
2.ioctl() 函式
int b_on =1;
ioctl(sock_fd, fionbio, &b_on);
#include #include #include int select(int n, fd_set *read_fds, fd_set *write_fds,
fd_set *except_fds, struct timeval *timeout);
// poll
#include int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
maxfd
所有監控的檔案描述符中最大的那乙個加1
read_fds
所有要讀的檔案檔案描述符的集合
write_fds
所有要的寫檔案檔案描述符的集合
except_fds
其他要向我們通知的檔案描述符
timeout
超時設定.select巨集null:一直阻塞,直到有檔案描述符就緒或出錯
時間值為0:僅僅檢測檔案描述符集的狀態,然後立即返回
時間值不為0:在指定時間內,如果沒有事件發生,則超時返回
void fd_zero(fd_set *fdset)
void fd_set(int fd,fd_set *fdset)
void fd_clr(int fd,fd_set *fdset)
int fd_isset(int fd,fd_set *fdset)
網路程式設計函式
include uint16 t htons uint16 t host16bitvalue uint32 t htonl uint32 t host32bitvalue 均返回 網路位元組序值 uint16 t ntohs uint16 t net16bitvalue uint32 t nohl ...
網路程式設計 connect函式
1 connect描述 定義函式 int connect int sockfd,struct sockaddr serv addr,int addrlen connect函式通常用於客戶端建立tcp連線。返回值 成功則返回0,失敗返回 1,錯誤原因存於errno中。錯誤 ebadf 引數sockfd...
網路程式設計 recv 函式
recv 是程式語言函式。函式原型int recv in sockets,out char buf,in int len,in int flags 這裡只描述同步socket的recv函式的執行流程。當應用程式呼叫recv函式時 1 recv先等待套接字s的傳送緩衝中的資料被協議傳送完畢,如果協議在...