在internet网络的世界里,socket可以说是最重要的任务间通讯的方式,尤其是当两个任务驻留在不同的机器上需要通过网络介质连接。今天系统复习一下socket编程,因为本人已经有了基本的网络和操作系统的知识,直接跳过很基本的背景知识介绍了。我理解的socket就是抽象封装了传输层以下软硬件行为,为上层应用程序提供进程/线程间通信管道。就是让应用开发人员不用管信息传输的过程,直接用socket API就OK了。贴个TCP的socket示意图体会以下。
Socket通信过程和API全解析
udp和TCP socket通信过程基本上是一样的,只是调用api时传入的配置不一样,以TCP client/server模型为例子看一下整个过程。
socket API
socket: establish socket interface
gethostname: obtain hostname of system
gethostbyname: returns a structure of type hostent for the given host name
bind: bind a name to a socket
listen: listen for connections on a socket
accept: accept a connection on a socket
connect: initiate a connection on a socket
setsockopt: set a particular socket option for the specified socket.
close: close a file descriptor
shutdown: shut down part of a full-duplex connection
1. socket()
#include /* See NOTES */
#include
int socket(int domain, int type, int protocol);
- 参数说明
domain: 设定socket双方通信协议域,是本地/internet ip4 or ip6
Name Purpose Man page
AF_UNIX, AF_LOCAL Local communication unix(7)
AF_INET IPv4 Internet protocols ip(7)
AF_INET6 IPv6 Internet protocols ipv6(7)
type: 设定socket的类型,常用的有
SOCK_STREAM - 一般对应TCP、sctp
SOCK_DGRAM - 一般对应UDP
SOCK_RAW -
protocol: 设定通信使用的传输层协议
常用的协议有IPPROTO_TCP、IPPTOTO_UDP、IPPROTO_SCTP、IPPROTO_TIPC等,可以设置为0,系统自己选定。注意protocol和type不是随意组合的。
socket() API是在glibc中实现的,该函数又调用到了kernel的sys_socket(),调用链如下。
详细的kernel实现我没有去读,大体上这样理解。调用socket()会在内核空间中分配内存然后保存相关的配置。同时会把这块kernel的内存与文件系统关联,以后便可以通过filehandle来访问修改这块配置或者read/write socket。操作socket就像操作file一样,应了那句unix一切皆file。提示系统的最大filehandle数是有限制的,/proc/sys