亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
亚洲美腿欧美偷拍| 国产日本欧美一区二区| 亚洲妇熟xx妇色黄| 在线电影欧美成精品| 美女免费视频一区| 久久一二三国产| 成人国产免费视频| 亚洲激情第一区| 日韩一区和二区| 国产精品一卡二卡| 一区二区三区在线视频播放| 91麻豆精品国产91久久久久久| 免费人成在线不卡| 国产欧美精品区一区二区三区| 99麻豆久久久国产精品免费| 偷拍日韩校园综合在线| 久久久三级国产网站| 99精品国产91久久久久久| 亚洲高清久久久| 久久久电影一区二区三区| 91久久人澡人人添人人爽欧美| 视频一区二区欧美| 国产欧美一区视频| 欧美三级视频在线观看| 国产美女在线精品| 亚洲曰韩产成在线| 国产三区在线成人av| 欧洲精品中文字幕| 韩国毛片一区二区三区| 亚洲女性喷水在线观看一区| 日韩三级视频在线看| jizzjizzjizz欧美| 久久精品国产在热久久| 亚洲精品中文字幕乱码三区| 日韩精品一区二区三区在线| 972aa.com艺术欧美| 国产专区欧美精品| 亚洲最色的网站| 久久久不卡影院| 51精品久久久久久久蜜臀| 成+人+亚洲+综合天堂| 麻豆成人久久精品二区三区红| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ原创 | 免费在线看成人av| 亚洲欧洲三级电影| 欧美mv日韩mv亚洲| 欧美日韩一区国产| av激情综合网| 激情五月婷婷综合网| 午夜av一区二区三区| 国产精品免费看片| 久久一二三国产| 日韩一区二区麻豆国产| 在线视频国内一区二区| jiyouzz国产精品久久| 国产一区二区三区高清播放| 日本怡春院一区二区| 亚洲国产精品久久人人爱| 国产精品久久久久aaaa| 久久网这里都是精品| 日韩一本二本av| 欧美高清一级片在线| 色综合色综合色综合| www.亚洲精品| 成人福利视频在线| 国产一区视频在线看| 九九国产精品视频| 日本 国产 欧美色综合| 日本不卡一区二区三区高清视频| 一区二区三区中文免费| 亚洲精品久久嫩草网站秘色| 国产精品灌醉下药二区| 中文字幕一区在线| 成人免费一区二区三区视频| 国产精品久久久久久久第一福利 | 久久综合色婷婷| 日韩女优毛片在线| 日韩一区二区三区电影在线观看| 91.成人天堂一区| 91精品国产综合久久香蕉麻豆| 欧美精选午夜久久久乱码6080| 欧美日韩高清影院| 91精品国产综合久久久久| 制服丝袜成人动漫| 日韩女优电影在线观看| 久久亚洲私人国产精品va媚药| 欧美精品一区二区三区蜜臀| 久久婷婷一区二区三区| 国产精品久久久久久福利一牛影视| 国产清纯在线一区二区www| 日本一区二区免费在线| 亚洲色图一区二区| 亚洲亚洲精品在线观看| 日韩专区一卡二卡| 久久99精品一区二区三区三区| 激情欧美一区二区| 成人av中文字幕| 在线观看日韩电影| 欧美一区二区三区在线视频| 精品国产伦一区二区三区观看方式| 久久精品亚洲一区二区三区浴池| 国产亚洲成av人在线观看导航| 欧美极品少妇xxxxⅹ高跟鞋| 亚洲精品免费播放| 日韩av电影天堂| 国产精品影视在线| 色婷婷综合中文久久一本| 欧美久久一二三四区| ww亚洲ww在线观看国产| 国产精品不卡一区二区三区| 亚洲国产cao| 狠狠色丁香九九婷婷综合五月| www.欧美色图| 在线不卡欧美精品一区二区三区| 久久综合999| 一区二区免费视频| 美女一区二区三区| 91在线丨porny丨国产| 欧美丰满少妇xxxbbb| 国产欧美精品区一区二区三区 | 亚洲一区二区在线观看视频| 久久99国产精品免费网站| 91丨九色丨蝌蚪富婆spa| 91精品国产高清一区二区三区| 国产日韩欧美不卡| 免费一级片91| 欧美综合视频在线观看| ww久久中文字幕| 亚洲第一会所有码转帖| 福利一区福利二区| 这里是久久伊人| 亚洲激情在线激情| 成人做爰69片免费看网站| 欧美精品久久一区| 亚洲乱码国产乱码精品精小说| 麻豆传媒一区二区三区| 91九色02白丝porn| 欧美韩国一区二区| 狠狠色丁香婷婷综合| 欧美伦理视频网站| 亚洲天堂中文字幕| 国产精一品亚洲二区在线视频| 欧美三级乱人伦电影| 亚洲视频资源在线| 成人黄色软件下载| 久久久电影一区二区三区| 美女一区二区久久| 91精品欧美综合在线观看最新| 亚洲精品美腿丝袜| 色综合久久中文综合久久97| 国产视频一区在线观看| 精油按摩中文字幕久久| 日韩一二在线观看| 免费黄网站欧美| 欧美精品自拍偷拍动漫精品| 一区二区高清在线| 91色乱码一区二区三区| 国产精品久久久久久久久久久免费看| 国产一区二区精品久久99| 日韩亚洲欧美综合| 免费成人深夜小野草| 91精品国产色综合久久不卡蜜臀| 亚洲激情六月丁香| 在线区一区二视频| 亚洲综合一二三区| 日本精品一区二区三区高清| 亚洲天堂av一区| 色网站国产精品| 亚洲激情校园春色| 在线亚洲高清视频| 性做久久久久久免费观看| 欧洲日韩一区二区三区| 性感美女久久精品| 8x8x8国产精品| 精品系列免费在线观看| 久久久午夜电影| 成人免费视频网站在线观看| 欧美国产日韩一二三区| a级精品国产片在线观看| 国产欧美一区二区精品婷婷| 福利视频网站一区二区三区| 成人免费一区二区三区视频 | 久久爱www久久做| 精品国产一区二区三区av性色 | 成人黄色av网站在线| 亚洲视频香蕉人妖| 欧美性受极品xxxx喷水| 亚洲影院免费观看| 欧美一级在线观看| 高清av一区二区| 一区二区三区欧美久久| 欧美日韩高清一区二区三区| 久久99久久久久| 国产精品毛片高清在线完整版| 色噜噜久久综合| 蜜臂av日日欢夜夜爽一区| 欧美国产成人在线| 欧美亚洲高清一区| 国产一区二区三区在线观看免费 | 欧美白人最猛性xxxxx69交|