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

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

?? syscalls.html

?? Beej的socket教材
?? HTML
?? 第 1 頁 / 共 3 頁
字號:
to a remote host.  Say, just for kicks, that you want to wait forincoming connections and handle them in some way.  The process is twostep: first you <TTCLASS="function">listen()</TT>, then you<TTCLASS="function">accept()</TT> (see below.)</P><P>The listen call is fairly simple, but requires a bit ofexplanation:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    int listen(int sockfd, int backlog); </PRE></TD></TR></TABLE><P><TTCLASS="parameter"><I>sockfd</I></TT> is the usual socket file descriptorfrom the <TTCLASS="function">socket()</TT> system call.<TTCLASS="parameter"><I>backlog</I></TT> is the number of connections allowed onthe incoming queue.  What does that mean?  Well, incoming connectionsare going to wait in this queue until you <TTCLASS="function">accept()</TT>them (see below) and this is the limit on how many can queue up.  Mostsystems silently limit this number to about 20; you can probably getaway with setting it to <TTCLASS="constant">5</TT> or<TTCLASS="constant">10</TT>.</P><P>Again, as per usual, <TTCLASS="function">listen()</TT> returns<TTCLASS="constant">-1</TT> and sets <TTCLASS="parameter"><I>errno</I></TT> onerror.</P><P>Well, as you can probably imagine, we need to call<TTCLASS="function">bind()</TT> before we call <TTCLASS="function">listen()</TT>or the kernel will have us listening on a random port.  Bleah!  So ifyou're going to be listening for incoming connections, the sequence ofsystem calls you'll make is:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    socket();    bind();    listen();    /* accept() goes here */ </PRE></TD></TR></TABLE><P>I'll just leave that in the place of sample code, since it'sfairly self-explanatory.  (The code in the <TTCLASS="function">accept()</TT>section, below, is more complete.)  The really tricky part of this wholesha-bang is the call to <TTCLASS="function">accept()</TT>.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="accept">4.5. <TTCLASS="function">accept()</TT>--"Thank you for calling port3490."</A></H2><P>Get ready--the <TTCLASS="function">accept()</TT> call is kinda weird!What's going to happen is this: someone far far away will try to<TTCLASS="function">connect()</TT> to your machine on a port that you are<TTCLASS="function">listen()</TT>ing on.  Their connection will be queuedup waiting to be <TTCLASS="function">accept()</TT>ed.  You call<TTCLASS="function">accept()</TT> and you tell it to get the pendingconnection.  It'll return to you a <EM>brand new socket filedescriptor</EM> to use for this single connection!  That's right,suddenly you have <EM>two socket file descriptors</EM> forthe price of one!  The original one is still listening on your port andthe newly created one is finally ready to <TTCLASS="function">send()</TT>and <TTCLASS="function">recv()</TT>.  We're there! </P><P>The call is as follows:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;     #include &#60;sys/socket.h&#62;     int accept(int sockfd, void *addr, int *addrlen); </PRE></TD></TR></TABLE><P><TTCLASS="parameter"><I>sockfd</I></TT> is the<TTCLASS="function">listen()</TT>ing socket descriptor.  Easy enough.<TTCLASS="parameter"><I>addr</I></TT> will usually be a pointer to a local<TTCLASS="type">struct sockaddr_in</TT>.  This is where the information aboutthe incoming connection will go (and with it you can determine whichhost is calling you from which port).  <TTCLASS="parameter"><I>addrlen</I></TT> isa local integer variable that should be set to<TTCLASS="computeroutput">sizeof(struct sockaddr_in)</TT> before itsaddress is passed to <TTCLASS="function">accept()</TT>.  Accept will not putmore than that many bytes into <TTCLASS="parameter"><I>addr</I></TT>. If it putsfewer in, it'll change the value of <TTCLASS="parameter"><I>addrlen</I></TT> toreflect that.</P><P>Guess what?  <TTCLASS="function">accept()</TT> returns<TTCLASS="constant">-1</TT> and sets <TTCLASS="parameter"><I>errno</I></TT> if anerror occurs.  Betcha didn't figure that.</P><P>Like before, this is a bunch to absorb in one chunk, so here's asample code fragment for your perusal:</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    // the port users will be connecting to    #define BACKLOG 10     // how many pending connections queue will hold    main()    {        int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd        struct sockaddr_in my_addr;    // my address information        struct sockaddr_in their_addr; // connector's address information        int sin_size;        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 = INADDR_ANY; // auto-fill with my IP        memset(&#38;(my_addr.sin_zero), '\0', 8); // zero the rest of the struct        // don't forget your error checking for these calls:        bind(sockfd, (struct sockaddr *)&#38;my_addr, sizeof(struct sockaddr));        listen(sockfd, BACKLOG);        sin_size = sizeof(struct sockaddr_in);        new_fd = accept(sockfd, (struct sockaddr *)&#38;their_addr, &#38;sin_size);        .        .        . </PRE></TD></TR></TABLE><P>Again, note that we will use the socket descriptor<TTCLASS="parameter"><I>new_fd</I></TT> for all <TTCLASS="function">send()</TT> and<TTCLASS="function">recv()</TT> calls.  If you're only getting one singleconnection ever, you can <TTCLASS="function">close()</TT> the listening<TTCLASS="parameter"><I>sockfd</I></TT> in order to prevent more incomingconnections on the same port, if you so desire.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="sendrecv">4.6. <TTCLASS="function">send()</TT> and <TTCLASS="function">recv()</TT>--Talkto me, baby!</A></H2><P>These two functions are for communicating over stream sockets orconnected datagram sockets.  If you want to use regular unconnecteddatagram sockets, you'll need to see the section on <AHREF="syscalls.html#sendtorecv"><TTCLASS="function">sendto()</TT> and<TTCLASS="function">recvfrom()</TT></A>, below.</P><P>The <TTCLASS="function">send()</TT> call:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    int send(int sockfd, const void *msg, int len, int flags); </PRE></TD></TR></TABLE><P><TTCLASS="parameter"><I>sockfd</I></TT> is the socket descriptor you want tosend data to (whether it's the one returned by<TTCLASS="function">socket()</TT> or the one you got with<TTCLASS="function">accept()</TT>.) <TTCLASS="parameter"><I>msg</I></TT> is a pointerto the data you want to send, and <TTCLASS="parameter"><I>len</I></TT> is thelength of that data in bytes.  Just set <TTCLASS="parameter"><I>flags</I></TT> to<TTCLASS="constant">0</TT>.  (See the <TTCLASS="function">send()</TT> man pagefor more information concerning flags.)</P><P>Some sample code might be:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    char *msg = "Beej was here!";    int len, bytes_sent;    .    .    len = strlen(msg);    bytes_sent = send(sockfd, msg, len, 0);    .    .    . </PRE></TD></TR></TABLE><P><TTCLASS="function">send()</TT> returns the number of bytes actuallysent out--<EM>this might be less than the number you told it tosend!</EM>  See, sometimes you tell it to send a whole gob of dataand it just can't handle it.  It'll fire off as much of the data as itcan, and trust you to send the rest later.  Remember, if the valuereturned by <TTCLASS="function">send()</TT> doesn't match the value in<TTCLASS="parameter"><I>len</I></TT>, it's up to you to send the rest of thestring.  The good news is this: if the packet is small (less than 1K orso) it will <EM>probably</EM> manage to send the whole thingall in one go.  Again, <TTCLASS="constant">-1</TT> is returned on error, and<TTCLASS="parameter"><I>errno</I></TT> is set to the error number.</P><P>The <TTCLASS="function">recv()</TT> call is similar in manyrespects:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    int recv(int sockfd, void *buf, int len, unsigned int flags); </PRE></TD></TR></TABLE><P><TTCLASS="parameter"><I>sockfd</I></TT> is the socket descriptor to readfrom, <TTCLASS="parameter"><I>buf</I></TT> is the buffer to read the informationinto, <TTCLASS="parameter"><I>len</I></TT> is the maximum length of the buffer,and <TTCLASS="parameter"><I>flags</I></TT> can again be set to<TTCLASS="constant">0</TT>.  (See the <TTCLASS="function">recv()</TT> man pagefor flag information.)</P><P><TTCLASS="function">recv()</TT> returns the number of bytes actuallyread into the buffer, or <TTCLASS="constant">-1</TT> on error (with<TTCLASS="parameter"><I>errno</I></TT> set, accordingly.)</P><P>Wait!  <TTCLASS="function">recv()</TT> can return<TTCLASS="constant">0</TT>.  This can mean only one thing: the remote sidehas closed the connection on you!  A return value of<TTCLASS="constant">0</TT> is <TTCLASS="function">recv()</TT>'s way of lettingyou know this has occurred.</P><P>There, that was easy, wasn't it?  You can now pass data back andforth on stream sockets!  Whee!  You're a Unix NetworkProgrammer!</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="sendtorecv">4.7. <TTCLASS="function">sendto()</TT> and<TTCLASS="function">recvfrom()</TT>--Talk to me, DGRAM-style</A></H2><P>"This is all fine and dandy," I hear you saying, "but where doesthis leave me with unconnected datagram sockets?"  No problemo, amigo.We have just the thing.</P><P>Since datagram sockets aren't connected to a remote host, guess whichpiece of information we need to give before we send a packet?  That'sright!  The destination address!  Here's the scoop:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    int sendto(int sockfd, const void *msg, int len, unsigned int flags,               const struct sockaddr *to, int tolen); </PRE></TD></TR></TABLE><P>As you can see, this call is basically the same as the call to<TTCLASS="function">send()</TT> with the addition of two other pieces ofinformation.  <TTCLASS="parameter"><I>to</I></TT> is a pointer to a <TTCLASS="type">structsockaddr</TT> (which you'll probably have as a <TTCLASS="type">structsockaddr_in</TT> and cast it at the last minute) which contains thedestination IP address and port.  <TTCLASS="parameter"><I>tolen</I></TT> cansimply be set to <TTCLASS="computeroutput">sizeof(structsockaddr)</TT>.</P><P>Just like with <TTCLASS="function">send()</TT>,<TTCLASS="function">sendto()</TT> returns the number of bytes actually sent(which, again, might be less than the number of bytes you told it tosend!), or <TTCLASS="constant">-1</TT> on error.</P><P>Equally similar are <TTCLASS="function">recv()</TT> and<TTCLASS="function">recvfrom()</TT>.  The synopsis of<TTCLASS="function">recvfrom()</TT> is:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    int recvfrom(int sockfd, void *buf, int len, unsigned int flags,                 struct sockaddr *from, int *fromlen); </PRE></TD></TR></TABLE><P>Again, this is just like <TTCLASS="function">recv()</TT> with theaddition of a couple fields.  <TTCLASS="parameter"><I>from</I></TT> is a pointerto a local <TTCLASS="type">struct sockaddr</TT> that will be filled with the IPaddress and port of the originating machine.<TTCLASS="parameter"><I>fromlen</I></TT> is a pointer to a local <TTCLASS="type">int</TT>that should be initialized to <TTCLASS="type">sizeof(struct sockaddr)</TT>.When the function returns, <TTCLASS="parameter"><I>fromlen</I></TT> will containthe length of the address actually stored in<TTCLASS="parameter"><I>from</I></TT>.</P><P><TTCLASS="function">recvfrom()</TT> returns the number of bytesreceived, or <TTCLASS="constant">-1</TT> on error (with<TTCLASS="parameter"><I>errno</I></TT> set accordingly.)</P><P>Remember, if you <TTCLASS="function">connect()</TT> a datagram socket,you can then simply use <TTCLASS="function">send()</TT> and<TTCLASS="function">recv()</TT> for all your transactions.  The socketitself is still a datagram socket and the packets still use UDP, but thesocket interface will automatically add the destination and sourceinformation for you.</P

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久久久久一二三区| 一区二区三区在线高清| 色综合一个色综合亚洲| 毛片av一区二区三区| 亚洲桃色在线一区| 久久久久久久综合色一本| 欧美日韩一区二区在线观看视频 | 成人午夜大片免费观看| 亚洲福利视频一区| 中文字幕一区不卡| 2欧美一区二区三区在线观看视频| 在线亚洲免费视频| www.亚洲在线| 国产美女娇喘av呻吟久久| 丝袜诱惑亚洲看片| 亚洲精品免费在线观看| 欧美国产日韩在线观看| 久久综合999| 日韩欧美中文字幕一区| 欧美日韩国产综合一区二区| 91婷婷韩国欧美一区二区| 粉嫩蜜臀av国产精品网站| 美日韩一区二区| 首页国产欧美久久| 亚洲国产毛片aaaaa无费看| 亚洲人123区| 国产精品久久毛片| 中文字幕av免费专区久久| 精品va天堂亚洲国产| 日韩欧美成人午夜| 制服丝袜激情欧洲亚洲| 欧美日韩免费观看一区二区三区 | 色噜噜狠狠成人网p站| 成av人片一区二区| 成人免费观看男女羞羞视频| 丰满放荡岳乱妇91ww| 国产盗摄精品一区二区三区在线| 国产真实乱对白精彩久久| 久久国产三级精品| 久草热8精品视频在线观看| 日韩国产在线观看一区| 五月天久久比比资源色| 视频一区视频二区在线观看| 亚洲国产精品久久一线不卡| 午夜久久电影网| 三级精品在线观看| 久久99久久99小草精品免视看| 久久草av在线| 国产精品一品二品| 99视频精品全部免费在线| 91丝袜美腿高跟国产极品老师| 色综合久久综合中文综合网| 日本乱人伦aⅴ精品| 欧美色综合影院| 6080午夜不卡| 久久综合丝袜日本网| 欧美激情中文字幕| 自拍视频在线观看一区二区| 一区二区三区.www| 奇米影视7777精品一区二区| 国产在线视频精品一区| 成人黄色综合网站| 日本黄色一区二区| 91精品国产综合久久国产大片| 欧美tickle裸体挠脚心vk| 亚洲国产精品二十页| 亚洲黄色在线视频| 蜜臀精品一区二区三区在线观看| 国产一本一道久久香蕉| 91在线观看污| 在线91免费看| 国产精品视频第一区| 亚洲成人精品一区二区| 国产真实乱对白精彩久久| 91麻豆产精品久久久久久 | 欧美综合在线视频| 日韩欧美视频在线 | 日韩精品91亚洲二区在线观看| 精品一区二区日韩| 色天天综合色天天久久| 日韩欧美国产精品一区| 国产精品久久久久久久久动漫| 亚洲va欧美va人人爽午夜| 久久爱www久久做| 99re66热这里只有精品3直播| 6080国产精品一区二区| 国产精品久久精品日日| 天天色 色综合| 成人18精品视频| 日韩一级大片在线| 亚洲三级在线免费| 国内精品国产三级国产a久久 | 欧美浪妇xxxx高跟鞋交| 国产女人18毛片水真多成人如厕| 亚洲地区一二三色| 不卡电影一区二区三区| 精品国产一区二区三区久久久蜜月 | 99久久精品免费观看| 日韩免费观看高清完整版在线观看| 国产精品久久99| 久久精品国产99国产| 欧美午夜在线一二页| 国产精品卡一卡二卡三| 极品销魂美女一区二区三区| 精品视频在线免费观看| 国产精品高潮呻吟久久| 精品伊人久久久久7777人| 欧美日韩国产综合一区二区三区| 国产精品家庭影院| 国产一区二区福利| 3d成人h动漫网站入口| 一区二区三区中文字幕电影| 成人自拍视频在线| 日韩免费观看2025年上映的电影| 玉足女爽爽91| 91丨porny丨国产入口| 中文字幕不卡一区| 国产成人在线看| 亚洲精品一区二区精华| 蜜臀av亚洲一区中文字幕| 欧美视频日韩视频| 亚洲在线视频一区| 色域天天综合网| 国产精品久久久久国产精品日日| 国产激情视频一区二区在线观看| 欧美变态tickle挠乳网站| 日韩成人一区二区三区在线观看| 欧美综合在线视频| 亚洲一区二区三区在线| 欧美午夜精品一区二区三区| 亚洲乱码国产乱码精品精可以看 | 日韩中文字幕91| 欧美日韩一区二区欧美激情| 亚洲一区免费在线观看| 欧美性一二三区| 亚洲成人在线观看视频| 欧美日韩一区高清| 日韩一区欧美二区| 欧美日韩aaa| 日韩专区一卡二卡| 日韩一级片在线观看| 精品中文字幕一区二区| 久久亚洲欧美国产精品乐播| 国产一区在线精品| 国产日韩欧美精品综合| 成人激情视频网站| 亚洲伦理在线精品| 欧美三级视频在线播放| 日韩成人一级片| 欧美精品一区二区三区在线| 国产精品亚洲第一区在线暖暖韩国| 国产亚洲精品福利| 波多野结衣中文字幕一区二区三区| 国产精品国产三级国产三级人妇| 一本大道综合伊人精品热热| 性欧美大战久久久久久久久| 欧美一区二区三区啪啪| 国产精品夜夜嗨| 最新国产成人在线观看| 欧美揉bbbbb揉bbbbb| 精品亚洲aⅴ乱码一区二区三区| 国产日韩高清在线| 99国产精品99久久久久久| 亚洲不卡一区二区三区| 精品少妇一区二区三区视频免付费| 国产盗摄一区二区| 一区二区在线观看免费视频播放| 欧美日韩高清一区二区不卡| 精品亚洲成a人| 综合久久久久久久| 欧美一区二区在线不卡| 国产91丝袜在线播放0| 亚洲精品国产a久久久久久| 日韩一区二区在线看片| 成人avav在线| 日av在线不卡| 国产精品理论片在线观看| 91.com在线观看| av影院午夜一区| 日本va欧美va欧美va精品| 日本一区二区免费在线观看视频 | 欧美亚洲动漫精品| 极品少妇一区二区三区精品视频| 日韩一区欧美一区| 日韩亚洲欧美综合| 91麻豆免费观看| 国产毛片一区二区| 午夜激情久久久| 国产精品午夜春色av| 欧美精品丝袜中出| 99国产麻豆精品| 国产在线精品不卡| 亚洲成av人综合在线观看| 中文字幕久久午夜不卡| 日韩情涩欧美日韩视频| 在线欧美一区二区| 成人午夜电影小说| 韩国v欧美v日本v亚洲v| 亚洲国产精品久久人人爱蜜臀| 中文一区二区完整视频在线观看|