一万小时

远没到拼智商的时候

2014-01-22 APUE读书笔记3

Chapter 16 Sockets

Socket用于不同进程间的通信,此处的进程可位于同一机器,或者不同机器。

Socket也有类似与file的descriptor, 即socket descriptor,与FD类似, 支持read/write等操作。(在unix下SD是由FD实现的)

int socket(int domain, int type, int protocol)

domain可以是IPv4,IPv6等,type包括SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET, SOCK_STREAM, 分别用于不同的数据通信(例如是否是reliable等),protocol则是协议,例如IPPROTO_IP,IPPROTO_IPV6, IPPROTO_ICMP, IPPROTO_RAW, IPPROTO_TCP, IPPROTO_UDP. protocol通常可以设置为0,即默认,则 protocol会根据type自动选择(默认),例如SOCK_STREAM对应于IPPROTO_TCP, SOCK_DGRAM对应于IPPROTO_UDP.

下面作者将DGRAM通信(UDP)比作写信,而STREAM(TCP)比作电话,还是比较生动的。

With a datagram (SOCK_DGRAM) interface, no logical connection needs to exist between peers for them to communicate. All you need to do is send a message addressed to the socket being used by the peer process.
A datagram, therefore, provides a connectionless service. A byte stream (SOCK_STREAM), in contrast, requires that, before you can exchange data, you set up a logical connection between your socket and the socket belonging to the peer with which you wish to communicate.
A datagram is a self-contained message. Sending a datagram is analogous to mailing someone a letter. You can mail many letters, but you can’t guarantee the order of delivery, and some might get lost along the way. Each letter contains the address of the recipient, making the letter independent from all the others. Each letter can even go to different recipients.
In contrast, using a connection-oriented protocol for communicating with a peer is like making a phone call. First, you need to establish a connection by placing a phone call, but after the connection is in place, you can communicate bidirectionally with each other. The connection is a peer-to-peer communication channel over which you talk. Your words contain no addressing information, as a point-to-point virtual connection exists between both ends of the call, and the connection itself implies a particular source and destination.

Byte ordering, 基于CPU架构的不同,对于一个较大数据类型,通常有2中不同的解释,即big endian(n:n+1:n+2:n+3), little endian(n+3:n+2:n+1:n). 于是形成了不同的byte ordering,例如对于一个32bit的整形:0x04030201, 此时MSB(Most Significant Byte,总是最左边的byte)=04, LSB=01, 但是在big endian系统下(使用cp指针),cp[0]=04, cp[3]=01, 在little endian下cp[0]=01, cp[3]=04

而网络协议也会制定自己的byte ordering,例如TCP/IP是big endian,而常见的intel CPU是lttle endian,则二者需要转换,也就是network byte order和 processor byte order之间的转换。常用的函数为hton (h means host, n means network), 具体如htonl, htons(long, short)

创建完socket descriptor(通过socket函数),即可进行bind/connect等操作,来建立连接。 server侧可以通过listen/accept等来监控和接受连接。

对于connection less的连接(UDP)通常不需要首先connect,而在每次具体的write/read(send/recv)中指定地址,但也可以connect,然后后续的操作 则默认使用connect时的地址。其它用于数据交换的函数包括send/sendto/recv/recvfrom.

TCP支持OOB(out of band)类型的数据,此类数据有更高的优先级,会先于已经queue的数据发送,”urgent” data. UDP不支持。可以通过在send中指定MSG_OOB来设定 此种类型数据。

Chap.17 Advanced IPC

Unix Domain Sockets, 通常用于同一机器不同进程之间的通信,与Internet Domain Sockets不同, Unix Domain Sockets有更高的效率(没有协议,header解析,checksum等),并且stream/datagram 都是可靠的。int socketpair(int domain, int type, int protocol, int sockfd[2]);

Named Unix Domain Sockets, 可以在filesystem中创建一个知名的socket file,作为server/client通信 的媒介,有3个函数可以使用:int serv_listen(const char *name); int serv_accept(int listenfd, uid_t *uidptr); int cli_conn(const char *name);

在不同进程之间传递FD,是一个强大的机制。APUE中有2个版本的open server实现,其中V2实现了,一个server监听处理 多个client的连接和请求。

Chap. 18 Terminal I/O

终端设备的IO,isatty可以判断是否是terminal device, 有2中读写模式,即canonical(line at a time)和noncanonical.

Chap. 19 Pseudo Terminal

Chap. 20 A Database Library

Top

Press q to hide the help

Key Action Key Action
Small Scroll j Scroll Down k Scroll Up
Big Scroll b Scroll to Bottom t Scroll to Top
Post Navigation n Next Post(if exists) p Previous Post(if exists)
Page Navigation h Go to Blog's Home Page a Go to Blog's Archive Page
Page Navigation c Go to Blog's Category Page ? Show this help
Misc s Go to Search Box q hide the help