sokcet常用程式設計函式

2021-06-25 22:07:41 字數 3453 閱讀 7165

1.設定傳送超時和接收超時。

select模型

一般需要同時處理多個檔案描述符,即多個socket時,考慮select模型。

select的函式格式(我所說的是unix系統下的伯克利socket程式設計,和windows下的有區別,一會兒說明):

int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout);

引數一:最大的檔案描述符加1。

引數二:用於檢查可讀性,

引數三:用於檢查可寫性,

引數四:用於檢查帶外資料,

引數五:乙個指向timeval結構的指標,用於決定select等待i/o的最長時間。如果為空將一直等待。timeval結構的定義:struct timeval

返回值》0:就緒描述字的正數目

-1:出錯

0 :超時

readset writeset exceptset指定我們要讓核心測試讀、寫和異常條件的描述字。如果對某乙個的條件不感興趣,就可以把它設為null。如果三個指標都為null,我們就有了乙個比sleep()函式更為精確的定時器(sleep()以毫秒為最小單位,這個以微秒為單位)。

select使用描述字集,典型地是乙個整數陣列,其中每個整數中的每一位對應乙個描述字。假設使用32位整數,那麼該陣列的第乙個元素對應於描述字0~31,第二個元素對應於描述字32~63,依此類推。所有的實現細節都與應用程式無關,它們隱藏在名為fd_set的資料型別和以下四個巨集中:

void fd_zero (fd_set *fdset); // clear all bits in fdset

void fd_set (int fd,fd_set *fdset); // turn on the bit for fd in fdset

void fd_clr (int fd,fd_set *fdset); // turn off the bit for fd in fdset

intfd_isset(int fd,fd_set *fdset); // is the bit for fd on in fdset

eg:定義乙個fd_set變數,然後開啟描述字1.4.5對應的位:

fd_set fdset;

fd_zero (&fdset); // 如果不初始化,會導致不可預期的後果

fd_set (1,&fdset);

fd_set (4,&fdset);

fd_set (5,&fdset);

int maxfdp1引數指定待測試的描述字個數,它的值是待測試的最大描述字加1。

舉例:

main()

; //select等待3微秒,3微秒輪詢,要非阻塞就置0

char buffer[256]=; //256位元組的接收緩衝區

/* 假定已經建立udp連線,具體過程不寫,簡單,當然tcp也同理,主機ip和port都已經給定,要寫的檔案已經開啟

sock=socket(...);

bind(...);

fd=open(...); */

while⑴

// end if break;

}// end switch

}//end while

}//end main

其它模型

select在socket程式設計中還是比較重要的,可是對於初學socket的人來說都不太愛用select寫程式,他們只是習慣寫諸如connect、accept、recv或recvfrom這樣的阻塞程式(所謂阻塞方式block,顧名思義,就是程序或是執行緒執行到這些函式時必須等待某個事件的發生,如果事件沒有發生,程序或執行緒就被阻塞,函式不能立即返回)。

可是使用select就可以完成非阻塞(所謂非阻塞方式non-block,就是程序或執行緒執行此函式時不必非要等待事件的發生,一旦執行肯定返回,以返回值的不同來反映函式的執**況,如果事件發生則與阻塞方式相同,若事件沒有發生則返回乙個**來告知事件未發生,而程序或執行緒繼續執行,所以效率較高)方式工作的程式,它能夠監視我們需要監視的檔案描述符的變化情況——讀寫或是異常。

如果用socket的recvfrom或者sendto,預設是沒有超時機制的,即阻塞模式。如果要設定超時,則通過以下方法:

#include

#include

#include

#include

struct timeval timeout;

timeout.tv_sec = 3;//3秒

timeout.tv_usec = 0;//0微秒 

//設定傳送超時

setsockopt(socket,sol_socket,so_sndtimeo, (char *)&timeout,sizeof(struct timeval));

//設定接收超時

setsockopt(socket,sol_socket,so_rcvtimeo, (char *)&timeout,sizeof(struct timeval))

2.獲取錯誤碼和錯誤原因

#include

printf("err no = %d, err msg = %s\r\n",errno,strerror(errno));

3.mac 位址 字串-》陣列

char mac = "ff-b3-3c-67-a4-05";

short m[6];

sscanf(mac, "%x-%x-%x-%x-%x-%x",(int *)&m[0],(int *)&m[1],(int *)&m[2],(int *)&m[3], (int *)&m[4], (int *)&m[5]);

4.起執行緒

全域性變數:pthread_t debug_thread;

執行緒主函式:

void * debug_thread_func(void* args)

return ((void *)0);

}主程序中

int main()

5.定時器

#include

#include

#include

#include

typedef void (*timerfuncptr)(int);

void init_sig_action ( timerfuncptr funcptr , int x )

void init_time( unsigned int tv_sec, unsigned int tv_usec)

void dhcp_timer_func( int signo)

int main()

{init_sig_action (&dhcp_timer_func,0);

init_time(2,0);

Linux C socket 程式設計常用函式

linux c socket 程式設計常用函式 include include include include include include include include include include include include include include 在指定ip上設定tcp監聽 ...

程式設計常用子函式

1,有序陣列中二分查詢 相等時最左或最右 int binarysearchvalue int a,int n,int value,bool is left return result 2,字串的動態增加並排序 思想 1,擴容 利用動態陣列思想,當字串陣列array str元素滿了時,通過分配拷貝釋放...

openssl常用函式 SSL TLS程式設計

ssl tls 協議已經廣泛應用於電子商務中,用來保證資訊傳輸的安全性。利用 openssl 進行安全套接字程式設計和普通套接字程式設計類似。主要函式 1.初始化 ssl演算法庫函式 int ssl library init void define openssl add ssl algorithm...