• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 96  文章 - 255  trackbacks - 0
            <2008年3月>
            2425262728291
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            E-mail:zbln426@163.com QQ:85132383 長期尋找對戰略游戲感興趣的合作伙伴。

            常用鏈接

            留言簿(21)

            隨筆分類

            隨筆檔案

            SDL相關網站

            我的個人網頁

            我的小游戲

            資源下載

            搜索

            •  

            積分與排名

            • 積分 - 492144
            • 排名 - 38

            最新評論

            閱讀排行榜

            評論排行榜

            作者:龍飛

                    這里的“通訊”加上了引號,是因為實際上所有的socket都有通訊的功能,只是在我們的例子中,之前那個socket只負責listen,而這個socket負責接受信息并echo回去。
             我們現看看這個函數:
            bool TcpServer::isAccept()
            {
                unsigned 
            int clntAddrLen = sizeof(clntAddr);

                
            if ( (communicationSock = accept(listenSock, (sockaddr*)&clntAddr, &clntAddrLen)) < 0 ) {
                    
            return false;
                } 
            else {
                    std::cout 
            << "Client(IP: " << inet_ntoa(clntAddr.sin_addr) << ") connected.\n";
                    
            return true;
                }
            }

            用accept()創建新的socket

                    在我們的例子中,communicationSock實際上是用函數accept()創建的。
            int accept(int socket, struct sockaddr* clientAddress, unsigned int* addressLength);
            在Linux中的實現為:
            /* Await a connection on socket FD.
               When a connection arrives, open a new socket to communicate with it,
               set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
               peer and *ADDR_LEN to the address's actual length, and return the
               new socket's descriptor, or -1 for errors.

               This function is a cancellation point and therefore not marked with
               __THROW.  
            */
            extern int accept (int __fd, __SOCKADDR_ARG __addr,
                       socklen_t 
            *__restrict __addr_len);
            這個函數實際上起著構造socket作用的僅僅只有第一個參數(另外還有一個不在這個函數內表現出來的因素,后面會討論到),后面兩個指針都有副作用,在socket創建后,會將客戶端sockaddr的數據以及結構體的大小傳回。
                    當程序調用accept()的時候,程序有可能就停下來等accept()的結果。這就是我們前一小節說到的block(阻塞)。這如同我們調用std::cin的時候系統會等待輸入直到回車一樣。accept()是一個有可能引起block的函數。請注意我說的是“有可能”,這是因為accept()的block與否實際上決定與第一個參數socket的屬性。這個文件描述符如果是block的,accept()就block,否則就不block。默認情況下,socket的屬性是“可讀可寫”,并且,是阻塞的。所以,我們不修改socket屬性的時候,accept()是阻塞的。

            accept()的另一面connect()

                    accept()只是在server端被動的等待,它所響應的,是client端connect()函數:
            int connect(int socket, struct sockaddr* foreignAddress, unsigned int addressLength);
            雖然我們這里不打算詳細說明這個client端的函數,但是我們可以看出來,這個函數與之前我們介紹的bind()有幾分相似,特別在Linux的實現中:
            /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
               For connectionless socket types, just set the default address to send to
               and the only address from which to accept transmissions.
               Return 0 on success, -1 for errors.

               This function is a cancellation point and therefore not marked with
               __THROW.  
            */
            extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
            connect() 也使用了const的sockaddr,只不過是遠程電腦上的而非bind()的本機。
                    accept()在server端表面上是通過listen socket創建了新的socket,實際上,這種行為是在接受對方客戶機程序中connect()函數的請求后發生的。綜合起看,被創建的新socket實際上包含了listen socket的信息以及客戶端connect()請求中所包含的信息——客戶端的sockaddr地址。

            新socket與sockaddr的關系

                    accept()創建的新socket(我們例子中的communicationSock,這里我們簡單用newSock來帶指)首先包含了listen socket的信息,所以,newSock具有本機sockaddr的信息;其次,因為它響應于client端connect()函數的請求,所以,它還包含了clinet端sockaddr的信息。
                    我們說過,stream流形式的TCP協議實際上是建立起一個“可來可去”的通道。用于listen的通道,遠程機的目標地址是不確定的;但是newSock卻是有指定的本機地址和遠程機地址,所以,這個socket,才是我們真正用于TCP“通訊”的socket。

            inet_ntoa()
            #include <arpa/inet.h>

            /* Convert Internet number in IN to ASCII representation.  The return value
               is a pointer to an internal array containing the string.  
            */
            extern char *inet_ntoa (struct in_addr __in) __THROW;
                    對于這個函數,我們可以作為一種,將IP地址,由in_addr結構轉換為可讀的ASCII形式的固定用法。

            posted on 2008-07-15 13:04 lf426 閱讀(4196) 評論(0)  編輯 收藏 引用 所屬分類: SDL入門教程 、Linux與C++socket 編程入門教程
            久久久久久国产a免费观看不卡| 波多野结衣中文字幕久久 | 一级做a爰片久久毛片免费陪| 久久人人爽人人爽人人片AV东京热| 久久亚洲av无码精品浪潮| 亚洲一级Av无码毛片久久精品| 亚洲中文久久精品无码| 久久成人影院精品777| 亚洲欧美国产精品专区久久 | 亚洲一区二区三区日本久久九| 国产亚洲精久久久久久无码AV| 少妇无套内谢久久久久| 热久久这里只有精品| 久久亚洲sm情趣捆绑调教| 久久国产精品国产自线拍免费| 久久亚洲国产精品123区| 久久99国产乱子伦精品免费| 久久久91人妻无码精品蜜桃HD| 麻豆一区二区99久久久久| 久久人人爽人人爽人人片AV东京热| 久久精品国产第一区二区三区| 亚洲人成无码www久久久| 久久亚洲精品视频| 久久免费的精品国产V∧| 无码八A片人妻少妇久久| 国内精品久久久久影院网站| 久久国产精品77777| 超级碰碰碰碰97久久久久| 久久精品国产黑森林| 国产精品久久久久影院嫩草| 亚洲精品乱码久久久久久| 欧美亚洲国产精品久久久久| 久久久久97国产精华液好用吗| 青青草原综合久久| 99久久精品国产麻豆| 国产精品久久国产精麻豆99网站| 色诱久久久久综合网ywww| 久久婷婷成人综合色综合| 成人久久免费网站| 久久久久亚洲AV成人片| 99国产欧美久久久精品蜜芽 |