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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? syscalls.html

?? Beej的socket教材
?? HTML
?? 第 1 頁 / 共 3 頁
字號:
<HTML><HEAD><TITLE>System Calls or Bust</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="structs and Data Handling"HREF="structs.html"><LINKREL="NEXT"TITLE="Client-Server Background"HREF="clientserver.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="structs.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="clientserver.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="sect1"><H1CLASS="sect1"><ANAME="syscalls">4. System Calls or Bust</A></H1><P>This is the section where we get into the system calls that allowyou to access the network functionality of a Unix box.  When you callone of these functions, the kernel takes over and does all the work foryou automagically.</P><P>The place most people get stuck around here is what order to callthese things in.  In that, the <BCLASS="command">man</B> pages are no use,as you've probably discovered.  Well, to help with that dreadfulsituation, I've tried to lay out the system calls in the followingsections in <EM>exactly</EM> (approximately) the same orderthat you'll need to call them in your programs.</P><P>That, coupled with a few pieces of sample code here and there,some milk and cookies (which I fear you will have to supply yourself),and some raw guts and courage, and you'll be beaming data around theInternet like the Son of Jon Postel!</P><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="socket">4.1. <TTCLASS="function">socket()</TT>--Get the File Descriptor!</A></H2><P>I guess I can put it off no longer--I have to talk about the<TTCLASS="function">socket()</TT> system call.  Here's the breakdown:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;sys/types.h&#62;    #include &#60;sys/socket.h&#62;    int socket(int domain, int type, int protocol); </PRE></TD></TR></TABLE><P>But what are these arguments?  First,<TTCLASS="parameter"><I>domain</I></TT> should be set to"<TTCLASS="constant">AF_INET</TT>", just like in the <TTCLASS="type">structsockaddr_in</TT> (above.)  Next, the <TTCLASS="parameter"><I>type</I></TT>argument tells the kernel what kind of socket this is:<TTCLASS="constant">SOCK_STREAM</TT> or <TTCLASS="constant">SOCK_DGRAM</TT>.Finally, just set <TTCLASS="parameter"><I>protocol</I></TT> to"<TTCLASS="constant">0</TT>" to have <TTCLASS="function">socket()</TT> choosethe correct protocol based on the <TTCLASS="parameter"><I>type</I></TT>.  (Notes:there are many more <TTCLASS="parameter"><I>domain</I></TT>s than I've listed.There are many more <TTCLASS="parameter"><I>type</I></TT>s than I've listed.  Seethe <TTCLASS="function">socket()</TT> man page.  Also, there's a "better"way to get the <TTCLASS="parameter"><I>protocol</I></TT>.  See the<TTCLASS="function">getprotobyname()</TT> man page.)</P><P><TTCLASS="function">socket()</TT> simply returns to you a socketdescriptor that you can use in later system calls, or<TTCLASS="constant">-1</TT> on error.  The global variable<TTCLASS="parameter"><I>errno</I></TT> is set to the error's value (see the<TTCLASS="function">perror()</TT> man page.)</P><P>In some documentation, you'll see mention of a mystical"<TTCLASS="constant">PF_INET</TT>".  This is a weird etherial beast that israrely seen in nature, but I might as well clarify it a bit here.  Oncea long time ago, it was thought that maybe a address family (what the"AF" in "<TTCLASS="constant">AF_INET</TT>" stands for) might support severalprotocols that were referenced by their protocol family (what the "PF" in"<TTCLASS="constant">PF_INET</TT>" stands for).  That didn't happen.  Ohwell.  So the correct thing to do is to use <TTCLASS="constant">AF_INET</TT>in your <TTCLASS="type">struct sockaddr_in</TT> and <TTCLASS="constant">PF_INET</TT>in your call to <TTCLASS="function">socket()</TT>.  But practicallyspeaking, you can use <TTCLASS="constant">AF_INET</TT> everywhere.  And,since that's what W. Richard Stevens does in his book, that's what I'lldo here.</P><P>Fine, fine, fine, but what good is this socket?  The answer isthat it's really no good by itself, and you need to read on and makemore system calls for it to make any sense.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="bind">4.2. <TTCLASS="function">bind()</TT>--What port am I on?</A></H2><P>Once you have a socket, you might have to associate that socketwith a port on your local machine.  (This is commonly done if you'regoing to <TTCLASS="function">listen()</TT> for incoming connections on aspecific port--MUDs do this when they tell you to "telnet to x.y.z port6969".)  The port number is used by the kernel to match an incomingpacket to a certain process's socket descriptor.  If you're going toonly be doing a <TTCLASS="function">connect()</TT>, this may be unnecessary.Read it anyway, just for kicks.</P><P>Here is the synopsis for the <TTCLASS="function">bind()</TT> systemcall:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;sys/types.h&#62;    #include &#60;sys/socket.h&#62;    int bind(int sockfd, struct sockaddr *my_addr, int addrlen); </PRE></TD></TR></TABLE><P><TTCLASS="parameter"><I>sockfd</I></TT> is the socket file descriptorreturned by <TTCLASS="function">socket()</TT>.<TTCLASS="parameter"><I>my_addr</I></TT> is a pointer to a <TTCLASS="type">structsockaddr</TT> that contains information about your address, namely,port and IP address.  <TTCLASS="parameter"><I>addrlen</I></TT> can be set to<TTCLASS="computeroutput">sizeof(struct sockaddr)</TT>.</P><P>Whew.  That's a bit to absorb in one chunk.  Let's have anexample:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;string.h&#62;    #include &#60;sys/types.h&#62;    #include &#60;sys/socket.h&#62;    #include &#60;netinet/in.h&#62;    #define MYPORT 3490    main()    {        int sockfd;        struct sockaddr_in my_addr;        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 = inet_addr("10.12.110.57");        memset(&#38;(my_addr.sin_zero), '\0', 8); // zero the rest of the struct        // don't forget your error checking for bind():        bind(sockfd, (struct sockaddr *)&#38;my_addr, sizeof(struct sockaddr));        .        .        . </PRE></TD></TR></TABLE><P>There are a few things to notice here:<TTCLASS="parameter"><I>my_addr.sin_port</I></TT> is in Network Byte Order.  So is<TTCLASS="parameter"><I>my_addr.sin_addr.s_addr</I></TT>.  Another thing to watchout for is that the header files might differ from system to system.  Tobe sure, you should check your local <BCLASS="command">man</B>pages.</P><P>Lastly, on the topic of <TTCLASS="function">bind()</TT>, Ishould mention that some of the process of getting your own IP addressand/or port can can be automated:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;        my_addr.sin_port = 0; // choose an unused port at random        my_addr.sin_addr.s_addr = INADDR_ANY;  // use my IP address </PRE></TD></TR></TABLE><P>See, by setting <TTCLASS="parameter"><I>my_addr.sin_port</I></TT> to zero,you are telling <TTCLASS="function">bind()</TT> to choose the port for you.Likewise, by setting <TTCLASS="parameter"><I>my_addr.sin_addr.s_addr</I></TT> to<TTCLASS="constant">INADDR_ANY</TT>, you are telling it to automaticallyfill in the IP address of the machine the process is running on.</P><P>If you are into noticing little things, you might have seen that Ididn't put <TTCLASS="constant">INADDR_ANY</TT> into Network Byte Order!Naughty me.  However, I have inside info:<TTCLASS="constant">INADDR_ANY</TT> is really zero!  Zero still has zero onbits even if you rearrange the bytes.  However, purists will point outthat there could be a parallel dimension where<TTCLASS="constant">INADDR_ANY</TT> is, say, 12 and that my code won't workthere.  That's ok with me:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;        my_addr.sin_port = htons(0); // choose an unused port at random        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);  // use my IP address </PRE></TD></TR></TABLE><P>Now we're so portable you probably wouldn't believe it.  I justwanted to point that out, since most of the code you come across won'tbother running <TTCLASS="constant">INADDR_ANY</TT> through<TTCLASS="function">htonl()</TT>.</P><P><TTCLASS="function">bind()</TT> also returns <TTCLASS="constant">-1</TT>on error and sets <TTCLASS="parameter"><I>errno</I></TT> to the error'svalue.</P><P>Another thing to watch out for when calling<TTCLASS="function">bind()</TT>: don't go underboard with your port numbers.All ports below 1024 are RESERVED (unless you're the superuser)!  Youcan have any port number above that, right up to 65535 (provided theyaren't already being used by another program.)</P><P>Sometimes, you might notice, you try to rerun a server and<TTCLASS="function">bind()</TT> fails, claiming "Address already in use."What does that mean?  Well, a bit a of socket that was connected isstill hanging around in the kernel, and it's hogging the port.  You caneither wait for it to clear (a minute or so), or add code to yourprogram allowing it to reuse the port, like this:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    int yes=1;	//char yes='1'; // Solaris people use this    // lose the pesky "Address already in use" error message    if (setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&#38;yes,sizeof(int)) == -1) {        perror("setsockopt");        exit(1);    } </PRE></TD></TR></TABLE><P>One small extra final note about <TTCLASS="function">bind()</TT>:there are times when you won't absolutely have to call it.  If you are<TTCLASS="function">connect()</TT>ing to a remote machine and you don'tcare what your local port is (as is the case with<BCLASS="command">telnet</B> where you only care about the remote port),you can simply call <TTCLASS="function">connect()</TT>, it'll check to seeif the socket is unbound, and will <TTCLASS="function">bind()</TT> it to anunused local port if necessary.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="connect">4.3. <TTCLASS="function">connect()</TT>--Hey, you!</A></H2><P>Let's just pretend for a few minutes that you're a telnetapplication.  Your user commands you (just like in the movie<EM>TRON</EM>) to get a socket file descriptor.  You complyand call <TTCLASS="function">socket()</TT>.  Next, the usertells you to connect to "<TTCLASS="computeroutput">10.12.110.57</TT>"on port "<TTCLASS="computeroutput">23</TT>" (the standard telnetport.)  Yow!  What do you do now?</P><P>Lucky for you, program, you're now perusing the section on<TTCLASS="function">connect()</TT>--how to connect to a remote host.  Soread furiously onward!  No time to lose!</P><P>The <TTCLASS="function">connect()</TT> call is as follows:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;sys/types.h&#62;    #include &#60;sys/socket.h&#62;    int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); </PRE></TD></TR></TABLE><P><TTCLASS="parameter"><I>sockfd</I></TT> is our friendly neighborhood socketfile descriptor, as returned by the <TTCLASS="function">socket()</TT> call,<TTCLASS="parameter"><I>serv_addr</I></TT> is a <TTCLASS="type">struct sockaddr</TT>containing the destination port and IP address, and<TTCLASS="parameter"><I>addrlen</I></TT> can be set to<TTCLASS="computeroutput">sizeof(struct sockaddr)</TT>.</P><P>Isn't this starting to make more sense?  Let's have anexample:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;string.h&#62;    #include &#60;sys/types.h&#62;    #include &#60;sys/socket.h&#62;    #include &#60;netinet/in.h&#62;    #define DEST_IP   "10.12.110.57"    #define DEST_PORT 23    main()    {        int sockfd;        struct sockaddr_in dest_addr;   // will hold the destination addr        sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!        dest_addr.sin_family = AF_INET;          // host byte order        dest_addr.sin_port = htons(DEST_PORT);   // short, network byte order        dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);        memset(&#38;(dest_addr.sin_zero), '\0', 8);  // zero the rest of the struct        // don't forget to error check the connect()!        connect(sockfd, (struct sockaddr *)&#38;dest_addr, sizeof(struct sockaddr));        .        .        . </PRE></TD></TR></TABLE><P>Again, be sure to check the return value from<TTCLASS="function">connect()</TT>--it'll return <TTCLASS="constant">-1</TT> onerror and set the variable <TTCLASS="parameter"><I>errno</I></TT>.</P><P>Also, notice that we didn't call <TTCLASS="function">bind()</TT>.Basically, we don't care about our local port number; we only care wherewe're going (the remote port).  The kernel will choose a local port forus, and the site we connect to will automatically get this informationfrom us.  No worries.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="listen">4.4. <TTCLASS="function">listen()</TT>--Will somebody please callme?</A></H2><P>Ok, time for a change of pace.  What if you don't want to connect

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩免费看网站| 99精品国产99久久久久久白柏| 欧美亚洲禁片免费| 亚洲国产精品久久一线不卡| 欧美精品v国产精品v日韩精品 | 国产麻豆精品一区二区| 国产婷婷精品av在线| 91免费看`日韩一区二区| 一区二区在线电影| 欧美美女网站色| 极品美女销魂一区二区三区免费| 国产女人18水真多18精品一级做| 色综合婷婷久久| 秋霞国产午夜精品免费视频| 国产婷婷一区二区| 日本丶国产丶欧美色综合| 免费在线观看视频一区| 国产亚洲欧洲一区高清在线观看| 91免费观看国产| 免费的成人av| 国产精品萝li| 欧美一级久久久久久久大片| 国产丶欧美丶日本不卡视频| 亚洲蜜臀av乱码久久精品蜜桃| 欧美人与z0zoxxxx视频| 国产精品综合av一区二区国产馆| 欧美激情一区二区三区不卡| 欧美亚洲日本国产| 国产一区二区美女诱惑| 亚洲欧洲三级电影| 欧美成va人片在线观看| 色悠悠亚洲一区二区| 免费在线成人网| 亚洲欧洲韩国日本视频| 日韩精品一区二区三区视频| 99久久精品一区| 久久成人免费日本黄色| 一区二区三区精品视频| 久久久久久久网| 宅男在线国产精品| 色综合久久88色综合天天6| 国产真实乱对白精彩久久| 亚洲高清免费观看 | 欧美日韩久久久久久| 国产一区二区三区在线观看精品 | 全国精品久久少妇| 亚洲色图欧美在线| 久久久.com| 欧美一区二区大片| 欧美视频第二页| 成人精品在线视频观看| 久久精品国产精品亚洲红杏| 亚洲香肠在线观看| 亚洲情趣在线观看| 久久久三级国产网站| 欧美一卡二卡三卡四卡| 色久综合一二码| 成人高清视频在线观看| 国产一区二区精品久久99| 美女脱光内衣内裤视频久久网站| 亚洲国产人成综合网站| 一区二区三区中文字幕| 亚洲视频你懂的| 国产精品久久精品日日| 国产三级三级三级精品8ⅰ区| 日韩欧美国产一区二区三区 | 色综合久久88色综合天天 | 91在线看国产| aaa欧美日韩| 成人av综合一区| 国产白丝网站精品污在线入口| 久久国内精品视频| 蜜桃视频在线观看一区二区| 亚洲成人免费视频| 亚洲国产美国国产综合一区二区| 亚洲色图视频免费播放| 亚洲精品国产高清久久伦理二区| 亚洲天堂av老司机| 一区二区三区中文字幕| 亚洲自拍偷拍九九九| 亚洲国产精品久久不卡毛片| 香蕉av福利精品导航| 天天色综合天天| 蜜桃精品视频在线| 精彩视频一区二区三区| 六月婷婷色综合| 国内不卡的二区三区中文字幕| 国内精品国产三级国产a久久| 国产麻豆9l精品三级站| 成人短视频下载| 欧美综合在线视频| 欧美一区永久视频免费观看| 日韩久久免费av| 中文一区二区在线观看| 亚洲免费观看高清完整版在线观看 | 国产成人在线观看| 成人性视频网站| 在线视频国产一区| 91精品国产入口在线| 精品成人在线观看| 亚洲欧洲性图库| 午夜伦理一区二区| 激情都市一区二区| 北条麻妃一区二区三区| 欧美日韩亚洲不卡| 久久先锋资源网| 一区二区三区四区不卡视频| 美女视频免费一区| 91女人视频在线观看| 91精品国产91久久综合桃花| 国产精品毛片a∨一区二区三区| 夜夜精品视频一区二区| 激情国产一区二区| 在线精品视频免费播放| 日韩欧美资源站| 国产精品网站导航| 午夜精品成人在线视频| 国产精品99久久久久久久女警| 色综合天天综合网天天狠天天| 欧美一区午夜精品| 亚洲丝袜制服诱惑| 另类小说图片综合网| 99久久国产免费看| 欧美不卡一二三| 亚洲一级二级三级| 国产精品一区专区| 91麻豆精品国产无毒不卡在线观看| 中文字幕av免费专区久久| 美女一区二区三区| 欧美三区免费完整视频在线观看| 久久精品人人做人人爽97| 亚洲一二三专区| 波多野结衣中文字幕一区 | 欧美经典一区二区| 日本不卡的三区四区五区| av亚洲精华国产精华| 欧美成人激情免费网| 亚洲高清免费观看| 一本到不卡精品视频在线观看 | 成人av免费在线| 欧美大黄免费观看| 天涯成人国产亚洲精品一区av| 91麻豆精品在线观看| 国产亚洲va综合人人澡精品 | 国产一区二区三区香蕉| 欧美日韩二区三区| 亚洲黄色性网站| 成人av在线看| 2023国产精品| 另类小说图片综合网| 欧美精品三级在线观看| 一级女性全黄久久生活片免费| jlzzjlzz欧美大全| 国产日本欧洲亚洲| 国产成a人亚洲精| 久久精品男人天堂av| 极品瑜伽女神91| 亚洲精品一区二区三区蜜桃下载| 日韩和的一区二区| 欧美丰满高潮xxxx喷水动漫 | 麻豆国产精品一区二区三区| 欧美色图天堂网| 亚洲高清免费在线| 欧美日韩高清一区二区三区| 午夜天堂影视香蕉久久| 欧美猛男超大videosgay| 亚洲成人激情社区| 欧美午夜精品久久久久久超碰 | 精油按摩中文字幕久久| 精品久久人人做人人爽| 狠狠色狠狠色综合系列| 久久久亚洲午夜电影| 岛国一区二区在线观看| 国产精品久久久久三级| 99免费精品视频| 亚洲精品国产高清久久伦理二区| 一本久久综合亚洲鲁鲁五月天| 曰韩精品一区二区| 欧美日韩亚洲丝袜制服| 视频精品一区二区| 日韩欧美你懂的| 国产精品91xxx| 中文字幕综合网| 色呦呦国产精品| 青青草成人在线观看| 精品国精品自拍自在线| 国产91精品精华液一区二区三区 | 国产成人av在线影院| 国产精品人成在线观看免费| 色婷婷av一区二区| 视频一区视频二区中文| 精品黑人一区二区三区久久| 成人黄色在线看| 亚洲va欧美va人人爽午夜| 日韩视频一区二区三区在线播放| 久久99九九99精品| 中文字幕一区二区日韩精品绯色| 日本韩国精品在线| 看电视剧不卡顿的网站| 国产精品久久久久婷婷|