基本的套接字编程
socket函数
想要执行网络I/O,首先需要调用socket函数创建套接字,需要头文件#include <sys/socket.h>
int socket (int domain, int type, int protocol);
参数:
domain
: 执行协议域,取值如下:
domain | 说明 |
---|---|
AF_INET | IPV4协议 |
AF_INET6 | IPV6协议 |
AF_LOCAL | Unix 域协议 |
AF_ROUTE | 路由套接字 |
AF_KEY | 密钥套接字 |
type
: 套接字类型,取值如下
type | 说明 |
---|---|
SOCK_STREAM | 字节流套接字 |
SOCK_DGRAM | 数据报套接字 |
SOCK_SEQPACKET | 有序分组套接字 |
SOCK_RAW | 原始套接字 |
protocol
: 某个协议类型常值,或者设置为0。
protocal | 说明 |
---|---|
IPPROTO_TCP | TCP传输协议 |
IPP |