Linux中網路編程的常用函數(1)

來源:互聯網
上載者:User

1、fcntl

標頭檔 #include <fcntl.h>

#include <fcntl.h>

定義函數 int fcntl(int fd , int cmd);

int fcntl(int fd,int cmd,long arg);

int fcntl(int fd,int cmd,struct flock * lock);

fcntl()用來操作檔案描述符的一些特性。參數fd代表欲設定的檔案描述詞,參數cmd代表欲操作的指令。

其詳細情況見參考中。

【1】 http://linux.die.net/man/2/fcntl

【2】 http://baike.baidu.com/view/1930855.htm

【3】 http://blog.mcuol.com/User/lvembededsys/Article/2388_1.htm

【4】 http://blog.chinaunix.net/space.php?uid=21461208&do=blog&cuid=1915256

2、setsockopt

    setsockopt()函數用於任意類型、任意狀態套介面的設定選項值。儘管在不同協議層上存在選項,但本函數僅定義了最高的“套介面”層次上的選項。

【1】 http://linux.die.net/man/2/setsockopt

【2】 http://baike.baidu.com/view/569217.htm

3、inet_aton

   inet_aton() converts the Internet host address cp from the standard numbers-and-dots notation into binary data and stores it in the structure that inp points to. inet_aton() returns non-zero if the address is valid, zero if not.

The inet_addr() function也是相同功能。

【1】 http://linux.die.net/man/3/inet_aton

【2】 http://baike.baidu.com/view/1870333.htm

【3】http://linux.die.net/man/3/inet_addr

4、對於sockaddr_in結構體的定義,則不同標頭檔定義又不同。

struct sockaddr_in {

    sa_family_t    sin_family; /* address family: AF_INET */

    u_int16_t      sin_port;   /* port in network byte order */

    struct in_addr sin_addr;   /* internet address */

};

 

/* Internet address. */

struct in_addr {

    u_int32_t      s_addr;     /* address in network byte order */

};

【1】 http://linux.die.net/man/7/ip

【2】 http://baike.baidu.com/view/2355183.htm

5、htonl()

    將主機的無符號長整形數轉換成網路位元組順序。

【1】 http://baike.baidu.com/view/569196.html

【2】 http://linux.die.net/man/3/htonl

6、gettimeofday

    The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.

【1】 http://linux.die.net/man/2/gettimeofday

【2】 http://blog.sina.com.cn/s/blog_4d61a7570100eci0.html

【3】 http://zhidao.baidu.com/question/75454941

【4】http://hi.baidu.com/%B1%AB%B9%FA%CC%CE/blog/item/778bcc23d850f7ae4623e8a0.html

7、semctl

semctl() performs the control operation specified by cmd on the semaphore set identified by semid, or on the semnum-th semaphore of that set. (The semaphores in a set are numbered starting at 0.)

系統調用semctl用來執行在訊號量集上的控制操作。

【1】 http://linux.die.net/man/2/semctl

【2】 http://baike.baidu.com/view/3803900.htm

【3】 http://hi.baidu.com/rhose/blog/item/83e74dd74dcf0d1a3bf3cf5b.html

8、waitpid

    通過waitpid調用和wait調用來收集子進程的資訊。

【1】 http://www.cnblogs.com/mydomain/archive/2010/10/24/1859826.html

【2】 http://baike.baidu.com/view/2365304.htm

【3】 http://linux.die.net/man/2/waitpid

9、getopt

getopt()用來分析命令列參數。

int getopt(int argc, char * const argv[], const char *optstring);

調用一次,返回一個選項。在命令列選項參數再也檢查不到optstring中包含的選項時,返回-1,同時optind儲存第一個不包含在optstring中的選項的命令列參數。

字串optstring可以下列元素,

(1)單個字元,表示選項,

(2)單個字元後接一個冒號:表示該選項後必須跟一個參數。參數緊跟在選項後或者以空格隔開。該參數的指標賦給optarg。

(3)單個字元後跟兩個冒號,表示該選項後必須跟一個參數。參數必須緊跟在選項後不能以空格隔開。該參數的指標賦給optarg。(這個特性是GNU的擴充)。

getopt處理以'-’開頭的命令列參數,如optstring="ab:c::d::",命令列為getopt.exe -a -b host -ckeke -d haha

在這個命令列參數中,-a和-b就是選項元素,去掉'-',a,b,c就是選項。host是b的參數,keke是c的參數。但haha並不是d的參數,因為它們中間有空格隔開。

預設情況下getopt會重新排列命令列參數的順序,所以到最後所有不包含在optstring中的選項的命令列參數都排到最後。

如getopt.exe -a ima -b host -ckeke -d haha,包含在optstring中命令列參數是:

-a -b host -ckeke -d

不包含在optstring中的參數為ima及haha,所以重排後,如下所示:

最後命令列參數的順序是: -a -b host -ckeke -d ima haha

如果optstring中的字串以'+'加號開頭或者環境變數POSIXLY_CORRE被設定。那麼一遇到不包含選項的命令列參數,getopt就會停止,返回-1。

【1】 http://baike.baidu.com/view/2406693.htm

【2】 http://linux.die.net/man/3/getopt

【3】 http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspx

【4】 http://blogold.chinaunix.net/u1/50826/showart_472964.html

【5】 http://www.ibm.com/developerworks/cn/aix/library/au-unix-getopt.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.