linux下socket编程常用头文件

原文:http://blog.chinaunix.net/uid-26073752-id-2014655.html(有修改)

sys/types.h:数据类型定义(包含很多类型重定义,如pid_tint8_t)

sys/socket.h:提供socket函数及数据结构,如socket()bind()connect()struct sockaddr的定义等

netinet/in.h:定义数据结构sockaddr_in,某些结构体声明、宏定义,如struct sockaddr_inPROTO_ICMPINADDR_ANY

arpa/inet.h:提供IP地址转换函数,某些函数声明,如inet_ntop()inet_ntoa()

netdb.h:提供设置及获取域名的函数,某些结构体定义、宏定义和函数声明,如struct hostentstruct serventgethostbyname()gethostbyaddr()herror()

sys/ioctl.h:提供对I/O控制的函数,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 <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>


涉及到用户权限及密码验证问题时加入如下语句:
#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>


需要注意的是,应该在编译时链接线程库,即增加编译选项:
-lthread

原文地址:https://www.cnblogs.com/theCambrian/p/3317085.html