亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? structs.html

?? Beej的socket教材
?? HTML
字號(hào):
<HTML><HEAD><TITLE>structs and Data Handling</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.70"><LINKREL="HOME"TITLE="Beej's Guide to Network Programming"HREF="index.html"><LINKREL="PREVIOUS"TITLE="What is a socket?"HREF="theory.html"><LINKREL="NEXT"TITLE="System Calls or Bust"HREF="syscalls.html"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=utf-8"></HEAD><BODYCLASS="sect1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Beej's Guide to Network Programming</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="theory.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="syscalls.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="sect1"><H1CLASS="sect1"><ANAME="structs">3. <TTCLASS="type">struct</TT>s and Data Handling</A></H1><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 socketsinterface, since some of them are a real bear to figure out.</P><P>First the easy one: a socket descriptor.  A socket descriptor isthe following type:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    int </PRE></TD></TR></TABLE><P>Just a regular <TTCLASS="type">int</TT>.</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(sometimes called an "octet") first, or least significant byte first.The former is called "Network Byte Order".  Some machines store theirnumbers internally in Network Byte Order, some don't.  When I saysomething has to be in Network Byte Order, you have to call a function(such as <TTCLASS="function">htons()</TT>) to change it from "Host ByteOrder".  If I don't say "Network Byte Order", then you must leave thevalue in Host Byte Order.</P><P>(For the curious, "Network Byte Order" is also know as "Big-EndianByte Order".)</P><P>My First Struct<SUP>TM</SUP>--<TTCLASS="type">structsockaddr</TT>.  This structure holds socket address information formany types of sockets:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    struct sockaddr {        unsigned short    sa_family;    // address family, AF_xxx        char              sa_data[14];  // 14 bytes of protocol address    }; </PRE></TD></TR></TABLE><P><TTCLASS="parameter"><I>sa_family</I></TT> can be a variety of things, butit'll be <TTCLASS="constant">AF_INET</TT> for everything we do in thisdocument.  <TTCLASS="parameter"><I>sa_data</I></TT> contains a destination addressand port number for the socket.  This is rather unwieldy since you don'twant to tediously pack the address in the <TTCLASS="parameter"><I>sa_data</I></TT>by hand.</P><P>To deal with <TTCLASS="type">struct sockaddr</TT>, programmers created aparallel structure: <TTCLASS="type">struct sockaddr_in</TT> ("in" for"Internet".)</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    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    }; </PRE></TD></TR></TABLE><P>This structure makes it easy to reference elements of the socketaddress.  Note that <TTCLASS="parameter"><I>sin_zero</I></TT> (which is includedto pad the structure to the length of a <TTCLASS="type">struct sockaddr</TT>)should be set to all zeros with the function<TTCLASS="function">memset()</TT>.  Also, and this is the<EM>important</EM> bit, a pointer to a <TTCLASS="type">structsockaddr_in</TT> can be cast to a pointer to a <TTCLASS="type">structsockaddr</TT> and vice-versa.  So even though<TTCLASS="function">socket()</TT> wants a <TTCLASS="type">struct sockaddr*</TT>, youcan still use a <TTCLASS="type">struct sockaddr_in</TT> and cast it at the lastminute!  Also, notice that <TTCLASS="parameter"><I>sin_family</I></TT> correspondsto <TTCLASS="parameter"><I>sa_family</I></TT> in a <TTCLASS="type">struct sockaddr</TT>and should be set to "<TTCLASS="constant">AF_INET</TT>".  Finally, the<TTCLASS="parameter"><I>sin_port</I></TT> and <TTCLASS="parameter"><I>sin_addr</I></TT> mustbe in <EM>Network Byte Order</EM>!</P><P>"But," you object, "how can the entire structure,<TTCLASS="type">struct in_addr sin_addr</TT>, be in NetworkByte Order?"  This question requires careful examination of thestructure <TTCLASS="type">struct in_addr</TT>, one of theworst unions alive:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    // Internet address (a structure for historical reasons)    struct in_addr {        unsigned long s_addr; // that's a 32-bit long, or 4 bytes    }; </PRE></TD></TR></TABLE><P>Well, it <EM>used</EM> to be a union, but now thosedays seem to be gone.  Good riddance.  So if you have declared<TTCLASS="parameter"><I>ina</I></TT> to be of type <TTCLASS="type">structsockaddr_in</TT>, then <TTCLASS="parameter"><I>ina.sin_addr.s_addr</I></TT>references the 4-byte IP address (in Network Byte Order).  Note thateven if your system still uses the God-awful union for <TTCLASS="type">structin_addr</TT>, you can still reference the 4-byte IP address in exactlythe same way as I did above (this due to<TTCLASS="computeroutput">#define</TT>s.)</P><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="convert">3.1. Convert the Natives!</A></H2><P>We've now been lead right into the next section.  There's been toomuch talk about this Network to Host Byte Order conversion--now is thetime for action!</P><P>All righty.  There are two types that you can convert:<TTCLASS="type">short</TT> (two bytes) and <TTCLASS="type">long</TT> (four bytes).These functions work for the <TTCLASS="type">unsigned</TT> variations as well.Say you want to convert a <TTCLASS="type">short</TT> from Host Byte Order toNetwork Byte Order.  Start with "h" for "host", follow it with "to",then "n" for "network", and "s" for "short": h-to-n-s, or<TTCLASS="function">htons()</TT> (read: "Host to Network Short").</P><P>It's almost too easy...</P><P>You can use every combination if "n", "h", "s", and "l" you want,not counting the really stupid ones.  For example, there is NOT a<TTCLASS="function">stolh()</TT> ("Short to Long Host") function--not at thisparty, anyway.  But there are:</P><P>&#13;<P></P><UL><LI><P><TTCLASS="function">htons()</TT> -- "Host to Network Short"</P></LI><LI><P><TTCLASS="function">htonl()</TT> -- "Host to Network Long"</P></LI><LI><P><TTCLASS="function">ntohs()</TT> -- "Network to Host Short"</P></LI><LI><P><TTCLASS="function">ntohl()</TT> -- "Network to Host Long"</P></LI></UL></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 order on a <TTCLASS="type">char</TT>?"Then you might think, "Uh, never mind."  You might also think that sinceyour 68000 machine already uses network byte order, you don't have tocall <TTCLASS="function">htonl()</TT> on your IP addresses.  You would beright, <EM>BUT</EM> if you try to port to a machine that hasreverse network byte order, your program will fail.  Be portable!  Thisis a Unix world!  (As much as Bill Gates would like to think otherwise.)Remember: put your bytes in Network Byte Order before you put them onthe network.</P><P>A final point: why do <TTCLASS="parameter"><I>sin_addr</I></TT> and<TTCLASS="parameter"><I>sin_port</I></TT> need to be in Network Byte Order in a<TTCLASS="type">struct sockaddr_in</TT>, but <TTCLASS="parameter"><I>sin_family</I></TT>does not?  The answer: <TTCLASS="parameter"><I>sin_addr</I></TT> and<TTCLASS="parameter"><I>sin_port</I></TT> get encapsulated in the packet at the IPand UDP layers, respectively.  Thus, they must be in Network Byte Order.However, the <TTCLASS="parameter"><I>sin_family</I></TT> field is only used by thekernel to determine what type of address the structure contains, so itmust be in Host Byte Order.  Also, since<TTCLASS="parameter"><I>sin_family</I></TT> does <EM>not</EM> get sentout on the network, it can be in Host Byte Order.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="ipaddr">3.2. IP Addresses and How to Deal With Them</A></H2><P>Fortunately for you, there are a bunch of functions  that allowyou to manipulate IP addresses.  No need to figure them out by hand andstuff them in a <TTCLASS="type">long</TT> with the<TTCLASS="computeroutput">&#60;&#60;</TT> operator.</P><P>First, let's say you have a <TTCLASS="type">struct sockaddr_in ina</TT>,and you have an IP address"<TTCLASS="computeroutput">10.12.110.57</TT>" that you want to storeinto it.  The function you want to use,<TTCLASS="function">inet_addr()</TT>, converts an IP address innumbers-and-dots notation into an unsigned long.  The assignment can bemade as follows:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    ina.sin_addr.s_addr = inet_addr("10.12.110.57"); </PRE></TD></TR></TABLE><P>Notice that <TTCLASS="function">inet_addr()</TT> returns the addressin Network Byte Order already--you don't have to call<TTCLASS="function">htonl()</TT>.  Swell!</P><P>Now, the above code snippet isn't very robust because there is noerror checking.  See, <TTCLASS="function">inet_addr()</TT> returns<TTCLASS="constant">-1</TT> on error.  Remember binary numbers?<TTCLASS="constant">(unsigned)-1</TT> just happens to correspond to the IPaddress <TTCLASS="computeroutput">255.255.255.255</TT>!  That's thebroadcast address!  Wrongo.  Remember to do your error checkingproperly.</P><P>Actually, there's a cleaner interface you can use instead of<TTCLASS="function">inet_addr()</TT>: it's called<TTCLASS="function">inet_aton()</TT> ("aton" means "ascii to network"):</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;sys/socket.h&#62;    #include &#60;netinet/in.h&#62;    #include &#60;arpa/inet.h&#62;    int inet_aton(const char *cp, struct in_addr *inp); </PRE></TD></TR></TABLE><P>And here's a sample usage, while packing a <TTCLASS="type">structsockaddr_in</TT> (this example will make more sense to you when youget to the sections on <AHREF="syscalls.html#bind">&#13;<TTCLASS="function">bind()</TT></A> and <AHREF="syscalls.html#connect"><TTCLASS="function">connect()</TT></A>.)</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    struct sockaddr_in my_addr;    my_addr.sin_family = AF_INET;         // host byte order    my_addr.sin_port = htons(MYPORT);     // short, network byte order    inet_aton("10.12.110.57", &#38;(my_addr.sin_addr));    memset(&#38;(my_addr.sin_zero), '\0', 8); // zero the rest of the struct </PRE></TD></TR></TABLE><P><TTCLASS="function">inet_aton()</TT>, <EM>unlike practicallyevery other socket-related function</EM>, returns non-zero onsuccess, and zero on failure.  And the address is passed back in<TTCLASS="parameter"><I>inp</I></TT>.</P><P>Unfortunately, not all platforms implement<TTCLASS="function">inet_aton()</TT> so, although its use is preferred, theolder more common <TTCLASS="function">inet_addr()</TT> is used in thisguide.</P><P>All right, now you can convert string IP addresses to their binaryrepresentations.  What about the other way around?  What if you have a<TTCLASS="type">struct in_addr</TT> and you want to print it in numbers-and-dotsnotation?  In this case, you'll want to use the function<TTCLASS="function">inet_ntoa()</TT> ("ntoa" means "network to ascii") likethis:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    printf("%s", inet_ntoa(ina.sin_addr)); </PRE></TD></TR></TABLE><P>That will print the IP address.  Note that<TTCLASS="function">inet_ntoa()</TT> takes a <TTCLASS="type">struct in_addr</TT> asan argument, not a <TTCLASS="type">long</TT>.  Also notice that it returns apointer to a char.  This points to a statically stored char array within<TTCLASS="function">inet_ntoa()</TT> so that each time you call<TTCLASS="function">inet_ntoa()</TT> it will overwrite the last IP addressyou asked for.  For example:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    char *a1, *a2;    .    .    a1 = inet_ntoa(ina1.sin_addr);  // this is 192.168.4.14    a2 = inet_ntoa(ina2.sin_addr);  // this is 10.12.110.57    printf("address 1: %s\n",a1);    printf("address 2: %s\n",a2); </PRE></TD></TR></TABLE><P>will print:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    address 1: 10.12.110.57    address 2: 10.12.110.57 </PRE></TD></TR></TABLE><P>If you need to save the address, <TTCLASS="function">strcpy()</TT> itto your own character array.</P><P>That's all on this topic for now.  Later, you'll learn to converta string like "whitehouse.gov" into its corresponding IP address (see<AHREF="syscalls.html#dns">DNS</A>, below.)</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="theory.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="syscalls.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">What is a socket?</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top">&nbsp;</TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">System Calls or Bust</TD></TR></TABLE></DIV></BODY></HTML>

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
图片区日韩欧美亚洲| 久久久一区二区三区捆绑**| 欧美日韩情趣电影| 欧美不卡一二三| 国产亚洲婷婷免费| 亚洲精品国产a| 全国精品久久少妇| 成人免费视频国产在线观看| 色欧美片视频在线观看在线视频| 欧美一区二区三区啪啪| 久久精品欧美一区二区三区不卡 | 亚洲综合色网站| 蜜桃视频在线观看一区| av一区二区三区| 这里只有精品电影| 亚洲国产经典视频| 奇米一区二区三区av| 成人app网站| 日韩精品一区二| 亚洲欧美另类图片小说| 黄色资源网久久资源365| 色综合天天狠狠| 久久综合九色综合97婷婷女人| 亚洲三级电影全部在线观看高清| 老司机免费视频一区二区| 99国产欧美另类久久久精品| 精品裸体舞一区二区三区| 一级做a爱片久久| 国产91丝袜在线18| 日韩欧美在线123| 亚洲综合在线第一页| 国产福利一区二区| 91麻豆精品国产无毒不卡在线观看| 欧美国产精品专区| 久久www免费人成看片高清| 欧洲亚洲精品在线| 国产精品久久久99| 国产一区二区免费看| 在线播放91灌醉迷j高跟美女 | av电影天堂一区二区在线观看| 日韩一区二区三区视频在线观看| 亚洲免费看黄网站| 成人a免费在线看| 精品久久五月天| 日本不卡一二三| 欧美亚一区二区| 亚洲精品五月天| 成人av资源在线| 国产亚洲欧洲997久久综合| 奇米精品一区二区三区四区| 欧美日韩亚洲不卡| 最新成人av在线| 国产成a人亚洲精| 精品成a人在线观看| 蜜臀a∨国产成人精品| 欧美浪妇xxxx高跟鞋交| 亚洲影院免费观看| 91成人网在线| 亚洲综合一区二区三区| 色伊人久久综合中文字幕| 国产精品午夜春色av| 国产成人av影院| 久久午夜老司机| 狠狠色丁香久久婷婷综合丁香| 日韩一区二区精品在线观看| 日韩在线观看一区二区| 欧美三级乱人伦电影| 亚洲第一搞黄网站| 欧美日韩免费高清一区色橹橹 | 欧美另类高清zo欧美| 亚洲成年人影院| 欧美精品乱码久久久久久按摩| 亚洲一区在线观看视频| 欧美日韩在线观看一区二区| 亚洲一区二区美女| 欧美日韩精品久久久| 亚洲动漫第一页| 91精品国产欧美日韩| 玖玖九九国产精品| 精品剧情v国产在线观看在线| 韩国v欧美v亚洲v日本v| 国产永久精品大片wwwapp| 亚洲精品在线电影| 国产成人自拍网| 国产精品久久看| 色老头久久综合| 天堂蜜桃一区二区三区| 日韩一区二区三区视频在线| 国内精品久久久久影院色| 国产清纯美女被跳蛋高潮一区二区久久w | 不卡视频免费播放| 亚洲激情图片qvod| 欧美欧美午夜aⅴ在线观看| 免费国产亚洲视频| 国产亚洲va综合人人澡精品| 97成人超碰视| 亚洲成a人在线观看| 欧美成人艳星乳罩| 成人精品在线视频观看| 一个色妞综合视频在线观看| 91精品国产综合久久久久| 国产精品小仙女| 亚洲免费在线视频一区 二区| 欧美肥妇bbw| 国产不卡视频在线观看| 一区二区三区电影在线播| 日韩欧美一区电影| 成人黄色在线看| 五月婷婷激情综合| 国产日韩欧美不卡在线| 色噜噜久久综合| 久久激情五月婷婷| 国产精品嫩草影院av蜜臀| 欧美日韩电影在线| 国产91精品一区二区麻豆网站 | 欧美伦理电影网| 国产成人精品一区二区三区四区| 亚洲黄色av一区| 日韩欧美亚洲国产另类| 成年人国产精品| 日韩国产精品久久久久久亚洲| 国产日韩亚洲欧美综合| 欧美三级一区二区| 风间由美中文字幕在线看视频国产欧美| 亚洲人精品午夜| 欧美mv日韩mv| 色琪琪一区二区三区亚洲区| 国产一区二区不卡老阿姨| 亚洲综合无码一区二区| 久久人人97超碰com| 欧美日韩一区二区三区高清| 国产经典欧美精品| 热久久免费视频| 亚洲精品乱码久久久久久| 久久青草国产手机看片福利盒子 | 日韩精品视频网站| 亚洲视频资源在线| 久久中文字幕电影| 欧美日韩中文国产| 99久久综合精品| 精一区二区三区| 亚洲一区二区三区美女| 国产精品久久久久一区二区三区 | 国产一区在线视频| 日韩高清不卡一区二区三区| 亚洲精选免费视频| 国产日韩亚洲欧美综合| 欧美第一区第二区| 欧美日韩国产综合一区二区| 91蜜桃免费观看视频| 国产麻豆视频一区二区| 日本亚洲免费观看| 亚洲成a人v欧美综合天堂| 亚洲视频资源在线| 中文文精品字幕一区二区| 26uuu亚洲综合色| 91精品久久久久久蜜臀| 在线亚洲精品福利网址导航| 99久免费精品视频在线观看| 国产成人免费网站| 国产呦萝稀缺另类资源| 久久精品国产亚洲一区二区三区| 亚洲va中文字幕| 一区二区三区日韩欧美精品| 亚洲色图制服诱惑| 国产精品美女久久久久久久网站| 2022国产精品视频| 欧美变态tickling挠脚心| 51精品国自产在线| 欧美日韩综合在线免费观看| 欧美亚洲综合一区| 欧美专区日韩专区| 一本一道综合狠狠老| 91视频com| 色噜噜夜夜夜综合网| 色先锋资源久久综合| 色婷婷激情一区二区三区| 91一区二区三区在线观看| 99久久免费国产| 91碰在线视频| 99国产精品久| 一本大道久久a久久精二百| 色综合天天综合在线视频| 色悠久久久久综合欧美99| 欧美在线视频你懂得| 在线观看日产精品| 欧美日韩电影在线| 日韩午夜在线观看视频| 欧美大片在线观看| 精品对白一区国产伦| 国产午夜精品在线观看| 国产精品国产馆在线真实露脸| 国产精品黄色在线观看| 亚洲人成网站在线| 亚洲综合视频在线观看| 日本欧洲一区二区| 国产又粗又猛又爽又黄91精品| 成人深夜视频在线观看| 91国在线观看| 日韩无一区二区|