什么意思?有两种 Internet 套接口?是的。不,我在撒谎。其实还有很多,但是我可不想 吓着你。我们这里只讲两种。 Except for this sentence, where I'm going to tell you that "Raw Sockets" are also very powerful and you should look them up.
为什么流式套接口可以达到高质量的数据传输?他使用了“传输控制协议 (The Transmission Control Protocol)”,也叫 “TCP” (请参考 RFC-793 获得详细资料。)TCP 控制你的数据 按顺序到达并且没有错误。你也许听到 “TCP” 是因为听到过 “TCP/IP”。这里的 IP 是指 “Internet 协议”(请参考 RFC-791.) IP 只是处理 Internet 路由而已。
朋友们,现在是学习 数据封装 (Data Encapsulation) 的时候了! 这非常非常重要。It's so important that you might just learn about it if you take the networks course here at Chico State ;-). 主要的内容是:一个包,先是被第一个协议(在这里是 TFTP )包装(“封装”), 然后,整个数据(包括 TFTP 头)被另外一个协议(在这里是 UDP )封装,然后下 一个( IP ),一直重复下去,直到硬件(物理)层( Ethernet )。
当另外一台机器接收到包,硬件先剥去 Ethernet 头,内核剥去 IP 和 UDP 头,TFTP 程序再剥去 TFTP 头,最后得到数据。
struct sockaddr_in { short int sin_family; /* Address family */ unsigned short int sin_port; /* Port number */ struct in_addr sin_addr; /* Internet address */ unsigned char sin_zero[8]; /* Same size as struct sockaddr */
};
这个数据结构让可以轻松处理套接口地址的基本元素。注意 sin_zero (他 被加入到这个结构,并且长度和 struct sockaddr 一样) 应该使用函数 bzero() 或 memset() 来全部置零。 Also, and this is the important bit, a pointer to a struct sockaddr_in can be cast to a pointer to a struct sockaddr and vice-versa. 这样的话 即使 socket() 想要的是 struct sockaddr *, 你仍然可以使用 struct sockaddr_in,and cast it at the last minute! 同时,注意 sin_family 和 struct sockaddr 中的 sa_family 一致并能够设置为 "AF_INET"。最后, sin_port 和 sin_addr 必须是网络字节顺序 (Network Byte Order)!
Ok, time for a change of pace. What if you don't want to connect to a remote host. Say, just for kicks, that you want to wait for incoming connections and handle them in some way. 处理过程分两步:首先,你听--listen(),然后,你接受--accept() (请看 下面的内容)。
sockfd 相当简单,是和 listen() 中一样的套接口描述符。addr 是个指向局部的数据结构 struct sockaddr_in 的指针。This is where the information about the incoming connection will go (and you can determine which host is calling you from which port). 在他的地址传递给 accept 之前,addrlen 是个局部的整形变量,设置为 sizeof(struct sockaddr_in)。accept 将 不会将多余的字节给 addr。如果你放入的少些,那么在 addrlen 的值中反映 出来。
同样,在错误时返回-1并设置全局变量 errno。
现在是你应该熟悉的代码片段。
#include
#include
#include
#define MYPORT 3490 /* the port users will be connecting to */
#define BACKLOG 10 /* how many pending connections queue will hold */
main()
{
int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
int sin_size;
sockfd = socket(AF_INET, SOCK_STREAM, 0); /* do some error checking! */
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
/* don't forget your error checking for these calls: */
Except for one more tiny detail that I've mentioned many times in the past: connected datagram sockets. I need to talk about this here, since we're in the datagram section of the document. Let's say that talker calls connect() and specifies the listener's address. From that point on, talker may only sent to and receive from the address specified by connect(). For this reason, you don't have to use sendto() and recvfrom(); you can simply use send() and recv().