2023年全國(guó)碩士研究生考試考研英語(yǔ)一試題真題(含答案詳解+作文范文)_第1頁(yè)
已閱讀1頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、<p>  Beej's Guide to Network Programming</p><p><b>  作者:Beej</b></p><p>  source:http://library.n0i.net/programming/miscellaneous/beej-network/theory.html</p><p&

2、gt;  1. What is a socket?</p><p>  You hear talk of "sockets" all the time, and perhaps you are wondering just what they are exactly. Well, they’re this: a way to speak to other programs using stan

3、dard Unix file descriptors.What?</p><p>  Ok–you may have heard some Unix hacker state, "Jeez, everything in Unix is a file!" What that person may have been talking about is the fact that when Unix

4、 programs do any sort of I/O, they do it by reading or writing to a file descriptor. A file descriptor is simply an integer associated with an open file. But (and here’s the catch), that file can be a network connection,

5、 a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else. Everything in Unix is a file! So when you </p><p>  "Where do I get this file descriptor for network communication, Mr.

6、 Smarty-Pants?" is probably the last question on your mind right now, but I’m going to answer it anyway: You make a call to the socket () system routine. It returns the socket descriptor, and you communicate through

7、 it using the specialized send () and recv () (man send4, man</p><p>  recv5) socket calls.</p><p>  "But, hey!" you might be exclaiming right about now. "If it’s a file descripto

8、r, why in the name of Neptune can’t I just use the normal read () and write () calls to communicate through the socket?" The short answer is, "You can!" The longer answer is, "You can, but send () and

9、 recv () offer much greater control over your data transmission."</p><p>  What next? How about this: there are all kinds of sockets. There are DARPA Internet addresses (Internet Sockets),path names on

10、a local node (Unix Sockets), CCITT X.25 addresses (X.25 Sockets that you can safely ignore), and probably many others depending on which Unix flavor you run. This document deals only with the first: Internet Sockets.<

11、/p><p>  2.1. Two Types of Internet Sockets</p><p>  What’s this? There are two types of Internet sockets? Yes. Well, no. I’m lying. There are more, but I didn’t want to scare you. I’m only going t

12、o talk about two types here. Except for this sentence, where I’m going to tell you that "Raw Sockets" are also very powerful and you should look them up.</p><p>  All right, already. What are the t

13、wo types? One is "Stream Sockets"; the other is "Datagram Sockets", which may hereafter be referred to as "SOCK_STREAM" and "SOCK_DGRAM", respectively. Datagram sockets are sometim

14、es called "connectionless sockets". (Though they can be connect ()’d if you really want.) Stream sockets are reliable two-way connected communication streams. If you output two items into the socket in</p>

15、;<p>  the order "1, 2", they will arrive in the order "1, 2" at the opposite end. They will also be error free. Any errors you do encounter are figments of your own deranged mind, and are not

16、to be discussed here.</p><p>  What uses stream sockets? Well, you may have heard of the telnet application, yes? It uses stream sockets. All the characters you type need to arrive in the same order you type

17、 them, right? Also, web browsers use the HTTP protocol which uses stream sockets to get pages. Indeed, if you telnet to a web site on port 80, and type "GET /", it’ll dump</p><p>  the HTML back at

18、 you! How do stream sockets achieve this high level of data transmission quality? They use a protocol called "The Transmission Control Protocol", otherwise known as "TCP" .TCP makes sure your data arr

19、ives sequentially and error-free. You may have heard "TCP" before as the better half of "TCP/IP" where "IP" stands for "Internet Protocol" IP deals primarily with Internet routing

20、 and is not generally responsible for data integrity.</p><p>  Cool. What about Datagram sockets? Why are they called connectionless? What is the deal, here, anyway? Why are</p><p>  they unreli

21、able? Well, here are some facts: if you send a datagram, it may arrive. It may arrive out of order. If it</p><p>  arrives, the data within the packet will be error-free.</p><p>  Datagram socke

22、ts also use IP for routing, but they don’t use TCP; they use the "User Datagram Protocol", or "UDP" (see RFC-7688.)</p><p>  Why are they connectionless? Well, basically, it’s because you

23、 don’t have to maintain an open connection as you do with stream sockets. You just build a packet, slap an IP header on it with destination information, and send it out. No connection needed. They are generally used for

24、packet-by-packet transfers of information. Sample applications: tftp,</p><p>  bootp, etc.</p><p>  "Enough!" you may scream. "How do these programs even work if datagrams might g

25、et lost?!" Well, my human friend, each has it’s own protocol on top of UDP. For example, the tftp protocol says that for each packet that gets sent, the recipient has to send back a packet that says, "I got it!

26、" (an "ACK" packet.) If the sender of the original packet gets no reply in, say, five seconds, he’ll re-transmit the packet until he finally gets an ACK. This acknowledgment procedure is very important whe

27、n impl</p><p>  2.2. Low level Nonsense and Network Theory</p><p>  Since I just mentioned layering of protocols, it’s time to talk about how networks really work, and to show some</p>&l

28、t;p>  examples of how SOCK_DGRAM packets are built. Practically, you can probably skip this section. It’s good background, however. Hey, kids, it’s time to learn about Data Encapsulation! This is very very important.

29、Basically, it says this: a packet is born, the packet is wrapped ("encapsulated") in a header (and rarely footer) by the first protocol (say, the TFTP protocol), then the whole thing (TFTP header included) is e

30、ncapsulated again by the next protocol (say, UDP), then again by</p><p>  the next (IP), then again by the final protocol on the hardware (physical) layer (say, Ethernet).</p><p>  When another

31、computer receives the packet, the hardware strips the Ethernet header, the kernel strips the IP and UDP headers, the TFTP program strips the TFTP header, and it finally has the data.</p><p>  Now I can final

32、ly talk about the infamous Layered Network Model. This Network Model describes a system of network functionality that has many advantages over other models. For instance, you can write sockets programs that are exactly t

33、he same without caring how the data is physically transmitted ,because programs on lower levels deal with it for you. The actual network hardware and topology is transparent to the socket programmer. Without any further

34、ado, I’ll present the layers of the full-blow</p><p>  ? Application</p><p>  ? Presentation</p><p><b>  ? Session</b></p><p>  ? Transport</p><p

35、><b>  ? Network</b></p><p>  ? Data Link</p><p>  ? Physical</p><p>  The Physical Layer is the hardware (serial, Ethernet, etc.). The Application Layer is just about

36、 as far from the physical layer as you can imagine–it’s the place where users interact with the network. Now, this model is so general you could probably use it as an automobile repair guide if you really wanted to. A la

37、yered model more consistent with Unix might be:</p><p>  ? Application Layer (telnet, ftp, etc.)</p><p>  ? Host-to-Host Transport Layer (TCP, UDP)</p><p>  ? Internet Layer (IP and

38、 routing)</p><p>  ? Network Access Layer (Ethernet, ATM, or whatever)</p><p>  At this point in time, you can probably see how these layers correspond to the encapsulation of the original data.

39、 See how much work there is in building a simple packet? Jeez! And you have to type in the packet headers yourself using "cat"! Just kidding. All you have to do for stream sockets is send() the data out. All yo

40、u have to do for datagram sockets is encapsulate the packet in the method of your choosing and sendto() it out. The kernel builds the Transport Layer and Internet Layer on for </p><p>  So ends our brief for

41、ay into network theory. Oh yes, I forgot to tell you everything I wanted to say about routing: nothing! That’s right, I’m not going to talk about it at all. </p><p>  3. struts and Data Handling</p>&

42、lt;p>  Well, we’re finally here. It’s time to talk about programming. In this section, I’ll cover various data types used by the</p><p>  sockets interface .First the easy one: a socket descriptor. A sock

43、et descriptor is the following type:int,Just a regular int.</p><p>  Things get weird from here, so just read through and bear with me. Know this: there are two byte orderings: most significant byte first, o

44、r least significant byte first. The former is called "Network Byte Order". Some machines store their numbers internally in Network Byte Order, some don’t. When I say something has to be in Network Byte Order, y

45、ou have to call a function (such as htons()) to change it from "Host Byte Order”. If I don’t say "Network Byte Order", then you must leave the value in </p><p>  3.1. Convert the Natives!</

46、p><p>  We’ve now been lead right into the next section. There’s been too much talk about this Network to Host Byte Order conversion–now is the time for action! All righty. There are two types that you can conv

47、ert: short (two bytes) and long (four bytes). These functions work for the unsigned variations as well. Say you want to convert a short from Host Byte Order to Network Byte Order. Start with "h" for "host&

48、quot;, follow it with "to", then "n" for "network", and "s" for "short": h-to-n-s, or htons() (rea</p><p>  "Host to Network Short").</p>&l

49、t;p>  It’s almost too easy....You can use every combination if "n", "h", "s", and "l" you want. For example, there is NOT a stolh() ("Short to Long Host") function–not

50、 at this party, anyway. But there are:</p><p>  ? htons() – "Host to Network Short"</p><p>  ? htonl() – "Host to Network Long"</p><p>  ? ntohs() – "Networ

51、k to Host Short"</p><p>  ? ntohl() – "Network to Host Long"</p><p>  Now, you may think you’re wising up to this. You might think, "What do I do if I have to change byte ord

52、er on a char?" Then you might think, "Uh, never mind." You might also think that since your 68000 machine already uses network byte order, you don’t have to call htonl() on your IP addresses. You would be

53、right, BUT if you try to port to a machine that has reverse network byte order, your program will fail. Be portable! This is a Unix world! (As much as Bill Gates would like to think otherwise</p><p>  A fina

54、l point: why do sin_addr and sin_port need to be in Network Byte Order in a struct sockaddr_in,but sin_family does not? The answer: sin_addr and sin_port get encapsulated in the packet at the IP and</p><p> 

55、 UDP layers, respectively. Thus, they must be in Network Byte Order. However, the sin_family field is only used by the kernel to determine what type of address the structure contains, so it must be in Host Byte Order. Al

56、so, since sin_family does not get sent out on the network, it can be in Host Byte Order.</p><p>  3.2. IP Addresses and How to Deal With Them</p><p>  Fortunately for you, there are a bunch of f

57、unctions that allow you to manipulate IP addresses. First, let’s say you have a struct sockaddr_in ina, and you have an IP address "10.12.110.57" that you want to store into it. The function you want to use, in

58、et_addr(), converts an IP address in numbers-and-dots notation into an unsigned long. The assignment can be made as follows:</p><p>  ina.sin_addr.s_addr = inet_addr("10.12.110.57");</p><

59、;p>  Notice that inet_addr() returns the address in Network Byte Order already–you don’t have to call htonl().</p><p><b>  Swell!</b></p><p>  Now, the above code snippet isn’t ve

60、ry robust because there is no error checking. See, inet_addr() returns -1 on error. Remember binary numbers? (unsigned)-1 just happens to correspond to the IP address 255.255.255.255! That’s the broadcast address! Wrongo

61、. Remember to do your error checking properly.</p><p>  Actually, there’s a cleaner interface you can use instead of inet_addr(): it’s called inet_aton().</p><p>  Beej 對(duì)網(wǎng)絡(luò)編程的指導(dǎo)</p><p

62、><b> ?。ㄗg文)</b></p><p><b>  1.什么是套接字?</b></p><p>  你經(jīng)常聽說(shuō)“套接字”,但是也許你并不知道它們確切是什么。它們是用標(biāo)準(zhǔn) Unix 文件描繪器向別的程序發(fā)出信號(hào)的途徑。什么?好吧,你也許聽到過(guò)一些Unix 黑客這樣說(shuō):“Jeez,所有Unix里的一切都是一個(gè)文件!”他說(shuō)的也許是這樣一個(gè)

63、事實(shí),當(dāng)Unix程序做任何一種I/O,它們利用了一個(gè)文件描繪器的讀寫功能。文件描繪器就是一個(gè)與打開文件關(guān)聯(lián)的簡(jiǎn)單整數(shù)。但是(這點(diǎn)很重要),那個(gè)文件可能是一個(gè)網(wǎng)絡(luò)連接,一個(gè)FIFO,一個(gè)管道,一個(gè)終端,一份on-the-disk文件,或者是別的。Unix里的一切都是一個(gè)文件!所有當(dāng)要在因特網(wǎng)上聯(lián)絡(luò)另一個(gè)程序時(shí)你得通過(guò)一個(gè)文件描繪器,你最好相信它。</p><p>  “哪里可以得到這個(gè)文件描繪器用于網(wǎng)絡(luò)通信呢,Mr

64、. Smarty-pants?”這可能是現(xiàn)在你腦海中的最后一個(gè)問(wèn)題了,但我將馬上給予回答:你只要?jiǎng)?chuàng)建一個(gè)套接字系統(tǒng)路由,它會(huì)給你送來(lái)一個(gè)套接字描繪器。然后你通過(guò)用專門的套接字命令發(fā)送與接收進(jìn)行連接。</p><p>  “但是,嗨!”現(xiàn)在你也許會(huì)這樣問(wèn),“如果這是一個(gè)文件描繪器,為什么我不能通過(guò)套接字以海王星的名義用一般的讀寫命令進(jìn)行連接?”簡(jiǎn)短的回答是:“你完全可以!”具體的回答是:“你可以,但是發(fā)送與接收命令

65、能對(duì)你的數(shù)據(jù)傳遞控制的更好。”</p><p>  接下來(lái)呢?這個(gè)回答如何:有各種各樣的套接字,如因特網(wǎng)套接字,Unix 套接字,CCITTX.25 地址(你能完全忽略X.25 套接字),還有許多別的取決于Unix使用哪種運(yùn)行。這份文件只關(guān)于第一個(gè):因特網(wǎng)套接字。</p><p>  2.1因特網(wǎng)套接字的兩種類型</p><p>  這是什么?因特網(wǎng)套接字有兩種類型

66、?是的。等等,不是。我在說(shuō)謊。有更多的類型,但是我不想嚇倒你,這兒我只講兩種類型,并會(huì)告訴你們“Raw 套接字”的效力很大,你們必須把它們找出來(lái)。</p><p>  好了,現(xiàn)在開始。這兩種類型是什么?一種是“流式套接字”,另一種是“數(shù)據(jù)報(bào)式套接字”,這在將來(lái)會(huì)被尊敬的稱為“SOCK-STREAM”和“SOCK-DGRAM”。數(shù)據(jù)報(bào)式套接字有時(shí)也被叫做“無(wú)連接套接字”(盡管如果你真的要連接的話,它們也可以)。&l

67、t;/p><p>  流式套接字是可靠的雙連接通信流。如果你輸出兩個(gè)東西在套接字里,順序?yàn)椤?,2”,它們會(huì)以“1,2”的順序到達(dá)另一端。而且它們不會(huì)出錯(cuò)。若是你確實(shí)發(fā)現(xiàn)錯(cuò)誤那也是你自己的腦袋出現(xiàn)的,這里將不做討論。</p><p>  什么會(huì)用到流式套接字呢?也許你聽過(guò)遠(yuǎn)程登錄申請(qǐng)。它就用到流式套接字。你輸入的所有符號(hào)必須以同樣的順序到達(dá),不是嗎?同樣的,網(wǎng)頁(yè)瀏覽器用HTTP協(xié)議鏈接,這也用

68、到流式套接字。事實(shí)是,如果你登陸到端口號(hào)為80的網(wǎng)站,然后輸入“GET/”,它將返回HTML給你。</p><p>  流式套接字是如何高水平的保證數(shù)據(jù)傳輸質(zhì)量呢?它們使用一種協(xié)議叫做“傳輸控制協(xié)議”,或者叫做“TCP”。 TCP保證你的數(shù)據(jù)按順序到達(dá)而且不會(huì)出現(xiàn)錯(cuò)誤。你以前也許聽過(guò)“TCP”作為“TCP/IP”較好的一半。那么“IP”代表著“互聯(lián)網(wǎng)協(xié)議”,初級(jí)處理因特網(wǎng)路線安排。它一般不負(fù)責(zé)數(shù)據(jù)的完整。<

69、/p><p>  酷!那么數(shù)據(jù)報(bào)式套接字呢?為什么它們會(huì)被叫做無(wú)連接套接字?到底是因?yàn)槭裁??為什么它們不可靠?好吧,這里是一些事實(shí):如果你傳送一份數(shù)據(jù)資料,它能到達(dá),但是它的順序可能出現(xiàn)混亂。數(shù)據(jù)報(bào)式套接字也使用IP安排路線,但它們不使用TCP;它們使用UDP。為什么它們無(wú)法連接呢?基本上,這是因?yàn)槟悴槐乇A粢粋€(gè)打開的連接,像流式套接字那樣。你只要建立一個(gè)IP郵件,在上面擊入表頭連著目的地的信息,然后傳送出去,不需要

70、連接,它們一般用于以封包形式存信息,應(yīng)用程序樣本:tftp,bootp,等。</p><p>  “夠了!”你也許會(huì)叫起來(lái)?!斑@些程序怎么工作,如果數(shù)據(jù)資料會(huì)丟失?”當(dāng)然,好心的朋友,每個(gè)都有自己的協(xié)議在UDP頂上。比如說(shuō),tftp協(xié)議對(duì)于每封發(fā)出的郵件,接受者必須回復(fù)一封郵件說(shuō)明“收到了”。(一封“ACK”郵件)如果原郵件的發(fā)送者沒(méi)收到回復(fù),比如說(shuō),5秒鐘后,他會(huì)重新傳遞郵件直到他最終收到一封“ACK”。這個(gè)回

71、復(fù)步驟在運(yùn)行SOCK-DGRSM應(yīng)用程序很重要。</p><p>  2.2低級(jí)言論和網(wǎng)絡(luò)理論</p><p>  既然我剛才提到了協(xié)議,現(xiàn)在是時(shí)候談?wù)劸W(wǎng)絡(luò)如何真正地工作,還有舉例說(shuō)明SOCK DGRAM郵件怎么建立。實(shí)際上,你極有可能略過(guò)這部分,盡管如此,這是很好的背景。</p><p>  嗨,孩子們,學(xué)習(xí)數(shù)據(jù)包裝的時(shí)間到了!這非常重要。基本上是這樣的,一份郵件

72、誕生,被第一項(xiàng)協(xié)議(比如TFTP協(xié)議)包裝在表頭(極少是footer)里,然后整個(gè)(包括TFTP表頭)被第二項(xiàng)協(xié)議重新包裝,然后是下一個(gè)(IP),接著是硬件(物理的)封裝(比如以太網(wǎng))上的最后協(xié)議。</p><p>  當(dāng)另一臺(tái)計(jì)算機(jī)收到郵件,硬件去除以太網(wǎng)表頭,核心處理器除去IP和UDP表頭,TFTP程序去除TFTP表頭,最后剩下數(shù)據(jù)。</p><p>  現(xiàn)在我最后談?wù)勚姆謱泳W(wǎng)絡(luò)模

73、式。</p><p>  這個(gè)網(wǎng)絡(luò)模式描述了一個(gè)優(yōu)于其他模式的網(wǎng)絡(luò)功能系統(tǒng)。</p><p>  舉個(gè)例子說(shuō),你能編寫一模一樣的套接字程序,而不用擔(dān)心數(shù)據(jù)怎么被轉(zhuǎn)化,因?yàn)榈图?jí)程序?yàn)槟闾幚砗昧?。真正的網(wǎng)絡(luò)硬件和高級(jí)系統(tǒng)對(duì)套接字編程是無(wú)錯(cuò)誤的。</p><p>  為免更多的麻煩,我將提出the layers of the full-blown model。記住底下這些

74、用于網(wǎng)絡(luò)課堂測(cè)試:申請(qǐng),顯示,學(xué)期,運(yùn)輸,網(wǎng)絡(luò),數(shù)據(jù)連接,物理的。 物理層是一種硬件。應(yīng)用層是你能想象的離物理層最遠(yuǎn)的,是使用者與網(wǎng)絡(luò)相互作用的地方。</p><p>  現(xiàn)在,這個(gè)模式很普遍,你能把它當(dāng)成一個(gè)汽車修理向?qū)?lái)使用,如果你想的話。與Unix聯(lián)系緊密的層是: </p><p>  .應(yīng)用層(telent,ftp),</p><p>  . 主機(jī)對(duì)

75、主機(jī)的傳輸層(TCP,UDP),</p><p>  .網(wǎng)絡(luò)層 (IP 與 路由)</p><p>  .網(wǎng)絡(luò)鏈接層 (Ethernet, ATM)</p><p>  在這點(diǎn)上你能看到這些層怎么回應(yīng)原始數(shù)據(jù)的包裝。</p><p>  看到建立一份簡(jiǎn)單的郵件需要多大的工作量了嗎?Jeez!你必須自己用“貓”輸入郵件表頭。開玩笑而已。對(duì)于st

76、ream 套接字你所要做的就是把數(shù)據(jù)發(fā)送出去,而對(duì)于datagram 套接字你要做的是用你自己選擇的方法包裝郵件并把它發(fā)送出去。核心為你建立傳輸層和 網(wǎng)絡(luò)層,而硬件建立網(wǎng)絡(luò)鏈接層。啊,這就是現(xiàn)代科技!</p><p>  這里我們結(jié)束了對(duì)網(wǎng)絡(luò)理論的簡(jiǎn)要突擊。對(duì)了,我忘了告訴你們我想說(shuō)的關(guān)于路線安排的一切:什么也沒(méi)有!是的,我不準(zhǔn)備再談?wù)撍恕?lt;/p><p>  3.struts和數(shù)據(jù)管理

77、</p><p>  好吧,我們終于到了這里,是時(shí)候說(shuō)說(shuō)編程了。在這個(gè)部分我將提到套接字接口用到的各種數(shù)據(jù)類型。</p><p>  首先是簡(jiǎn)單的一個(gè)套接字描繪器。一個(gè)套接字描繪器是以下的類型:整數(shù),只是一個(gè)規(guī)律的整數(shù)。</p><p>  事情從這兒開始變得奇怪了,但是只要好好看下去并對(duì)我耐心點(diǎn)。</p><p>  要知道,有兩種字節(jié)指令

78、:最重要的頭字節(jié),或者次要的頭字節(jié)。前者稱為“網(wǎng)絡(luò)字節(jié)指令”。一些機(jī)器儲(chǔ)存它們的數(shù)據(jù)在網(wǎng)絡(luò)字節(jié)指令內(nèi)部,一些卻不是這樣的。當(dāng)我說(shuō)什么必須在網(wǎng)絡(luò)字節(jié)指令內(nèi)時(shí),你必須聯(lián)系一項(xiàng)功能(比如htons())來(lái)改變它的"主機(jī)字節(jié)序列"。</p><p>  如果我不說(shuō)“網(wǎng)絡(luò)字節(jié)指令”,你必須保留主機(jī)字節(jié)序列(令人好奇的是,“網(wǎng)絡(luò)字節(jié)指令”)。</p><p>  3.1轉(zhuǎn)換原始的地

79、址</p><p>  我們現(xiàn)在有權(quán)到下一節(jié)了,有太多關(guān)于主字節(jié)命令的網(wǎng)絡(luò)工作要說(shuō)。</p><p>  轉(zhuǎn)換——現(xiàn)在是行動(dòng)的時(shí)間了!</p><p>  完全正確。我們能轉(zhuǎn)換的有兩種方式:短的(2字節(jié))和長(zhǎng)的(4字節(jié))。這些功能也適用看不見的轉(zhuǎn)換。譬如說(shuō),你想轉(zhuǎn)換短的從主字節(jié)命令到網(wǎng)絡(luò)字節(jié)命令。</p><p>  以h代表主機(jī),接著是to

80、,然后用n代表網(wǎng)絡(luò),s代表short:h-to-n-s,或者 htons( ) (讀作Host to Network Short)</p><p><b>  那太容易…… </b></p><p>  你能用任何組合比如“n”,“h”,“s”和你想要的 “l(fā)”。舉個(gè)例子,在這里沒(méi)有stolh( ) (Short to Long Host)</p>&l

81、t;p><b>  功能。但是有</b></p><p>  .htons( )-Host to Network Short</p><p>  .htonl( )-Host to Network Long</p><p>  .ntons( )- Network to Host Short</p><p>  n

82、tonl( )- Network to Host Long</p><p>  現(xiàn)在你可能認(rèn)為你知道這些了。你可能在想,“如果我必須要改變字節(jié)命令,我可以做什么呢?”接著你可能想,“恩,別介意!”你可能還會(huì)想當(dāng)有68000臺(tái)計(jì)算機(jī)準(zhǔn)備用網(wǎng)絡(luò)字節(jié)命令,你不必用你的IP地址呼叫htonl( )。你是對(duì)的,但是如果試圖到達(dá)一臺(tái)與你用相反網(wǎng)絡(luò)字節(jié)命令的計(jì)算機(jī),你的程序?qū)⑹ _@是一個(gè)網(wǎng)絡(luò)世界(像比爾.蓋茨想得那樣)。記住

83、:在你傳到網(wǎng)上之前先把你的字節(jié)放在你的網(wǎng)絡(luò)字節(jié)命令里。</p><p>  最后一點(diǎn):為什么sin_addr和sin_port需要在struct sockaddr_in網(wǎng)絡(luò)字節(jié)命令里,而sin_family不需要 。答案是sin_addr和sin_ port層被分別包裝在封包里。它們必須用網(wǎng)絡(luò)字節(jié)命令。然而,sin_family領(lǐng)域只是被利用通過(guò)核心來(lái)決定結(jié)構(gòu)包括那種形式地址,所以必須在主字節(jié)命令里。同時(shí),也因?yàn)?/p>

84、sin_family不能在網(wǎng)絡(luò)上發(fā)送,它能存在與主字節(jié)命令當(dāng)中。</p><p>  3.2 IP地址和怎樣處理它們</p><p>  你是幸運(yùn)的,上面有一系列允許你操作IP地址的功能。首先,我們假設(shè)你有一個(gè)struct sockaddr_in ina,你想把一個(gè)IP“10.12.110.57”儲(chǔ)存到里面,你想用inet_addr( )這個(gè)程序的功能把IP地址這一串?dāng)?shù)字與點(diǎn)的符號(hào)轉(zhuǎn)換為看

85、不見的空隙。指令可以寫</p><p>  ina.sin_addr.s_addr=inet_addr(“10.12.110.57”);</p><p>  注意把inet_addr( )還原。</p><p>  這樣就行了?你不需要用htonl( )。</p><p><b>  好!</b></p>

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論