socket編程中需要用到的頭文件
sys/types.h:數(shù)據(jù)類型定義
sys/socket.h:提供socket函數(shù)及數(shù)據(jù)結(jié)構(gòu)
netinet/in.h:定義數(shù)據(jù)結(jié)構(gòu)sockaddr_in
arpa/inet.h:提供IP地址轉(zhuǎn)換函數(shù)
netdb.h:提供設(shè)置及獲取域名的函數(shù)
sys/ioctl.h:提供對I/O控制的函數(shù)
sys/poll.h:提供socket等待測試機(jī)制的函數(shù)
?
其他在網(wǎng)絡(luò)程序中常見的頭文件
unistd.h:提供通用的文件、目錄、程序及進(jìn)程操作的函數(shù)
errno.h:提供錯誤號errno的定義,用于錯誤處理
fcntl.h:提供對文件控制的函數(shù)
time.h:提供有關(guān)時間的函數(shù)
crypt.h:提供使用DES加密算法的加密函數(shù)
pwd.h:提供對/etc/passwd文件訪問的函數(shù)
shadow.h:提供對/etc/shadow文件訪問的函數(shù)
pthread.h:提供多線程操作的函數(shù)
signal.h:提供對信號操作的函數(shù)
sys/wait.h、sys/ipc.h、sys/shm.h:提供進(jìn)程等待、進(jìn)程間通訊(IPC)及共享內(nèi)存的函數(shù)
?
建議: 在編寫網(wǎng)絡(luò)程序時,可以直接使用下面這段頭文件代碼
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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>
?
涉及到用戶權(quán)限及密碼驗證問題時加入如下語句:
#include <shadow.h>
#include <crypt.h>
#include <pwd.h>
需要注意的是,應(yīng)該在編譯時鏈接加密算法庫,即增加編譯選項:
-lcrypt
?
涉及到文件及時間操作加入如下語句:
#include <sys/time.h>
#include <utime.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/file.h>
?
涉及到多進(jìn)程操作時加入如下語句:
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
?
涉及到多線程操作時加入如下語句:
#include <pthread.h>
#include <sys/poll.h>
需要注意的是,應(yīng)該在編譯時鏈接線程庫,即增加編譯選項:
-lthread