📄 00000004.htm
字号:
the order "1, 2" at the opposite end. They will also be error free. Any <BR>errors you do encounter are figments of your own deranged mind, and are not <BR>to be discussed here. <BR>What uses stream sockets? Well, you may have heard of the telnet <BR>application, yes? It uses stream sockets. All the characters you type need <BR>to arrive in the same order you type them, right? Also, WWW browsers use the <BR> <BR>HTTP protocol which uses stream sockets to get pages. Indeed, if you telnet <BR>to a WWW site on port 80, and type "GET pagename", it'll dump the HTML back <BR>at you! <BR>How do stream sockets achieve this high level of data transmission quality? <BR>They use a protocol called "The Transmission Control Protocol", otherwise <BR>known as "TCP" (see RFC-793 for extremely detailed info on TCP.) TCP makes <BR>sure your data arrives sequentially and error-free. You may have heard "TCP" <BR> <BR>before as the better half of "TCP/IP" where "IP" stands for "Internet <BR>Protocol" (see RFC-791.) IP deals with Internet routing only. <BR>Cool. What about Datagram sockets? Why are they called connectionless? What <BR>is the deal, here, anyway? Why are they unreliable? Well, here are some <BR>facts: if you send a datagram, it may arrive. It may arrive out of order. If <BR> <BR>it arrives, the data within the packet will be error-free. <BR>Datagram sockets also use IP for routing, but they don't use TCP; they use <BR>the "User Datagram Protocol", or "UDP" (see RFC-768.) <BR>Why are they connectionless? Well, basically, it's because you don't have to <BR> <BR>maintain an open connection as you do with stream sockets. You just build a <BR>packet, slap an IP header on it with destination information, and send it <BR>out. No connection needed. They are generally used for packet-by-packet <BR>transfers of information. Sample applications: tftp, bootp, etc. <BR>"Enough!" you may scream. "How do these programs even work if datagrams <BR>might get lost?!" Well, my human friend, each has it's own protocol on top <BR>of UDP. For example, the tftp protocol says that for each packet that gets <BR>sent, the recipient has to send back a packet that says, "I got it!" (an <BR>"ACK" packet.) If the sender of the original packet gets no reply in, say, <BR>five seconds, he'll re-transmit the packet until he finally gets an ACK. <BR>This acknowledgment procedure is very important when implementing SOCK_DGRAM <BR> <BR>applications. <BR>---------------------------------------------------------------------------- <BR> <BR>3. Low level Nonsense and Network Theory <BR>Since I just mentioned layering of protocols, it's time to talk about how <BR>networks really work, and to show some examples of how SOCK_DGRAM packets <BR>are built. Practically, you can probably skip this section. It's good <BR>background, however. <BR> [Encapsulated Protocols Image] Hey, kids, it's time to learn about Data <BR> Encapsulation! This is very very important. <BR> <BR>It's so important that you might just learn about it if you take the <BR>networks course here at Chico State ;-). Basically, it says this: a packet <BR>is born, the packet is wrapped ("encapsulated") in a header (and maybe <BR>footer) by the first protocol (say, the TFTP protocol), then the whole thing <BR> <BR>(TFTP header included) is encapsulated again by the next protocol (say, <BR>UDP), then again by the next (IP), then again by the final protocol on the <BR>hardware (physical) layer (say, Ethernet). <BR>When another computer receives the packet, the hardware strips the Ethernet <BR>header, the kernel strips the IP and UDP headers, the TFTP program strips <BR>the TFTP header, and it finally has the data. <BR>Now I can finally talk about the infamous Layered Network Model. This <BR>Network Model describes a system of network functionality that has many <BR>advantages over other models. For instance, you can write sockets programs <BR>that are exactly the same without caring how the data is physically <BR>transmitted (serial, thin Ethernet, AUI, whatever) because programs on lower <BR> <BR>levels deal with it for you. The actual network hardware and topology is <BR>transparent to the socket programmer. <BR>Without any further ado, I'll present the layers of the full-blown model. <BR>Remember this for network class exams: <BR> * Application <BR> * Presentation <BR> * Session <BR> * Transport <BR> * Network <BR> * Data Link <BR> * Physical <BR>The Physical Layer is the hardware (serial, Ethernet, etc.). The Application <BR> <BR>Layer is just about as far from the physical layer as you can imagine--it's <BR>the place where users interact with the network. <BR>Now, this model is so general you could probably use it as an automobile <BR>repair guide if you really wanted to. A layered model more consistent with <BR>Unix might be: <BR> * Application Layer (telnet, ftp, etc.) <BR> * Host-to-Host Transport Layer (TCP, UDP) <BR> * Internet Layer (IP and routing) <BR> * Network Access Layer (was Network, Data Link, and Physical) <BR>At this point in time, you can probably see how these layers correspond to <BR>the encapsulation of the original data. <BR>See how much work there is in building a simple packet? Jeez! And you have <BR>to type in the packet headers yourself using "cat"! Just kidding. All you <BR>have to do for stream sockets is send() the data out. All you have to do for <BR> <BR>datagram sockets is encapsulate the packet in the method of your choosing <BR>and sendto() it out. The kernel builds the Transport Layer and Internet <BR>Layer on for you and the hardware does the Network Access Layer. Ah, modern <BR>technology. <BR>So ends our brief foray into network theory. Oh yes, I forgot to tell you <BR>everything I wanted to say about routing: nothing! That's right, I'm not <BR>going to talk about it at all. The router strips the packet to the IP <BR>header, consults its routing table, blah blah blah. Check out the IP RFC if <BR>you really really care. If you never learn about it, well, you'll live. <BR>---------------------------------------------------------------------------- <BR> <BR>4. structs <BR>Well, we're finally here. It's time to talk about programming. In this <BR>section, I'll cover various data types used by the sockets interface, since <BR>some of them are a real bitch to figure out. <BR>First the easy one: a socket descriptor. A socket descriptor is the <BR>following type: <BR> int <BR>Just a regular int. <BR>Things get weird from here, so just read through and bear with me. Know <BR>this: there are two byte orderings: most significant byte (sometimes called <BR>an "octet") first, or least significant byte first. The former is called <BR>"Network Byte Order". Some machines store their numbers internally in <BR>Network Byte Order, some don't. When I say something has to be in NBO, you <BR>have to call a function (such as htons()) to change it from "Host Byte <BR>Order". If I don't say "NBO", then you must leave the value in Host Byte <BR>Order. <BR>My First Struct(TM)--struct sockaddr. This structure holds socket address <BR>information for many types of sockets: <BR> struct sockaddr <BR> unsigned short sa_family; /* address family, AF_xxx */ <BR> char sa_data[14]; /* 14 bytes of protocol address */ <BR> ; <BR>sa_family can be a variety of things, but it'll be "AF_INET" for everything <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -