linux編程常用頭文件

轉自:socket網絡編程中的頭文件

Socket編程中需要用到的頭文件

stdlib.h : //某些結構體定義和宏定義,如EXIT_FAILURE、EXIT_SUCCESS等

sys/types.h:包含很多類型重定義,如pid_t、int8_t等

sys/socket.h:與套接字相關的函數聲明和結構體定義,如socket()、bind()、connect()及struct sockaddr的定義等

netinet/in.h:定義數據結構,如sockaddr_in,PROTO_ICMP、INADDR_ANY等

arpa/inet.h:提供IP地址轉換函數,如inet_ntop()、inet_ntoa()等

netdb.h:提供設置及獲取域名的結構、宏和函數,如struct hostent、struct servent、gethostbyname()、gethostbyaddr()、herror()等

sys/ioctl.h:提供對I/O控制的函數,如ioctl()

sys/poll.h:提供socket等待測試機制的函數

unistd.h:提供通用的文件、目錄、程序及進程操作的函數

errno.h:提供錯誤號errno的定義,用於錯誤處理

fcntl.h:提供對文件控制的函數

time.h:提供有關時間的函數

crypt.h:提供使用DES加密算法的加密函數

pwd.h:提供對/etc/passwd文件訪問的函數

shadow.h:提供對/etc/shadow文件訪問的函數

pthread.h:提供多線程操作的函數

signal.h:提供對信號操作的函數

sys/wait.h、sys/ipc.h、sys/shm.h:提供進程等待、進程間通訊(IPC)及共享內存的函數

建議:在編寫網絡程序時,可以直接使用下面這段頭文件代碼

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <fcntl.h>

涉及到用戶權限及密碼驗證問題時加入如下語句:

#include <shadow.h>
#include <crypt.h>
#include <pwd.h>

需要注意的是,應該在編譯時鏈接加密算法庫,即增加編譯選項:-lcrypt

涉及到文件及時間操作加入如下語句:

#include <sys/time.h>
#include <utime.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/file.h>

涉及到多進程操作時加入如下語句:

#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>

涉及到多線程操作時加入如下語句:

#include <pthread.h>
#include <sys/poll.h>

需要注意的是,應該在編譯時鏈接線程庫,即增加編譯選項:-pthread

發佈了79 篇原創文章 · 獲贊 127 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章