Linux C socket 程式設計常用函式

2021-07-10 03:48:08 字數 1601 閱讀 8037

/*

* linux c socket 程式設計常用函式

*/#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

/* * 在指定ip上設定tcp監聽

*/int starttcplistener(const

char* ip, const

char* port)

/* * 在本機的所有ip位址上啟動監聽

*/int starttcplisteneronallips(const

char* port)

/* * 在無資料的前提下,保持tcp鏈結有效

*/int settcpkeepalive(int sock_fd,int alive,int idle,int interval,int count)

/* * 獲取tcp鏈結

*/int accepttcp(int server_fd)

return client_fd;}/*

* 鏈結tcp伺服器

*/int connecttcpsocket(const

char* ip,const

char* port)

/* * 設定組播

*/int setmulticast(const

char* ip, const

char* port,struct sockaddr_in *multicast_add)

/* * 設定udp

*/int setupdsocket(const

char *this_addr,const

char *this_port,const

char *that_addr, const

char *that_port,struct sockaddr_in *addr_this, struct sockaddr_in *addr_that)

/* * 設定udp接受快取區大小

* 在 cat /proc/sys/net/ipv4/udp_mem 可以檢視 當前系統的udp快取區大小

* 該檔案輸出3個值,分別為最小值,預設值和最大值

* 在 2.6.7 以後的核心版本中,系統預設自動調節接收快取區大小,如果手動設定快取區,則系統不再自動調整

*/int setudprecvbuffer(const

int sock_fd,int buffer_size)

/* * 從udp接收資料

*/int udprecv(const

int sock_fd,char *buf,int length)

/* * 向udp傳送資料

*/int udpsend(int sockfd,struct sockaddr_in *udp_add,const

char*buf,int start,int len)

linux c socket程式設計

這裡主要是 網上有原理 socket 有同步,非同步之分,我這是個同步的,同步又有阻塞和非阻塞之分,這的非阻塞是用select 函式實現的 客戶端 include include include include include include include include include incl...

linux c socket程式設計

ntohl 與 htonl 相反的過程 ntohs 與htons 相反的過程 inet addr char 點分十進位制轉化為整型 inet nota int 整型轉化為點分十進位制 主機位元組序 和 網路位元組序 當主機位元組序埠號位為 0x1234 那麼 網路位元組序就為 0x12 0x34 如...

linux C socket程式設計

這是乙個關於socket的題 要求你自己寫乙個socket程式,然後解析客戶端從瀏覽器位址列提交的資料。要求 在瀏覽器中輸入http localhost 8500 後,你的socket程式向瀏覽器返回乙個網頁,以後就用這個網頁向你寫得這個socket程式提交資料,你的socket程式負責解析這些資料...