锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品美腿一区在线看,亚洲激情在线播放,欧美大片免费久久精品三p http://m.shnenglu.com/keigoliye/category/11701.html涓鍒囬兘鏄焊鑰佽檸zh-cnMon, 13 Sep 2010 20:55:54 GMTMon, 13 Sep 2010 20:55:54 GMT60c & c++ pluginshttp://m.shnenglu.com/keigoliye/archive/2010/09/13/126511.html鏆楀鏁欑埗鏆楀鏁欑埗Mon, 13 Sep 2010 08:45:00 GMThttp://m.shnenglu.com/keigoliye/archive/2010/09/13/126511.htmlhttp://m.shnenglu.com/keigoliye/comments/126511.htmlhttp://m.shnenglu.com/keigoliye/archive/2010/09/13/126511.html#Feedback0http://m.shnenglu.com/keigoliye/comments/commentRss/126511.htmlhttp://m.shnenglu.com/keigoliye/services/trackbacks/126511.html

鍦ˋX3+SP3涓湁涓涓伐鍏稢ode Profiler錛岃繎鏉ュ彂鐜版槸闈炲父濂界敤鐨勫伐鍏鳳紝鍙互媯鏌ヤ唬鐮佽繍琛岀殑杞ㄨ抗錛屼互鍙婁唬鐮佽繍琛岀殑鏃墮棿錛屽浜庤窡韙唬鐮佹槸闈炲父鏈夋晥鐨勩?/p>


C++浠g爜搴﹂噺宸ュ叿-cccc
杞歡搴﹂噺澶氫釜鎸囨爣渚濊禆浜庝唬鐮佽緇熻錛屽姣忓崈琛屼唬鐮佸彂鐜?/span>bug絳夛紝鎵浠ヤ唬鐮佽鏄竴涓熀紜鏁版嵁銆侰CCC搴﹂噺鏁版嵁鍙粨鍚堟祴璇曢闄╁畬鍠勬祴璇曡鍒掋?br>

CodeStatistics
涓涓皬鐨勫懡浠よ宸ュ叿銆傛垜涓昏鐢ㄥ畠鏉ュ仛涓や歡浜嬫儏錛屼竴鑰呮槸浠g爜緇熻鍔熻兘錛涘彟涓鑰呮槸鍒犻櫎絀虹櫧琛屽姛鑳姐傚湪鏌愭鏃跺欙紝鎴戣寰楀浜庢暣鐞嗕唬鐮佸拰浜嗚В浠g爜鎯呭喌榪樻槸鏈変竴瀹氱殑甯姪浣滅敤鐨勩傛槑澶╂垜浼氬皢浠g爜鐨勮緇撴潫絎︽牸寮忓姞涓婄殑錛屽綋鐒訛紝榪樻湁鍒嗘瀽浠g爜鍏冪礌錛屽鏈夊灝戜釜鍑芥暟錛屽灝戜釜綾伙紝涔熷湪鎴戠‘瀹氳娣誨姞鐨勫姛鑳戒箣鍐呫傚綋鐒訛紝鑳芥暣鐞嗕唬鐮佸氨鏇存浜嗐?br>

闈欐佷唬鐮佸垎鏋愬伐鍏稢ppcheck
Cppcheck鏄竴嬈懼紑婧?/span>c++闈欐佷唬鐮佸垎鏋愬伐鍏?/span>,鍦ㄦ嫻嬫簮鐮佹椂鍙牴鎹鍒欏氨鑳芥寲鎺樺嚭鐤戜技緙洪櫡,銆甯紑婧愰」鐩彂鐜扮殑bug鏈?/span>:

http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Found_bugs

鍔熻兘姣旇緝寮哄ぇ,浣跨敤寰堢畝鍗?br>



 



鏆楀鏁欑埗 2010-09-13 16:45 鍙戣〃璇勮
]]>
綰痗璇█瀹炵幇鐨処OCP鏈嶅姟鍣?/title><link>http://m.shnenglu.com/keigoliye/archive/2010/02/24/108341.html</link><dc:creator>鏆楀鏁欑埗</dc:creator><author>鏆楀鏁欑埗</author><pubDate>Wed, 24 Feb 2010 05:34:00 GMT</pubDate><guid>http://m.shnenglu.com/keigoliye/archive/2010/02/24/108341.html</guid><wfw:comment>http://m.shnenglu.com/keigoliye/comments/108341.html</wfw:comment><comments>http://m.shnenglu.com/keigoliye/archive/2010/02/24/108341.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/keigoliye/comments/commentRss/108341.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/keigoliye/services/trackbacks/108341.html</trackback:ping><description><![CDATA[<span style="font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">#include   <winsock2.h>   <br style="line-height: normal; ">//#include   <windows.h>   <br style="line-height: normal; ">#include   <stdio.h>   <br style="line-height: normal; "><br style="line-height: normal; ">#define   PORT   5150   <br style="line-height: normal; ">#define   DATA_BUFSIZE   8192   <br style="line-height: normal; ">    <br style="line-height: normal; ">typedef   struct   <br style="line-height: normal; ">{   <br style="line-height: normal; ">        OVERLAPPED   Overlapped;   <br style="line-height: normal; ">        WSABUF   DataBuf;   <br style="line-height: normal; ">        CHAR   Buffer[DATA_BUFSIZE];   <br style="line-height: normal; ">        DWORD   BytesSEND;   <br style="line-height: normal; ">        DWORD   BytesRECV;   <br style="line-height: normal; ">}   PER_IO_OPERATION_DATA,   *   LPPER_IO_OPERATION_DATA;   <br style="line-height: normal; ">    <br style="line-height: normal; ">    <br style="line-height: normal; ">typedef   struct     <br style="line-height: normal; ">{   <br style="line-height: normal; ">        SOCKET   Socket;   <br style="line-height: normal; ">}   PER_HANDLE_DATA,   *   LPPER_HANDLE_DATA;   <br style="line-height: normal; ">    <br style="line-height: normal; ">    <br style="line-height: normal; ">DWORD   WINAPI   ServerWorkerThread(LPVOID   CompletionPortID);   <br style="line-height: normal; ">    <br style="line-height: normal; ">int main(void)   <br style="line-height: normal; ">{   <br style="line-height: normal; ">        SOCKADDR_IN   InternetAddr;   <br style="line-height: normal; ">        SOCKET   Listen;   <br style="line-height: normal; ">        SOCKET   Accept;   <br style="line-height: normal; ">        HANDLE   CompletionPort;   <br style="line-height: normal; ">        SYSTEM_INFO   SystemInfo;   <br style="line-height: normal; ">        LPPER_HANDLE_DATA   PerHandleData;   <br style="line-height: normal; ">        LPPER_IO_OPERATION_DATA   PerIoData;   <br style="line-height: normal; ">        int   i;   <br style="line-height: normal; ">        DWORD   RecvBytes;   <br style="line-height: normal; ">        DWORD   Flags;   <br style="line-height: normal; ">        DWORD   ThreadID;   <br style="line-height: normal; ">        WSADATA   wsaData;   <br style="line-height: normal; ">        DWORD   Ret;   <br style="line-height: normal; ">    <br style="line-height: normal; ">        if   ((Ret   =   WSAStartup(0x0202,   &wsaData))   !=   0)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">              printf("WSAStartup澶辮觸浜嗭紝閿欒淇℃伅濡備笅:   %d\n",   Ret);   <br style="line-height: normal; ">              return;   <br style="line-height: normal; ">        }   <br style="line-height: normal; ">    <br style="line-height: normal; ">        // 璁劇疆涓涓狪/O瀹屾垚绔彛.   <br style="line-height: normal; ">    <br style="line-height: normal; ">        if   ((CompletionPort   =   CreateIoCompletionPort(INVALID_HANDLE_VALUE,   NULL,   0,   0))   ==   NULL)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">              printf(   "CreateIoCompletionPort 澶辮觸浜嗭紝閿欒淇℃伅濡備笅:   %d\n",   GetLastError());   <br style="line-height: normal; ">              return;   <br style="line-height: normal; ">        }   <br style="line-height: normal; ">    <br style="line-height: normal; ">        // 嫻嬭瘯緋葷粺涓湁澶氬皯cpu澶勭悊鍣?nbsp;<br style="line-height: normal; ">    <br style="line-height: normal; ">        GetSystemInfo(&SystemInfo);   <br style="line-height: normal; ">    <br style="line-height: normal; ">        //   鍩轟簬緋葷粺鍙敤鐨勫鐞嗗櫒鍒涘緩宸ヤ綔綰跨▼錛屼負姣忎釜澶勭悊鍣ㄥ垱寤鴻繛涓嚎紼?nbsp;  <br style="line-height: normal; ">    <br style="line-height: normal; ">        for(i   =   0;   i   <   SystemInfo.dwNumberOfProcessors   *   2;   i++)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">              HANDLE   ThreadHandle;   <br style="line-height: normal; ">    <br style="line-height: normal; ">              // 鍒涘緩涓涓湇鍔$綰跨▼騫朵笖浼犻掍竴涓畬鎴愮鍙g粰榪欎釜綰跨▼.   <br style="line-height: normal; ">    <br style="line-height: normal; ">              if   ((ThreadHandle   =   CreateThread(NULL,   0,   ServerWorkerThread,   CompletionPort,   <br style="line-height: normal; ">                    0,   &ThreadID))   ==   NULL)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    printf("CreateThread()鍙戠敓浜嗗涓嬮敊璇細 %d\n",   GetLastError());   <br style="line-height: normal; ">                    return;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">              else <br style="line-height: normal; ">              {printf("鍒涘緩浜嗕竴涓畬鎴愮鍙?\n");<br style="line-height: normal; ">              }<br style="line-height: normal; ">              //   鍏抽棴 thread鍙ユ焺 <br style="line-height: normal; ">              CloseHandle(ThreadHandle);   <br style="line-height: normal; ">        }   <br style="line-height: normal; ">    <br style="line-height: normal; ">        //   鍒涘緩涓涓洃鍚鎺ュ瓧 <br style="line-height: normal; ">    <br style="line-height: normal; ">        if   ((Listen   =WSASocket(AF_INET,   SOCK_STREAM,   0,   NULL,0,WSA_FLAG_OVERLAPPED))   ==   INVALID_SOCKET)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">              printf("WSASocket() 鍙戠敓浜嗗涓嬮敊璇細 %d\n",   WSAGetLastError());   <br style="line-height: normal; ">              return;   <br style="line-height: normal; ">        }<br style="line-height: normal; ">        else     <br style="line-height: normal; ">        {printf("鍒涘緩鐩戝惉濂楁帴瀛楁垚鍔焅n");}<br style="line-height: normal; ">        InternetAddr.sin_family   =   AF_INET;   <br style="line-height: normal; ">        InternetAddr.sin_addr.s_addr   =   htonl(INADDR_ANY);   <br style="line-height: normal; ">        InternetAddr.sin_port   =   htons(PORT);   <br style="line-height: normal; ">    <br style="line-height: normal; ">        if   (bind(Listen,   (PSOCKADDR)   &InternetAddr,   sizeof(InternetAddr))   ==   SOCKET_ERROR)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">              printf("bind()绔彛鎴朓P鏃跺彂鐢熶簡濡備笅閿欒錛?%d\n",   WSAGetLastError());   <br style="line-height: normal; ">              return;   <br style="line-height: normal; ">        }   <br style="line-height: normal; ">        else<br style="line-height: normal; ">        {printf("緇戝畾绔彛%d鎴愬姛\n",PORT);} <br style="line-height: normal; ">        // 鍑嗗socket 鐢ㄦ潵鐩戝惉   <br style="line-height: normal; ">    <br style="line-height: normal; ">        if   (listen(Listen,   5)   ==   SOCKET_ERROR)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">              printf("listen() 鍙戠敓浜嗗涓嬮敊璇?nbsp;  %d\n",   WSAGetLastError());   <br style="line-height: normal; ">              return;   <br style="line-height: normal; ">        }   <br style="line-height: normal; ">         else<br style="line-height: normal; ">        {printf("棰勫鐞嗘垚鍔燂紝寮濮嬪湪绔彛 %d 澶勭洃鍚?..\n",PORT);} <br style="line-height: normal; ">        //鎺ュ彈榪炴帴騫朵笖浜ょ粰瀹屾垚绔彛澶勭悊 <br style="line-height: normal; ">    <br style="line-height: normal; ">        while(TRUE)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">              if   ((Accept   =   WSAAccept(Listen,   NULL,   NULL,   NULL,   0))   ==   SOCKET_ERROR)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    printf("WSAAccept()   鍙戠敓浜嗗涓嬮敊璇細   %d\n",   WSAGetLastError());   <br style="line-height: normal; ">                    return;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">    <br style="line-height: normal; ">              // 鍒涘緩涓涓鎺ュ瓧淇℃伅緇撴瀯浣撳幓鑱旂郴璧鋒潵socket   <br style="line-height: normal; ">              if   ((PerHandleData   =   (LPPER_HANDLE_DATA)   GlobalAlloc(GPTR,     <br style="line-height: normal; ">                    sizeof(PER_HANDLE_DATA)))   ==   NULL)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    printf("GlobalAlloc()   鍙戠敓浜嗗涓嬮敊璇細   %d\n",   GetLastError());   <br style="line-height: normal; ">                    return;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">    <br style="line-height: normal; ">              // 灝嗘帴鍙楀埌鐨勫鎺ュ瓧涓庡師濮嬬殑瀹屾垚绔彛鑱旂郴璧鋒潵.   <br style="line-height: normal; ">    <br style="line-height: normal; ">              printf("鍙風爜涓?nbsp;  %d   鐨剆ocket榪炴帴涓婁簡\n",   Accept);   <br style="line-height: normal; ">              PerHandleData->Socket   =   Accept;   <br style="line-height: normal; ">    <br style="line-height: normal; ">              if   (CreateIoCompletionPort((HANDLE)   Accept,   CompletionPort,   (DWORD)   PerHandleData,   <br style="line-height: normal; ">                    0)   ==   NULL)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    printf("CreateIoCompletionPort   鍙戠敓浜嗗涓嬮敊璇細   %d\n",   GetLastError());   <br style="line-height: normal; ">                    return;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">    <br style="line-height: normal; ">              //   鍒涘緩姣忎竴涓狪/O 濂楁帴瀛椾俊鎭粨鏋勪綋鍘誨拰涓嬮潰琚皟鐢ㄧ殑 to   associate   with   the     <br style="line-height: normal; ">              //   WSARecv 榪炴帴.   <br style="line-height: normal; ">    <br style="line-height: normal; ">              if   ((PerIoData   =   (LPPER_IO_OPERATION_DATA)   GlobalAlloc(GPTR,                     sizeof(PER_IO_OPERATION_DATA)))   ==   NULL)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    printf("GlobalAlloc() 鍙戠敓浜嗗涓嬮敊璇細 %d\n",   GetLastError());   <br style="line-height: normal; ">                    return;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">              else{printf("鎺ユ敹浜嗕竴涓繛鎺n");} <br style="line-height: normal; ">              ZeroMemory(&(PerIoData->Overlapped),   sizeof(OVERLAPPED));   <br style="line-height: normal; ">              PerIoData->BytesSEND   =   0;   <br style="line-height: normal; ">              PerIoData->BytesRECV   =   0;   <br style="line-height: normal; ">              PerIoData->DataBuf.len   =   DATA_BUFSIZE;   <br style="line-height: normal; ">              PerIoData->DataBuf.buf   =   PerIoData->Buffer;   <br style="line-height: normal; ">    <br style="line-height: normal; ">              Flags   =   0;   <br style="line-height: normal; ">              if   (WSARecv(Accept,   &(PerIoData->DataBuf),   1,   &RecvBytes,   &Flags,   <br style="line-height: normal; ">                    &(PerIoData->Overlapped),   NULL)   ==   SOCKET_ERROR)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    if   (WSAGetLastError()   !=   ERROR_IO_PENDING)   <br style="line-height: normal; ">                    {   <br style="line-height: normal; ">                          printf("WSARecv() 鍙戠敓浜嗗涓嬮敊璇細 %d\n",   WSAGetLastError());   <br style="line-height: normal; ">                          return;   <br style="line-height: normal; ">                    }   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">        }   <br style="line-height: normal; ">}   <br style="line-height: normal; ">    <br style="line-height: normal; ">DWORD   WINAPI   ServerWorkerThread(LPVOID   CompletionPortID)   <br style="line-height: normal; ">{   <br style="line-height: normal; ">        HANDLE   CompletionPort   =   (HANDLE)   CompletionPortID;   <br style="line-height: normal; ">        DWORD   BytesTransferred;   <br style="line-height: normal; ">        LPOVERLAPPED   Overlapped;   <br style="line-height: normal; ">        LPPER_HANDLE_DATA   PerHandleData;   <br style="line-height: normal; ">        LPPER_IO_OPERATION_DATA   PerIoData;   <br style="line-height: normal; ">        DWORD   SendBytes,   RecvBytes;   <br style="line-height: normal; ">        DWORD   Flags;   <br style="line-height: normal; ">          <br style="line-height: normal; ">        while(TRUE)   <br style="line-height: normal; ">        {   <br style="line-height: normal; ">    <br style="line-height: normal; ">              if   (GetQueuedCompletionStatus(CompletionPort,   &BytesTransferred,   <br style="line-height: normal; ">                    (LPDWORD)&PerHandleData,   (LPOVERLAPPED   *)   &PerIoData,   INFINITE)   ==   0)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    printf("GetQueuedCompletionStatus   鍙戠敓浜嗗涓嬮敊璇細 %d\n",   GetLastError());   <br style="line-height: normal; ">                    return   0;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">    <br style="line-height: normal; ">              //棣栧厛媯鏌ヤ竴涓嬪幓濂楁帴瀛楃湅鏄惁鍦ㄤ笂鍙戠敓浜嗛敊璇茍涓斿鏋滃彂鐢熶簡閿欒灝卞叧闂鎺?br style="line-height: normal; ">              //瀛楀茍涓旀竻闄や笌濂楁帴瀛楄繛鎺ョ殑 SOCKET_INFORMATION緇撴瀯淇℃伅浣?nbsp;<br style="line-height: normal; ">              if   (BytesTransferred   ==   0)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    printf("姝e湪鍏抽棴socket   %d\n",   PerHandleData->Socket);   <br style="line-height: normal; ">    <br style="line-height: normal; ">                    if   (closesocket(PerHandleData->Socket)   ==   SOCKET_ERROR)   <br style="line-height: normal; ">                    {   <br style="line-height: normal; ">                          printf("closesocket()   鍙戠敓浜嗗涓嬮敊璇細 %d\n",   WSAGetLastError());   <br style="line-height: normal; ">                          return   0;   <br style="line-height: normal; ">                    }   <br style="line-height: normal; ">    <br style="line-height: normal; ">                    GlobalFree(PerHandleData);   <br style="line-height: normal; ">                    GlobalFree(PerIoData);   <br style="line-height: normal; ">                    continue;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">    //媯鏌ュ鏋?BytesRECV瀛楁絳変簬0錛岃繖灝辨剰鍛崇潃涓涓?WSARecv璋冪敤鍒氬垰瀹屾垚浜嗘墍浠ヤ粠瀹屾垚鐨刉SARecv()璋冪敤涓?br style="line-height: normal; ">    //鐢˙ytesTransferred鍊兼洿鏂?BytesRECV瀛楁 <br style="line-height: normal; ">              if   (PerIoData->BytesRECV   ==   0)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    PerIoData->BytesRECV   =   BytesTransferred;   <br style="line-height: normal; ">                    PerIoData->BytesSEND   =   0;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">              else   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    PerIoData->BytesSEND   +=   BytesTransferred;   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">    <br style="line-height: normal; ">              if   (PerIoData->BytesRECV   >   PerIoData->BytesSEND)   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">    //鍙戝竷鍙﹀涓涓?WSASend()璇鋒眰<br style="line-height: normal; ">    //鏃㈢劧WSASend()涓嶆槸 gauranteed鍘誨彂閫佹墍鏈夊瓧鑺傜殑璇鋒眰<br style="line-height: normal; ">    //緇х畫璋冪敤 WSASend()鍙戦佺洿鍒版墍鏈夋敹鍒扮殑瀛楄妭琚彂閫?nbsp;<br style="line-height: normal; ">                    <br style="line-height: normal; ">                    ZeroMemory(&(PerIoData->Overlapped),   sizeof(OVERLAPPED));   <br style="line-height: normal; ">    <br style="line-height: normal; ">                    PerIoData->DataBuf.buf   =   PerIoData->Buffer   +   PerIoData->BytesSEND;   <br style="line-height: normal; ">                    PerIoData->DataBuf.len   =   PerIoData->BytesRECV   -   PerIoData->BytesSEND;   <br style="line-height: normal; ">    <br style="line-height: normal; ">                    if   (WSASend(PerHandleData->Socket,   &(PerIoData->DataBuf),   1,   &SendBytes,   0,   <br style="line-height: normal; ">                          &(PerIoData->Overlapped),   NULL)   ==   SOCKET_ERROR)   <br style="line-height: normal; ">                    {   <br style="line-height: normal; ">                          if   (WSAGetLastError()   !=   ERROR_IO_PENDING)   <br style="line-height: normal; ">                          {   <br style="line-height: normal; ">                                printf("WSASend() 鍙戠敓浜嗗涓嬮敊璇細   %d\n",   WSAGetLastError());   <br style="line-height: normal; ">                                return   0;   <br style="line-height: normal; ">                          }   <br style="line-height: normal; ">                    }   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">              else   <br style="line-height: normal; ">              {   <br style="line-height: normal; ">                    PerIoData->BytesRECV   =   0;   <br style="line-height: normal; ">    //鐜板湪娌℃湁鏇村鐨勫瓧鑺傚彂閫佽繃鍘葷敤鏉ost鍙﹀涓涓猈SARecv()璇鋒眰 <br style="line-height: normal; ">                    <br style="line-height: normal; ">                    Flags   =   0;   <br style="line-height: normal; ">                    ZeroMemory(&(PerIoData->Overlapped),   sizeof(OVERLAPPED));   <br style="line-height: normal; ">    <br style="line-height: normal; ">                    PerIoData->DataBuf.len   =   DATA_BUFSIZE;   <br style="line-height: normal; ">                    PerIoData->DataBuf.buf   =   PerIoData->Buffer;   <br style="line-height: normal; ">    <br style="line-height: normal; ">                    if   (WSARecv(PerHandleData->Socket,   &(PerIoData->DataBuf),   1,   &RecvBytes,   &Flags,   <br style="line-height: normal; ">                          &(PerIoData->Overlapped),   NULL)   ==   SOCKET_ERROR)   <br style="line-height: normal; ">                    {   <br style="line-height: normal; ">                          if   (WSAGetLastError()   !=   ERROR_IO_PENDING)   <br style="line-height: normal; ">                          {   <br style="line-height: normal; ">                                printf("WSARecv() 鍙戠敓浜嗗涓嬮敊璇細   %d\n",   WSAGetLastError());   <br style="line-height: normal; ">                                return   0;   <br style="line-height: normal; ">                          }   <br style="line-height: normal; ">                    }   <br style="line-height: normal; ">              }   <br style="line-height: normal; ">        }   <br style="line-height: normal; ">}  </span> <img src ="http://m.shnenglu.com/keigoliye/aggbug/108341.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/keigoliye/" target="_blank">鏆楀鏁欑埗</a> 2010-02-24 13:34 <a href="http://m.shnenglu.com/keigoliye/archive/2010/02/24/108341.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Linux鐜涓嬬殑Socket緙栫▼http://m.shnenglu.com/keigoliye/archive/2010/02/24/108340.html鏆楀鏁欑埗鏆楀鏁欑埗Wed, 24 Feb 2010 05:32:00 GMThttp://m.shnenglu.com/keigoliye/archive/2010/02/24/108340.htmlhttp://m.shnenglu.com/keigoliye/comments/108340.htmlhttp://m.shnenglu.com/keigoliye/archive/2010/02/24/108340.html#Feedback0http://m.shnenglu.com/keigoliye/comments/commentRss/108340.htmlhttp://m.shnenglu.com/keigoliye/services/trackbacks/108340.html浠涔堟槸Socket 
銆銆Socket鎺ュ彛鏄疶CP/IP緗戠粶鐨凙PI錛孲ocket鎺ュ彛瀹氫箟浜嗚澶氬嚱鏁版垨渚嬬▼錛岀▼搴忓憳鍙互鐢ㄥ畠浠潵寮鍙慣CP/IP緗戠粶涓婄殑搴旂敤紼嬪簭銆傝瀛nternet涓婄殑TCP/IP緗戠粶緙栫▼錛屽繀欏葷悊瑙ocket鎺ュ彛銆?nbsp;
銆銆Socket鎺ュ彛璁捐鑰呮渶鍏堟槸灝嗘帴鍙f斁鍦║nix鎿嶄綔緋葷粺閲岄潰鐨勩傚鏋滀簡瑙nix緋葷粺鐨勮緭鍏ュ拰杈撳嚭鐨勮瘽錛屽氨寰堝鏄撲簡瑙ocket浜嗐傜綉緇滅殑Socket鏁版嵁浼犺緭鏄竴縐嶇壒孌婄殑I/O錛孲ocket涔熸槸涓縐嶆枃浠舵弿榪扮銆係ocket涔熷叿鏈変竴涓被浼間簬鎵撳紑鏂囦歡鐨勫嚱鏁拌皟鐢⊿ocket()錛岃鍑芥暟榪斿洖涓涓暣鍨嬬殑Socket鎻忚堪絎︼紝闅忓悗鐨勮繛鎺ュ緩绔嬨佹暟鎹紶杈撶瓑鎿嶄綔閮芥槸閫氳繃璇ocket瀹炵幇鐨勩傚父鐢ㄧ殑Socket綾誨瀷鏈変袱縐嶏細嫻佸紡Socket錛圫OCK_STREAM錛夊拰鏁版嵁鎶ュ紡Socket錛圫OCK_DGRAM錛夈傛祦寮忔槸涓縐嶉潰鍚戣繛鎺ョ殑Socket錛岄拡瀵逛簬闈㈠悜榪炴帴鐨凾CP鏈嶅姟搴旂敤錛涙暟鎹姤寮廠ocket鏄竴縐嶆棤榪炴帴鐨凷ocket錛屽搴斾簬鏃犺繛鎺ョ殑UDP鏈嶅姟搴旂敤銆?nbsp;

Socket寤虹珛 
銆銆涓轟簡寤虹珛Socket錛岀▼搴忓彲浠ヨ皟鐢⊿ocket鍑芥暟錛岃鍑芥暟榪斿洖涓涓被浼間簬鏂囦歡鎻忚堪絎︾殑鍙ユ焺銆俿ocket鍑芥暟鍘熷瀷涓猴細 
銆銆int socket(int domain, int type, int protocol); 
銆銆domain鎸囨槑鎵浣跨敤鐨勫崗璁棌錛岄氬父涓篜F_INET錛岃〃紺轟簰鑱旂綉鍗忚鏃忥紙TCP/IP鍗忚鏃忥級錛泃ype鍙傛暟鎸囧畾socket鐨勭被鍨嬶細SOCK_STREAM 鎴朣OCK_DGRAM錛孲ocket鎺ュ彛榪樺畾涔変簡鍘熷Socket錛圫OCK_RAW錛夛紝鍏佽紼嬪簭浣跨敤浣庡眰鍗忚錛沺rotocol閫氬父璧嬪?0"銆係ocket()璋冪敤榪斿洖涓涓暣鍨媠ocket鎻忚堪絎︼紝浣犲彲浠ュ湪鍚庨潰鐨勮皟鐢ㄤ嬌鐢ㄥ畠銆?nbsp;
銆銆Socket鎻忚堪絎︽槸涓涓寚鍚戝唴閮ㄦ暟鎹粨鏋勭殑鎸囬拡錛屽畠鎸囧悜鎻忚堪絎﹁〃鍏ュ彛銆傝皟鐢⊿ocket鍑芥暟鏃訛紝socket鎵ц浣撳皢寤虹珛涓涓猄ocket錛屽疄闄呬笂"寤虹珛涓涓猄ocket"鎰忓懗鐫涓轟竴涓猄ocket鏁版嵁緇撴瀯鍒嗛厤瀛樺偍絀洪棿銆係ocket鎵ц浣撲負浣犵鐞嗘弿榪扮琛ㄣ?nbsp;
銆銆涓や釜緗戠粶紼嬪簭涔嬮棿鐨勪竴涓綉緇滆繛鎺ュ寘鎷簲縐嶄俊鎭細閫氫俊鍗忚銆佹湰鍦板崗璁湴鍧銆佹湰鍦頒富鏈虹鍙c佽繙绔富鏈哄湴鍧鍜岃繙绔崗璁鍙c係ocket鏁版嵁緇撴瀯涓寘鍚繖浜旂淇℃伅銆?nbsp;

Socket閰嶇疆 
銆銆閫氳繃socket璋冪敤榪斿洖涓涓猻ocket鎻忚堪絎﹀悗錛屽湪浣跨敤socket榪涜緗戠粶浼犺緭浠ュ墠錛屽繀欏婚厤緗socket銆傞潰鍚戣繛鎺ョ殑socket瀹㈡埛绔氳繃璋冪敤Connect鍑芥暟鍦╯ocket鏁版嵁緇撴瀯涓繚瀛樻湰鍦板拰榪滅淇℃伅銆傛棤榪炴帴socket鐨勫鎴風鍜屾湇鍔$浠ュ強闈㈠悜榪炴帴socket鐨勬湇鍔$閫氳繃璋冪敤bind鍑芥暟鏉ラ厤緗湰鍦頒俊鎭?nbsp;
Bind鍑芥暟灝唖ocket涓庢湰鏈轟笂鐨勪竴涓鍙g浉鍏寵仈錛岄殢鍚庝綘灝卞彲浠ュ湪璇ョ鍙g洃鍚湇鍔¤姹傘侭ind鍑芥暟鍘熷瀷涓猴細 
銆銆int bind(int sockfd,struct sockaddr *my_addr, int addrlen); 
銆銆Sockfd鏄皟鐢╯ocket鍑芥暟榪斿洖鐨剆ocket鎻忚堪絎?my_addr鏄竴涓寚鍚戝寘鍚湁鏈満IP鍦板潃鍙婄鍙e彿絳変俊鎭殑sockaddr綾誨瀷鐨勬寚閽堬紱addrlen甯歌璁劇疆涓簊izeof(struct sockaddr)銆?nbsp;
銆銆struct sockaddr緇撴瀯綾誨瀷鏄敤鏉ヤ繚瀛榮ocket淇℃伅鐨勶細 
銆銆struct sockaddr { 
銆銆 unsigned short sa_family; /* 鍦板潃鏃忥紝 AF_xxx */ 
char sa_data[14]; /* 14 瀛楄妭鐨勫崗璁湴鍧 */ 
}; 
銆銆sa_family涓鑸負AF_INET錛屼唬琛↖nternet錛圱CP/IP錛夊湴鍧鏃忥紱sa_data鍒欏寘鍚socket鐨処P鍦板潃鍜岀鍙e彿銆?nbsp;
銆銆鍙﹀榪樻湁涓縐嶇粨鏋勭被鍨嬶細 
銆銆struct sockaddr_in { 
銆銆 short int sin_family; /* 鍦板潃鏃?*/ 
銆銆 unsigned short int sin_port; /* 绔彛鍙?*/ 
銆銆 struct in_addr sin_addr; /* IP鍦板潃 */ 
銆銆 unsigned char sin_zero[8]; /* 濉厖0 浠ヤ繚鎸佷笌struct sockaddr鍚屾牱澶у皬 */ 
銆銆}; 
銆銆榪欎釜緇撴瀯鏇存柟渚夸嬌鐢ㄣ俿in_zero鐢ㄦ潵灝唖ockaddr_in緇撴瀯濉厖鍒頒笌struct sockaddr鍚屾牱鐨勯暱搴︼紝鍙互鐢╞zero()鎴杕emset()鍑芥暟灝嗗叾緗負闆躲傛寚鍚憇ockaddr_in 鐨勬寚閽堝拰鎸囧悜sockaddr鐨勬寚閽堝彲浠ョ浉浜掕漿鎹紝榪欐剰鍛崇潃濡傛灉涓涓嚱鏁版墍闇鍙傛暟綾誨瀷鏄痵ockaddr鏃訛紝浣犲彲浠ュ湪鍑芥暟璋冪敤鐨勬椂鍊欏皢涓涓寚鍚憇ockaddr_in鐨勬寚閽堣漿鎹負鎸囧悜sockaddr鐨勬寚閽堬紱鎴栬呯浉鍙嶃?nbsp;
銆銆浣跨敤bind鍑芥暟鏃訛紝鍙互鐢ㄤ笅闈㈢殑璧嬪煎疄鐜拌嚜鍔ㄨ幏寰楁湰鏈篒P鍦板潃鍜岄殢鏈鴻幏鍙栦竴涓病鏈夎鍗犵敤鐨勭鍙e彿錛?nbsp;
銆銆my_addr.sin_port = 0; /* 緋葷粺闅忔満閫夋嫨涓涓湭琚嬌鐢ㄧ殑绔彛鍙?*/ 
銆銆my_addr.sin_addr.s_addr = INADDR_ANY; /* 濉叆鏈満IP鍦板潃 */ 
閫氳繃灝唌y_addr.sin_port緗負0錛屽嚱鏁頒細鑷姩涓轟綘閫夋嫨涓涓湭鍗犵敤鐨勭鍙f潵浣跨敤銆傚悓鏍鳳紝閫氳繃灝唌y_addr.sin_addr.s_addr緗負INADDR_ANY錛岀郴緇熶細鑷姩濉叆鏈満IP鍦板潃銆?nbsp;
娉ㄦ剰鍦ㄤ嬌鐢╞ind鍑芥暟鏄渶瑕佸皢sin_port鍜宻in_addr杞崲鎴愪負緗戠粶瀛楄妭浼樺厛欏哄簭錛涜宻in_addr鍒欎笉闇瑕佽漿鎹€?nbsp;
銆銆璁$畻鏈烘暟鎹瓨鍌ㄦ湁涓ょ瀛楄妭浼樺厛欏哄簭錛氶珮浣嶅瓧鑺備紭鍏堝拰浣庝綅瀛楄妭浼樺厛銆侷nternet涓婃暟鎹互楂樹綅瀛楄妭浼樺厛欏哄簭鍦ㄧ綉緇滀笂浼犺緭錛屾墍浠ュ浜庡湪鍐呴儴鏄互浣庝綅瀛楄妭浼樺厛鏂瑰紡瀛樺偍鏁版嵁鐨勬満鍣紝鍦↖nternet涓婁紶杈撴暟鎹椂灝遍渶瑕佽繘琛岃漿鎹紝鍚﹀垯灝變細鍑虹幇鏁版嵁涓嶄竴鑷淬?nbsp;
銆銆涓嬮潰鏄嚑涓瓧鑺傞『搴忚漿鎹㈠嚱鏁幫細 
·htonl()錛氭妸32浣嶅間粠涓繪満瀛楄妭搴忚漿鎹㈡垚緗戠粶瀛楄妭搴?nbsp;
·htons()錛氭妸16浣嶅間粠涓繪満瀛楄妭搴忚漿鎹㈡垚緗戠粶瀛楄妭搴?nbsp;
·ntohl()錛氭妸32浣嶅間粠緗戠粶瀛楄妭搴忚漿鎹㈡垚涓繪満瀛楄妭搴?nbsp;
·ntohs()錛氭妸16浣嶅間粠緗戠粶瀛楄妭搴忚漿鎹㈡垚涓繪満瀛楄妭搴?nbsp;
銆銆Bind()鍑芥暟鍦ㄦ垚鍔熻璋冪敤鏃惰繑鍥?錛涘嚭鐜伴敊璇椂榪斿洖"-1"騫跺皢errno緗負鐩稿簲鐨勯敊璇彿銆傞渶瑕佹敞鎰忕殑鏄紝鍦ㄨ皟鐢╞ind鍑芥暟鏃朵竴鑸笉瑕佸皢绔彛鍙風疆涓哄皬浜?024鐨勫鹼紝鍥犱負1鍒?024鏄繚鐣欑鍙e彿錛屼綘鍙互閫夋嫨澶т簬1024涓殑浠諱綍涓涓病鏈夎鍗犵敤鐨勭鍙e彿銆?nbsp;

榪炴帴寤虹珛 
銆銆闈㈠悜榪炴帴鐨勫鎴風▼搴忎嬌鐢–onnect鍑芥暟鏉ラ厤緗畇ocket騫朵笌榪滅鏈嶅姟鍣ㄥ緩绔嬩竴涓猅CP榪炴帴錛屽叾鍑芥暟鍘熷瀷涓猴細 
銆銆int connect(int sockfd, struct sockaddr *serv_addr,int addrlen); 
Sockfd鏄痵ocket鍑芥暟榪斿洖鐨剆ocket鎻忚堪絎︼紱serv_addr鏄寘鍚繙绔富鏈篒P鍦板潃鍜岀鍙e彿鐨勬寚閽堬紱addrlen鏄繙绔湴璐ㄧ粨鏋勭殑闀垮害銆侰onnect鍑芥暟鍦ㄥ嚭鐜伴敊璇椂榪斿洖-1錛屽茍涓旇緗甧rrno涓虹浉搴旂殑閿欒鐮併傝繘琛屽鎴風紼嬪簭璁捐鏃犻』璋冪敤bind()錛屽洜涓鴻繖縐嶆儏鍐典笅鍙渶鐭ラ亾鐩殑鏈哄櫒鐨処P鍦板潃錛岃屽鎴烽氳繃鍝釜绔彛涓庢湇鍔″櫒寤虹珛榪炴帴騫朵笉闇瑕佸叧蹇冿紝socket鎵ц浣撲負浣犵殑紼嬪簭鑷姩閫夋嫨涓涓湭琚崰鐢ㄧ殑绔彛錛屽茍閫氱煡浣犵殑紼嬪簭鏁版嵁浠涔堟椂鍊欏埌鎵撴柇鍙c?nbsp;
銆銆Connect鍑芥暟鍚姩鍜岃繙绔富鏈虹殑鐩存帴榪炴帴銆傚彧鏈夐潰鍚戣繛鎺ョ殑瀹㈡埛紼嬪簭浣跨敤socket鏃舵墠闇瑕佸皢姝ocket涓庤繙绔富鏈虹浉榪炪傛棤榪炴帴鍗忚浠庝笉寤虹珛鐩存帴榪炴帴銆傞潰鍚戣繛鎺ョ殑鏈嶅姟鍣ㄤ篃浠庝笉鍚姩涓涓繛鎺ワ紝瀹冨彧鏄鍔ㄧ殑鍦ㄥ崗璁鍙g洃鍚鎴風殑璇鋒眰銆?nbsp;
銆銆Listen鍑芥暟浣縮ocket澶勪簬琚姩鐨勭洃鍚ā寮忥紝騫朵負璇ocket寤虹珛涓涓緭鍏ユ暟鎹槦鍒楋紝灝嗗埌杈劇殑鏈嶅姟璇鋒眰淇濆瓨鍦ㄦ闃熷垪涓紝鐩村埌紼嬪簭澶勭悊瀹冧滑銆?nbsp;
銆銆int listen(int sockfd錛?int backlog); 
Sockfd鏄疭ocket緋葷粺璋冪敤榪斿洖鐨剆ocket 鎻忚堪絎︼紱backlog鎸囧畾鍦ㄨ姹傞槦鍒椾腑鍏佽鐨勬渶澶ц姹傛暟錛岃繘鍏ョ殑榪炴帴璇鋒眰灝嗗湪闃熷垪涓瓑寰卆ccept()瀹冧滑錛堝弬鑰冧笅鏂囷級銆侭acklog瀵歸槦鍒椾腑絳夊緟鏈嶅姟鐨勮姹傜殑鏁扮洰榪涜浜嗛檺鍒訛紝澶у鏁扮郴緇熺己鐪佸間負20銆傚鏋滀竴涓湇鍔¤姹傚埌鏉ユ椂錛岃緭鍏ラ槦鍒楀凡婊★紝璇ocket灝嗘嫆緇濊繛鎺ヨ姹傦紝瀹㈡埛灝嗘敹鍒頒竴涓嚭閿欎俊鎭?nbsp;
褰撳嚭鐜伴敊璇椂listen鍑芥暟榪斿洖-1錛屽茍緗浉搴旂殑errno閿欒鐮併?nbsp;
銆銆accept()鍑芥暟璁╂湇鍔″櫒鎺ユ敹瀹㈡埛鐨勮繛鎺ヨ姹傘傚湪寤虹珛濂借緭鍏ラ槦鍒楀悗錛屾湇鍔″櫒灝辮皟鐢╝ccept鍑芥暟錛岀劧鍚庣潯鐪犲茍絳夊緟瀹㈡埛鐨勮繛鎺ヨ姹傘?nbsp;
銆銆int accept(int sockfd, void *addr, int *addrlen); 
銆銆sockfd鏄鐩戝惉鐨剆ocket鎻忚堪絎︼紝addr閫氬父鏄竴涓寚鍚憇ockaddr_in鍙橀噺鐨勬寚閽堬紝璇ュ彉閲忕敤鏉ュ瓨鏀炬彁鍑鴻繛鎺ヨ姹傛湇鍔$殑涓繪満鐨勪俊鎭紙鏌愬彴涓繪満浠庢煇涓鍙e彂鍑鴻璇鋒眰錛夛紱addrten閫氬父涓轟竴涓寚鍚戝間負sizeof(struct sockaddr_in)鐨勬暣鍨嬫寚閽堝彉閲忋傚嚭鐜伴敊璇椂accept鍑芥暟榪斿洖-1騫剁疆鐩稿簲鐨別rrno鍊箋?nbsp;
銆銆棣栧厛錛屽綋accept鍑芥暟鐩戣鐨剆ocket鏀跺埌榪炴帴璇鋒眰鏃訛紝socket鎵ц浣撳皢寤虹珛涓涓柊鐨剆ocket錛屾墽琛屼綋灝嗚繖涓柊socket鍜岃姹傝繛鎺ヨ繘紼嬬殑鍦板潃鑱旂郴璧鋒潵錛屾敹鍒版湇鍔¤姹傜殑鍒濆socket浠嶅彲浠ョ戶緇湪浠ュ墠鐨?socket涓婄洃鍚紝鍚屾椂鍙互鍦ㄦ柊鐨剆ocket鎻忚堪絎︿笂榪涜鏁版嵁浼犺緭鎿嶄綔銆?nbsp;

鏁版嵁浼犺緭 
銆銆Send()鍜宺ecv()榪欎袱涓嚱鏁扮敤浜庨潰鍚戣繛鎺ョ殑socket涓婅繘琛屾暟鎹紶杈撱?nbsp;
銆銆Send()鍑芥暟鍘熷瀷涓猴細 
銆銆int send(int sockfd, const void *msg, int len, int flags); 
Sockfd鏄綘鎯崇敤鏉ヤ紶杈撴暟鎹殑socket鎻忚堪絎︼紱msg鏄竴涓寚鍚戣鍙戦佹暟鎹殑鎸囬拡錛汱en鏄互瀛楄妭涓哄崟浣嶇殑鏁版嵁鐨勯暱搴︼紱flags涓鑸儏鍐典笅緗負0錛堝叧浜庤鍙傛暟鐨勭敤娉曞彲鍙傜収man鎵嬪唽錛夈?nbsp;
銆銆Send()鍑芥暟榪斿洖瀹為檯涓婂彂閫佸嚭鐨勫瓧鑺傛暟錛屽彲鑳戒細灝戜簬浣犲笇鏈涘彂閫佺殑鏁版嵁銆傚湪紼嬪簭涓簲璇ュ皢send()鐨勮繑鍥炲間笌嬈插彂閫佺殑瀛楄妭鏁拌繘琛屾瘮杈冦傚綋send()榪斿洖鍊間笌len涓嶅尮閰嶆椂錛屽簲璇ュ榪欑鎯呭喌榪涜澶勭悊銆?nbsp;
char *msg = "Hello!"; 
int len, bytes_sent; 
…… 
len = strlen(msg); 
bytes_sent = send(sockfd, msg,len,0); 
…… 
銆銆recv()鍑芥暟鍘熷瀷涓猴細 
銆銆int recv(int sockfd,void *buf,int len,unsigned int flags); 
銆銆Sockfd鏄帴鍙楁暟鎹殑socket鎻忚堪絎︼紱buf 鏄瓨鏀炬帴鏀舵暟鎹殑緙撳啿鍖猴紱len鏄紦鍐茬殑闀垮害銆侳lags涔熻緗負0銆俁ecv()榪斿洖瀹為檯涓婃帴鏀剁殑瀛楄妭鏁幫紝褰撳嚭鐜伴敊璇椂錛岃繑鍥?1騫剁疆鐩稿簲鐨別rrno鍊箋?nbsp;
Sendto()鍜宺ecvfrom()鐢ㄤ簬鍦ㄦ棤榪炴帴鐨勬暟鎹姤socket鏂瑰紡涓嬭繘琛屾暟鎹紶杈撱傜敱浜庢湰鍦皊ocket騫舵病鏈変笌榪滅鏈哄櫒寤虹珛榪炴帴錛屾墍浠ュ湪鍙戦佹暟鎹椂搴旀寚鏄庣洰鐨勫湴鍧銆?nbsp;
sendto()鍑芥暟鍘熷瀷涓猴細 
銆銆int sendto(int sockfd, const void *msg,int len,unsigned int flags,const struct sockaddr *to, int tolen); 
銆銆璇ュ嚱鏁版瘮send()鍑芥暟澶氫簡涓や釜鍙傛暟錛宼o琛ㄧず鐩湴鏈虹殑IP鍦板潃鍜岀鍙e彿淇℃伅錛岃宼olen甯稿父琚祴鍊間負sizeof (struct sockaddr)銆係endto 鍑芥暟涔熻繑鍥炲疄闄呭彂閫佺殑鏁版嵁瀛楄妭闀垮害鎴栧湪鍑虹幇鍙戦侀敊璇椂榪斿洖-1銆?nbsp;
銆銆Recvfrom()鍑芥暟鍘熷瀷涓猴細 
銆銆int recvfrom(int sockfd,void *buf,int len,unsigned int flags,struct sockaddr *from,int *fromlen); 
銆銆from鏄竴涓猻truct sockaddr綾誨瀷鐨勫彉閲忥紝璇ュ彉閲忎繚瀛樻簮鏈虹殑IP鍦板潃鍙婄鍙e彿銆俧romlen甯哥疆涓簊izeof (struct sockaddr)銆傚綋recvfrom()榪斿洖鏃訛紝fromlen鍖呭惈瀹為檯瀛樺叆from涓殑鏁版嵁瀛楄妭鏁般俁ecvfrom()鍑芥暟榪斿洖鎺ユ敹鍒扮殑瀛楄妭鏁版垨褰撳嚭鐜伴敊璇椂榪斿洖-1錛屽茍緗浉搴旂殑errno銆?nbsp;
濡傛灉浣犲鏁版嵁鎶ocket璋冪敤浜哻onnect()鍑芥暟鏃訛紝浣犱篃鍙互鍒╃敤send()鍜宺ecv()榪涜鏁版嵁浼犺緭錛屼絾璇ocket浠嶇劧鏄暟鎹姤socket錛屽茍涓斿埄鐢ㄤ紶杈撳眰鐨刄DP鏈嶅姟銆備絾鍦ㄥ彂閫佹垨鎺ユ敹鏁版嵁鎶ユ椂錛屽唴鏍鎬細鑷姩涓轟箣鍔犱笂鐩湴鍜屾簮鍦板潃淇℃伅銆?nbsp;

緇撴潫浼犺緭 
銆銆褰撴墍鏈夌殑鏁版嵁鎿嶄綔緇撴潫浠ュ悗錛屼綘鍙互璋冪敤close()鍑芥暟鏉ラ噴鏀捐socket錛屼粠鑰屽仠姝㈠湪璇ocket涓婄殑浠諱綍鏁版嵁鎿嶄綔錛?nbsp;
close(sockfd); 
銆銆浣犱篃鍙互璋冪敤shutdown()鍑芥暟鏉ュ叧闂socket銆傝鍑芥暟鍏佽浣犲彧鍋滄鍦ㄦ煇涓柟鍚戜笂鐨勬暟鎹紶杈擄紝鑰屼竴涓柟鍚戜笂鐨勬暟鎹紶杈撶戶緇繘琛屻傚浣犲彲浠ュ叧闂煇socket鐨勫啓鎿嶄綔鑰屽厑璁哥戶緇湪璇ocket涓婃帴鍙楁暟鎹紝鐩磋嚦璇誨叆鎵鏈夋暟鎹?nbsp;
銆銆int shutdown(int sockfd,int how); 
銆銆Sockfd鏄渶瑕佸叧闂殑socket鐨勬弿榪扮銆傚弬鏁?how鍏佽涓簊hutdown鎿嶄綔閫夋嫨浠ヤ笅鍑犵鏂瑰紡錛?nbsp;
銆銆·0-------涓嶅厑璁哥戶緇帴鏀舵暟鎹?nbsp;
銆銆·1-------涓嶅厑璁哥戶緇彂閫佹暟鎹?nbsp;
·2-------涓嶅厑璁哥戶緇彂閫佸拰鎺ユ敹鏁版嵁錛?nbsp;
·鍧囦負鍏佽鍒欒皟鐢╟lose () 
銆銆shutdown鍦ㄦ搷浣滄垚鍔熸椂榪斿洖0錛屽湪鍑虹幇閿欒鏃惰繑鍥?1騫剁疆鐩稿簲errno銆?nbsp;

闈㈠悜榪炴帴鐨凷ocket瀹炰緥 
銆銆浠g爜瀹炰緥涓殑鏈嶅姟鍣ㄩ氳繃socket榪炴帴鍚戝鎴風鍙戦佸瓧絎︿覆"Hello, you are connected!"銆傚彧瑕佸湪鏈嶅姟鍣ㄤ笂榪愯璇ユ湇鍔″櫒杞歡錛屽湪瀹㈡埛绔繍琛屽鎴瘋蔣浠訛紝瀹㈡埛绔氨浼氭敹鍒拌瀛楃涓層?nbsp;
銆銆璇ユ湇鍔″櫒杞歡浠g爜濡備笅錛?nbsp;
#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <sys/wait.h> 
#define SERVPORT 3333 /*鏈嶅姟鍣ㄧ洃鍚鍙e彿 */ 
#define BACKLOG 10 /* 鏈澶у悓鏃惰繛鎺ヨ姹傛暟 */ 
main() 

int sockfd,client_fd; /*sock_fd錛氱洃鍚瑂ocket錛沜lient_fd錛氭暟鎹紶杈搒ocket */ 
銆struct sockaddr_in my_addr; /* 鏈満鍦板潃淇℃伅 */ 
銆struct sockaddr_in remote_addr; /* 瀹㈡埛绔湴鍧淇℃伅 */ 
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
銆銆perror("socket鍒涘緩鍑洪敊錛?); exit(1); 

my_addr.sin_family=AF_INET; 
銆my_addr.sin_port=htons(SERVPORT); 
銆my_addr.sin_addr.s_addr = INADDR_ANY; 
bzero(&(my_addr.sin_zero),8); 
銆if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \ 
銆銆 == -1) { 
perror("bind鍑洪敊錛?); 
exit(1); 

銆if (listen(sockfd, BACKLOG) == -1) { 
perror("listen鍑洪敊錛?); 
exit(1); 

while(1) { 
銆銆sin_size = sizeof(struct sockaddr_in); 
銆銆if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, \ 
銆銆&sin_size)) == -1) { 
perror("accept鍑洪敊"); 
continue; 

銆銆printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr)); 
銆 if (!fork()) { /* 瀛愯繘紼嬩唬鐮佹 */ 
銆銆 if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1) 
銆銆 perror("send鍑洪敊錛?); 
close(client_fd); 
exit(0); 

銆銆close(client_fd); 
銆銆} 
銆} 

銆銆鏈嶅姟鍣ㄧ殑宸ヤ綔嫻佺▼鏄繖鏍風殑錛氶鍏堣皟鐢╯ocket鍑芥暟鍒涘緩涓涓猄ocket錛岀劧鍚庤皟鐢╞ind鍑芥暟灝嗗叾涓庢湰鏈哄湴鍧浠ュ強涓涓湰鍦扮鍙e彿緇戝畾錛岀劧鍚庤皟鐢╨isten鍦ㄧ浉搴旂殑socket涓婄洃鍚紝褰揳ccpet鎺ユ敹鍒頒竴涓繛鎺ユ湇鍔¤姹傛椂錛屽皢鐢熸垚涓涓柊鐨剆ocket銆傛湇鍔″櫒鏄劇ず璇ュ鎴鋒満鐨処P鍦板潃錛屽茍閫氳繃鏂扮殑socket鍚戝鎴風鍙戦佸瓧絎︿覆"Hello錛寉ou are connected!"銆傛渶鍚庡叧闂socket銆?nbsp;
銆銆浠g爜瀹炰緥涓殑fork()鍑芥暟鐢熸垚涓涓瓙榪涚▼鏉ュ鐞嗘暟鎹紶杈撻儴鍒嗭紝fork()璇彞瀵逛簬瀛愯繘紼嬭繑鍥炵殑鍊間負0銆傛墍浠ュ寘鍚玣ork鍑芥暟鐨刬f璇彞鏄瓙榪涚▼浠g爜閮ㄥ垎錛屽畠涓巌f璇彞鍚庨潰鐨勭埗榪涚▼浠g爜閮ㄥ垎鏄茍鍙戞墽琛岀殑銆?nbsp;

瀹㈡埛绔▼搴忎唬鐮佸涓嬶細 
#include<stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#define SERVPORT 3333 
#define MAXDATASIZE 100 /*姣忔鏈澶ф暟鎹紶杈撻噺 */ 
main(int argc, char *argv[]){ 
銆int sockfd, recvbytes; 
銆char buf[MAXDATASIZE]; 
銆struct hostent *host; 
銆struct sockaddr_in serv_addr; 
銆if (argc < 2) { 
fprintf(stderr,"Please enter the server's hostname!\n"); 
exit(1); 

銆if((host=gethostbyname(argv[1]))==NULL) { 
herror("gethostbyname鍑洪敊錛?); 
exit(1); 

銆if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ 
perror("socket鍒涘緩鍑洪敊錛?); 
exit(1); 

銆serv_addr.sin_family=AF_INET; 
銆serv_addr.sin_port=htons(SERVPORT); 
銆serv_addr.sin_addr = *((struct in_addr *)host->h_addr); 
銆bzero(&(serv_addr.sin_zero),8); 
銆if (connect(sockfd, (struct sockaddr *)&serv_addr, \ 
銆銆 sizeof(struct sockaddr)) == -1) { 
perror("connect鍑洪敊錛?); 
exit(1); 

銆if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1) { 
perror("recv鍑洪敊錛?); 
exit(1); 

銆buf[recvbytes] = '\0'; 
銆printf("Received: %s",buf); 
銆close(sockfd); 

銆銆瀹㈡埛绔▼搴忛鍏堥氳繃鏈嶅姟鍣ㄥ煙鍚嶈幏寰楁湇鍔″櫒鐨処P鍦板潃錛岀劧鍚庡垱寤轟竴涓猻ocket錛岃皟鐢╟onnect鍑芥暟涓庢湇鍔″櫒寤虹珛榪炴帴錛岃繛鎺ユ垚鍔熶箣鍚庢帴鏀朵粠鏈嶅姟鍣ㄥ彂閫佽繃鏉ョ殑鏁版嵁錛屾渶鍚庡叧闂璼ocket銆?nbsp;
銆銆鍑芥暟gethostbyname()鏄畬鎴愬煙鍚嶈漿鎹㈢殑銆傜敱浜嶪P鍦板潃闅句互璁板繂鍜岃鍐欙紝鎵浠ヤ負浜嗘柟渚匡紝浜轟滑甯稿父鐢ㄥ煙鍚嶆潵琛ㄧず涓繪満錛岃繖灝遍渶瑕佽繘琛屽煙鍚嶅拰IP鍦板潃鐨勮漿鎹€傚嚱鏁板師鍨嬩負錛?nbsp;
銆銆struct hostent *gethostbyname(const char *name); 
銆銆鍑芥暟榪斿洖涓篽osten鐨勭粨鏋勭被鍨嬶紝瀹冪殑瀹氫箟濡備笅錛?nbsp;
銆銆struct hostent { 
銆 char *h_name; /* 涓繪満鐨勫畼鏂瑰煙鍚?*/ 
銆銆 char **h_aliases; /* 涓涓互NULL緇撳熬鐨勪富鏈哄埆鍚嶆暟緇?*/ 
銆銆 int h_addrtype; /* 榪斿洖鐨勫湴鍧綾誨瀷錛屽湪Internet鐜涓嬩負AF-INET */ 
銆銆int h_length; /* 鍦板潃鐨勫瓧鑺傞暱搴?*/ 
銆銆 char **h_addr_list; /* 涓涓互0緇撳熬鐨勬暟緇勶紝鍖呭惈璇ヤ富鏈虹殑鎵鏈夊湴鍧*/ 
銆銆}; 
銆銆#define h_addr h_addr_list[0] /*鍦╤-addr-list涓殑絎竴涓湴鍧*/ 
銆銆褰?gethostname()璋冪敤鎴愬姛鏃訛紝榪斿洖鎸囧悜struct hosten鐨勬寚閽堬紝褰撹皟鐢ㄥけ璐ユ椂榪斿洖-1銆傚綋璋冪敤gethostbyname鏃訛紝浣犱笉鑳戒嬌鐢╬error()鍑芥暟鏉ヨ緭鍑洪敊璇俊鎭紝鑰屽簲璇ヤ嬌鐢╤error()鍑芥暟鏉ヨ緭鍑恒?nbsp;

銆銆鏃犺繛鎺ョ殑瀹㈡埛/鏈嶅姟鍣ㄧ▼搴忕殑鍦ㄥ師鐞嗕笂鍜岃繛鎺ョ殑瀹㈡埛/鏈嶅姟鍣ㄦ槸涓鏍風殑錛屼袱鑰呯殑鍖哄埆鍦ㄤ簬鏃犺繛鎺ョ殑瀹㈡埛/鏈嶅姟鍣ㄤ腑鐨勫鎴蜂竴鑸笉闇瑕佸緩绔嬭繛鎺ワ紝鑰屼笖鍦ㄥ彂閫佹帴鏀舵暟鎹椂錛岄渶瑕佹寚瀹氳繙绔満鐨勫湴鍧銆?nbsp;

闃誨鍜岄潪闃誨 
銆銆闃誨鍑芥暟鍦ㄥ畬鎴愬叾鎸囧畾鐨勪換鍔′互鍓嶄笉鍏佽紼嬪簭璋冪敤鍙︿竴涓嚱鏁般備緥濡傦紝紼嬪簭鎵ц涓涓鏁版嵁鐨勫嚱鏁拌皟鐢ㄦ椂錛屽湪姝ゅ嚱鏁板畬鎴愯鎿嶄綔浠ュ墠灝嗕笉浼氭墽琛屼笅涓紼嬪簭璇彞銆傚綋鏈嶅姟鍣ㄨ繍琛屽埌accept璇彞鏃訛紝鑰屾病鏈夊鎴瘋繛鎺ユ湇鍔¤姹傚埌鏉ワ紝鏈嶅姟鍣ㄥ氨浼氬仠姝㈠湪accept璇彞涓婄瓑寰呰繛鎺ユ湇鍔¤姹傜殑鍒版潵銆傝繖縐嶆儏鍐電О涓洪樆濉烇紙blocking錛夈傝岄潪闃誨鎿嶄綔鍒欏彲浠ョ珛鍗沖畬鎴愩傛瘮濡傦紝濡傛灉浣犲笇鏈涙湇鍔″櫒浠呬粎娉ㄦ剰媯鏌ユ槸鍚︽湁瀹㈡埛鍦ㄧ瓑寰呰繛鎺ワ紝鏈夊氨鎺ュ彈榪炴帴錛屽惁鍒欏氨緇х畫鍋氬叾浠栦簨鎯咃紝鍒欏彲浠ラ氳繃灝哠ocket璁劇疆涓洪潪闃誨鏂瑰紡鏉ュ疄鐜般傞潪闃誨socket鍦ㄦ病鏈夊鎴峰湪絳夊緟鏃跺氨浣縜ccept璋冪敤绔嬪嵆榪斿洖銆?nbsp;
銆銆#include <unistd.h> 
銆銆#include <fcntl.h> 
銆銆…… 
sockfd = socket(AF_INET,SOCK_STREAM,0); 
fcntl(sockfd,F_SETFL,O_NONBLOCK)錛?nbsp;
…… 
銆銆閫氳繃璁劇疆socket涓洪潪闃誨鏂瑰紡錛屽彲浠ュ疄鐜?杞"鑻ュ共Socket銆傚綋浼佸浘浠庝竴涓病鏈夋暟鎹瓑寰呭鐞嗙殑闈為樆濉濻ocket璇誨叆鏁版嵁鏃訛紝鍑芥暟灝嗙珛鍗寵繑鍥烇紝榪斿洖鍊間負-1錛屽茍緗甧rrno鍊間負EWOULDBLOCK銆備絾鏄繖縐?杞"浼氫嬌CPU澶勪簬蹇欑瓑寰呮柟寮忥紝浠庤岄檷浣庢ц兘錛屾氮璐圭郴緇熻祫婧愩傝岃皟鐢╯elect()浼氭湁鏁堝湴瑙e喅榪欎釜闂錛屽畠鍏佽浣犳妸榪涚▼鏈韓鎸傝搗鏉ワ紝鑰屽悓鏃朵嬌緋葷粺鍐呮牳鐩戝惉鎵瑕佹眰鐨勪竴緇勬枃浠舵弿榪扮鐨勪換浣曟椿鍔紝鍙紜鍦ㄤ換浣曡鐩戞帶鐨勬枃浠舵弿榪扮涓婂嚭鐜版椿鍔紝select()璋冪敤灝嗚繑鍥炴寚紺鴻鏂囦歡鎻忚堪絎﹀凡鍑嗗濂界殑淇℃伅錛屼粠鑰屽疄鐜頒簡涓鴻繘紼嬮夊嚭闅忔満鐨勫彉鍖栵紝鑰屼笉蹇呯敱榪涚▼鏈韓瀵硅緭鍏ヨ繘琛屾祴璇曡屾氮璐笴PU寮閿銆係elect鍑芥暟鍘熷瀷涓? 
int select(int numfds,fd_set *readfds,fd_set *writefds錛?nbsp;
fd_set *exceptfds,struct timeval *timeout); 
銆銆鍏朵腑readfds銆亀ritefds銆乪xceptfds鍒嗗埆鏄select()鐩戣鐨勮銆佸啓鍜屽紓甯稿鐞嗙殑鏂囦歡鎻忚堪絎﹂泦鍚堛傚鏋滀綘甯屾湜紜畾鏄惁鍙互浠庢爣鍑嗚緭鍏ュ拰鏌愪釜socket鎻忚堪絎﹁鍙栨暟鎹紝浣犲彧闇瑕佸皢鏍囧噯杈撳叆鐨勬枃浠舵弿榪扮0鍜岀浉搴旂殑sockdtfd鍔犲叆鍒皉eadfds闆嗗悎涓紱numfds鐨勫兼槸闇瑕佹鏌ョ殑鍙風爜鏈楂樼殑鏂囦歡鎻忚堪絎﹀姞1錛岃繖涓緥瀛愪腑numfds鐨勫煎簲涓簊ockfd+1錛涘綋select榪斿洖鏃訛紝readfds灝嗚淇敼錛屾寚紺烘煇涓枃浠舵弿榪扮宸茬粡鍑嗗琚鍙栵紝浣犲彲浠ラ氳繃FD_ISSSET()鏉ユ祴璇曘備負浜嗗疄鐜癴d_set涓搴旂殑鏂囦歡鎻忚堪絎︾殑璁劇疆銆佸浣嶅拰嫻嬭瘯錛屽畠鎻愪緵浜嗕竴緇勫畯錛?nbsp;
銆銆FD_ZERO(fd_set *set)----娓呴櫎涓涓枃浠舵弿榪扮闆嗭紱 
銆銆FD_SET(int fd,fd_set *set)----灝嗕竴涓枃浠舵弿榪扮鍔犲叆鏂囦歡鎻忚堪絎﹂泦涓紱 
銆銆FD_CLR(int fd,fd_set *set)----灝嗕竴涓枃浠舵弿榪扮浠庢枃浠舵弿榪扮闆嗕腑娓呴櫎錛?nbsp;
銆銆FD_ISSET(int fd,fd_set *set)----璇曞垽鏂槸鍚︽枃浠舵弿榪扮琚疆浣嶃?nbsp;
銆銆Timeout鍙傛暟鏄竴涓寚鍚憇truct timeval綾誨瀷鐨勬寚閽堬紝瀹冨彲浠ヤ嬌select()鍦ㄧ瓑寰卼imeout闀挎椂闂村悗娌℃湁鏂囦歡鎻忚堪絎﹀噯澶囧ソ鍗寵繑鍥炪俿truct timeval鏁版嵁緇撴瀯涓猴細 
銆銆struct timeval { 
銆銆 int tv_sec; /* seconds */ 
銆銆 int tv_usec; /* microseconds */ 
}; 

POP3瀹㈡埛绔疄渚?nbsp;
銆銆涓嬮潰鐨勪唬鐮佸疄渚嬪熀浜嶱OP3鐨勫鎴峰崗璁紝涓庨偖浠舵湇鍔″櫒榪炴帴騫跺彇鍥炴寚瀹氱敤鎴峰笎鍙風殑閭歡銆備笌閭歡鏈嶅姟鍣ㄤ氦浜掔殑鍛戒護瀛樺偍鍦ㄥ瓧絎︿覆鏁扮粍POPMessage涓紝紼嬪簭閫氳繃涓涓猟o-while寰幆渚濇鍙戦佽繖浜涘懡浠ゃ?nbsp;
#include<stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#define POP3SERVPORT 110 
#define MAXDATASIZE 4096 

main(int argc, char *argv[]){ 
int sockfd; 
struct hostent *host; 
struct sockaddr_in serv_addr; 
char *POPMessage[]={ 
"USER userid\r\n", 
"PASS password\r\n", 
"STAT\r\n", 
"LIST\r\n", 
"RETR 1\r\n", 
"DELE 1\r\n", 
"QUIT\r\n", 
NULL 
}; 
int iLength; 
int iMsg=0; 
int iEnd=0; 
char buf[MAXDATASIZE]; 

if((host=gethostbyname("your.server"))==NULL) { 
perror("gethostbyname error"); 
exit(1); 

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ 
perror("socket error"); 
exit(1); 

serv_addr.sin_family=AF_INET; 
serv_addr.sin_port=htons(POP3SERVPORT); 
serv_addr.sin_addr = *((struct in_addr *)host->h_addr); 
bzero(&(serv_addr.sin_zero),8); 
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1){ 
perror("connect error"); 
exit(1); 


do { 
send(sockfd,POPMessage[iMsg],strlen(POPMessage[iMsg]),0); 
printf("have sent: %s",POPMessage[iMsg]); 

iLength=recv(sockfd,buf+iEnd,sizeof(buf)-iEnd,0); 
iEnd+=iLength; 
buf[iEnd]='\0'; 
printf("received: %s,%d\n",buf,iMsg); 

iMsg++; 
} while (POPMessage[iMsg]); 

close(sockfd); 
}

鏆楀鏁欑埗 2010-02-24 13:32 鍙戣〃璇勮
]]>
欧美精品丝袜久久久中文字幕| 伊人色综合久久天天人守人婷| 韩国无遮挡三级久久| 久久国产乱子伦精品免费强| 久久久久国产视频电影| 伊人久久综合无码成人网| 久久精品国产亚洲av日韩| 香蕉久久夜色精品国产小说| 亚洲精品国产第一综合99久久| 色8久久人人97超碰香蕉987| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 国产综合免费精品久久久| 精品多毛少妇人妻AV免费久久| 久久精品www人人爽人人| 亚洲国产精品一区二区三区久久| 五月丁香综合激情六月久久| 色综合合久久天天综合绕视看| 久久人人超碰精品CAOPOREN| 久久99精品国产一区二区三区| 久久人做人爽一区二区三区| 国产一级持黄大片99久久| 一本色道久久综合| 久久青青草原精品国产软件| 99久久伊人精品综合观看| 久久亚洲私人国产精品vA| 久久精品免费全国观看国产| 伊人丁香狠狠色综合久久| 中文字幕无码av激情不卡久久| 久久综合中文字幕| 久久久久久久亚洲Av无码| 国产精品中文久久久久久久| 久久久无码精品午夜| 办公室久久精品| 久久精品国产免费一区| 久久久91精品国产一区二区三区| 久久99国产精品久久| 色综合合久久天天综合绕视看| 久久久国产精品福利免费| 99久久人人爽亚洲精品美女| 99久久无码一区人妻| 国内精品久久久久国产盗摄|