• <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>

            kenlistian

            厚積薄發. 勤為槳,思為帆

               :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理 ::
              73 隨筆 :: 4 文章 :: 22 評論 :: 0 Trackbacks

            //   Module   Name:   overlap.cpp  
              //         This   sample   illustrates   how   to   develop   a   simple   echo   server   Winsock  
              //         application   using   the   Overlapped   I/O   model   with   event   notification.   This    
              //         sample   is   implemented   as   a   console-style   application   and   simply   prints  
              //         messages   when   connections   are   established   and   removed   from   the   server.  
              //         The   application   listens   for   TCP   connections   on   port   5150   and   accepts   them  
              //         as   they   arrive.   When   this   application   receives   data   from   a   client,   it  
              //         simply   echos   (this   is   why   we   call   it   an   echo   server)   the   data   back   in  
              //         it's   original   form   until   the   client   closes   the   connection.  
              //  
              //   Compile:  
              //  
              //         cl   -o   overlap   overlap.cpp   ws2_32.lib  
              //  
              //         Note:   There   are   no   command   line   options   for   this   sample.  
              #include   <winsock2.h>  
              #include   <windows.h>  
              #include   <stdio.h>  
              #define   PORT   5150  
              #define   DATA_BUFSIZE   8192  
              typedef   struct   _SOCKET_INFORMATION   {  
                    CHAR   Buffer[DATA_BUFSIZE];  
                    WSABUF   DataBuf;  
                    SOCKET   Socket;  
                    WSAOVERLAPPED   Overlapped;  
                    DWORD   BytesSEND;  
                    DWORD   BytesRECV;  
              }   SOCKET_INFORMATION,   *   LPSOCKET_INFORMATION;  
              DWORD   WINAPI   ProcessIO(LPVOID   lpParameter);  
              DWORD   EventTotal   =   0;  
              WSAEVENT   EventArray[WSA_MAXIMUM_WAIT_EVENTS];  
              LPSOCKET_INFORMATION   SocketArray[WSA_MAXIMUM_WAIT_EVENTS];  
              CRITICAL_SECTION   CriticalSection;        
              void   main(void)  
              {  
                    WSADATA   wsaData;  
                    SOCKET   ListenSocket,   AcceptSocket;  
                    SOCKADDR_IN   InternetAddr;  
                    DWORD   Flags;  
                    DWORD   ThreadId;  
                    DWORD   RecvBytes;  
                    INT   Ret;  
                    InitializeCriticalSection(&CriticalSection);  
                    if   ((Ret   =   WSAStartup(0x0202,&wsaData))   !=   0)  
                    {  
                          printf("WSAStartup   failed   with   error   %d\n",   Ret);  
                          WSACleanup();  
                          return;  
                    }  
                    if   ((ListenSocket   =   WSASocket(AF_INET,   SOCK_STREAM,   0,   NULL,   0,    
                          WSA_FLAG_OVERLAPPED))   ==   INVALID_SOCKET)    
                    {  
                          printf("Failed   to   get   a   socket   %d\n",   WSAGetLastError());  
                          return;  
                    }  
                    InternetAddr.sin_family   =   AF_INET;  
                    InternetAddr.sin_addr.s_addr   =   htonl(INADDR_ANY);  
                    InternetAddr.sin_port   =   htons(PORT);  
                    if   (bind(ListenSocket,   (PSOCKADDR)   &InternetAddr,   sizeof(InternetAddr))   ==   SOCKET_ERROR)  
                    {  
                          printf("bind()   failed   with   error   %d\n",   WSAGetLastError());  
                          return;  
                    }  
                    if   (listen(ListenSocket,   5))  
                    {  
                          printf("listen()   failed   with   error   %d\n",   WSAGetLastError());  
                          return;  
                    }  
                    //   Setup   the   listening   socket   for   connections.  
                    if   ((AcceptSocket   =   WSASocket(AF_INET,   SOCK_STREAM,   0,   NULL,   0,  
                          WSA_FLAG_OVERLAPPED))   ==   INVALID_SOCKET)    
                    {  
                          printf("Failed   to   get   a   socket   %d\n",   WSAGetLastError());  
                          return;  
                    }  
                    if   ((EventArray[0]   =   WSACreateEvent())   ==   WSA_INVALID_EVENT)  
                    {  
                          printf("WSACreateEvent   failed   with   error   %d\n",   WSAGetLastError());  
                          return;  
                    }  


                    //   Create   a   thread   to   service   overlapped   requests  
                    if   (CreateThread(NULL,   0,   ProcessIO,   NULL,   0,   &ThreadId)   ==   NULL)  
                    {  
                          printf("CreateThread   failed   with   error   %d\n",   GetLastError());  
                          return;  
                    }    
                    EventTotal   =   1;  
                    while(TRUE)  
                    {  
                           // 服務端阻塞在這里等待
                          if   ((AcceptSocket   =   accept(ListenSocket,   NULL,   NULL))   ==   INVALID_SOCKET)  
                          {  
                                  printf("accept   failed   with   error   %d\n",   WSAGetLastError());  
                                  return;  
                          }  
                          EnterCriticalSection(&CriticalSection);  
                          //   Create   a   socket   information   structure   to   associate   with   the   accepted   socket.  
                          if   ((SocketArray[EventTotal]   =   (LPSOCKET_INFORMATION)   GlobalAlloc(GPTR,  
                                sizeof(SOCKET_INFORMATION)))   ==   NULL)  
                          {  
                                printf("GlobalAlloc()   failed   with   error   %d\n",   GetLastError());  
                                return;  
                          }    
                          //   Fill   in   the   details   of   our   accepted   socket.  
                          SocketArray[EventTotal]->Socket   =   AcceptSocket;  
                          ZeroMemory(&(SocketArray[EventTotal]->Overlapped),   sizeof(OVERLAPPED));  
                          SocketArray[EventTotal]->BytesSEND   =   0;  
                          SocketArray[EventTotal]->BytesRECV   =   0;  
                          SocketArray[EventTotal]->DataBuf.len   =   DATA_BUFSIZE;  
                          SocketArray[EventTotal]->DataBuf.buf   =   SocketArray[EventTotal]->Buffer;  
                          if   ((SocketArray[EventTotal]->Overlapped.hEvent   =   EventArray[EventTotal]   =    
                                  WSACreateEvent())   ==   WSA_INVALID_EVENT)  
                          {  
                                printf("WSACreateEvent()   failed   with   error   %d\n",   WSAGetLastError());  
                                return;  
                          }  
                          //   Post   a   WSARecv   request   to   to   begin   receiving   data   on   the   socket  
                          Flags   =   0;  
                          if   (WSARecv(SocketArray[EventTotal]->Socket,    
                                &(SocketArray[EventTotal]->DataBuf),   1,   &RecvBytes,   &Flags,  
                                &(SocketArray[EventTotal]->Overlapped),   NULL)   ==   SOCKET_ERROR)  
                          {  
                                if   (WSAGetLastError()   !=   ERROR_IO_PENDING)  
                                {  
                                      printf("WSARecv()   failed   with   error   %d\n",   WSAGetLastError());  
                                      return;  
                                }  
                          }  
                          EventTotal++;  
                          LeaveCriticalSection(&CriticalSection);  


                          //   Signal   the   first   event   in   the   event   array   to   tell   the   worker   thread   to  
                          //   service   an   additional   event   in   the   event   array  
                          if   (WSASetEvent(EventArray[0])   ==   FALSE)  
                          {  
                                printf("WSASetEvent   failed   with   error   %d\n",   WSAGetLastError());  
                                return;  
                          }  
                    }  
              } 

             

            DWORD   WINAPI   ProcessIO(LPVOID   lpParameter)  
              {  
                    DWORD   Index;  
                    DWORD   Flags;  
                    LPSOCKET_INFORMATION   SI;  
                    DWORD   BytesTransferred;  
                    DWORD   i;  
                    DWORD   RecvBytes,   SendBytes;  
                    //   Process   asynchronous   WSASend,   WSARecv   requests.  
                    while(TRUE)  
                    {  
                          if   ((Index   =   WSAWaitForMultipleEvents(EventTotal,   EventArray,   FALSE,  
                                WSA_INFINITE,   FALSE))   ==   WSA_WAIT_FAILED)  
                          {  
                                printf("WSAWaitForMultipleEvents   failed   %d\n",   WSAGetLastError());  
                                return   0;  
                          }    
                          //   If   the   event   triggered   was   zero   then   a   connection   attempt   was   made  
                          //   on   our   listening   socket.  
                          if   ((Index   -   WSA_WAIT_EVENT_0)   ==   0)  
                          {  
                                WSAResetEvent(EventArray[0]);  
                                continue;  
                          }  
                          SI   =   SocketArray[Index   -   WSA_WAIT_EVENT_0];  
                          WSAResetEvent(EventArray[Index   -   WSA_WAIT_EVENT_0]);  
                          if   (WSAGetOverlappedResult(SI->Socket,   &(SI->Overlapped),   &BytesTransferred,  
                                FALSE,   &Flags)   ==   FALSE   ||   BytesTransferred   ==   0)  
                          {  
                                printf("Closing   socket   %d\n",   SI->Socket);  
                                if   (closesocket(SI->Socket)   ==   SOCKET_ERROR)  
                                {  
                                      printf("closesocket()   failed   with   error   %d\n",   WSAGetLastError());  
                                }  
                                GlobalFree(SI);  
                                WSACloseEvent(EventArray[Index   -   WSA_WAIT_EVENT_0]);  
                                //   Cleanup   SocketArray   and   EventArray   by   removing   the   socket   event   handle  
                                //   and   socket   information   structure   if   they   are   not   at   the   end   of   the  
                                //   arrays.  
                                EnterCriticalSection(&CriticalSection);  
                                if   ((Index   -   WSA_WAIT_EVENT_0)   +   1   !=   EventTotal)  
                                      for   (i   =   Index   -   WSA_WAIT_EVENT_0;   i   <   EventTotal;   i++)  
                                      {  
                                            EventArray[i]   =   EventArray[i   +   1];  
                                            SocketArray[i]   =   SocketArray[i   +   1];  
                                      }  
                                EventTotal--;  
                                LeaveCriticalSection(&CriticalSection);  
                                continue;  
                          }  
                          //   Check   to   see   if   the   BytesRECV   field   equals   zero.   If   this   is   so,   then  
                          //   this   means   a   WSARecv   call   just   completed   so   update   the   BytesRECV   field  
                          //   with   the   BytesTransferred   value   from   the   completed   WSARecv()   call.  
                          if   (SI->BytesRECV   ==   0)  
                          {  
                                SI->BytesRECV   =   BytesTransferred;  
                                SI->BytesSEND   =   0;  
                          }  
                          else  
                          {  
                                SI->BytesSEND   +=   BytesTransferred;  
                          }  
                          if   (SI->BytesRECV   >   SI->BytesSEND)  
                          {  
                                //   Post   another   WSASend()   request.  
                                //   Since   WSASend()   is   not   gauranteed   to   send   all   of   the   bytes   requested,  
                                //   continue   posting   WSASend()   calls   until   all   received   bytes   are   sent.  
                                ZeroMemory(&(SI->Overlapped),   sizeof(WSAOVERLAPPED));  
                                SI->Overlapped.hEvent   =   EventArray[Index   -   WSA_WAIT_EVENT_0];  
                                SI->DataBuf.buf   =   SI->Buffer   +   SI->BytesSEND;  
                                SI->DataBuf.len   =   SI->BytesRECV   -   SI->BytesSEND;  
                                if   (WSASend(SI->Socket,   &(SI->DataBuf),   1,   &SendBytes,   0,  
                                      &(SI->Overlapped),   NULL)   ==   SOCKET_ERROR)  
                                {  
                                      if   (WSAGetLastError()   !=   ERROR_IO_PENDING)  
                                      {  
                                            printf("WSASend()   failed   with   error   %d\n",   WSAGetLastError());  
                                            return   0;  
                                      }  
                                }  
                          }  
                          else  
                          {  
                                SI->BytesRECV   =   0;  
                                //   Now   that   there   are   no   more   bytes   to   send   post   another   WSARecv()   request.  
                                Flags   =   0;  
                                ZeroMemory(&(SI->Overlapped),   sizeof(WSAOVERLAPPED));  
                                SI->Overlapped.hEvent   =   EventArray[Index   -   WSA_WAIT_EVENT_0];  
                                SI->DataBuf.len   =   DATA_BUFSIZE;  
                                SI->DataBuf.buf   =   SI->Buffer;  
                                if   (WSARecv(SI->Socket,   &(SI->DataBuf),   1,   &RecvBytes,   &Flags,  
                                      &(SI->Overlapped),   NULL)   ==   SOCKET_ERROR)  
                                {  
                                      if   (WSAGetLastError()   !=   ERROR_IO_PENDING)  
                                      {  
                                            printf("WSARecv()   failed   with   error   %d\n",   WSAGetLastError());  
                                            return   0;  
                                      }  
                                }  
                          }  
                    }  
              } 

            posted on 2008-01-03 15:50 kenlistian 閱讀(185) 評論(0)  編輯 收藏 引用
            久久精品免费网站网| 久久91这里精品国产2020| 久久精品青青草原伊人| 亚洲AV无码1区2区久久 | 狠狠色伊人久久精品综合网| 久久AⅤ人妻少妇嫩草影院| 中文字幕久久亚洲一区| 久久人妻少妇嫩草AV无码专区| 国内精品伊人久久久久| 怡红院日本一道日本久久 | 久久亚洲欧美国产精品| 亚洲国产成人久久综合一| 亚洲午夜精品久久久久久浪潮| 精品国产乱码久久久久久郑州公司 | 久久久无码精品亚洲日韩按摩 | 久久久久亚洲精品天堂久久久久久| 国产精品久久久久久久人人看| 日本三级久久网| 精品免费久久久久久久| 久久亚洲AV成人无码软件| 精品久久久久中文字| 久久福利青草精品资源站| AV无码久久久久不卡蜜桃| 国产精品内射久久久久欢欢| 精品国产91久久久久久久| 日韩精品久久无码人妻中文字幕 | 亚洲午夜久久久精品影院| 久久久亚洲欧洲日产国码二区| 超级碰碰碰碰97久久久久| 久久er国产精品免费观看8| 久久99国产精品二区不卡| 国产亚洲精品美女久久久| 亚洲AV无码久久寂寞少妇| 日产精品久久久久久久| 久久综合给合久久国产免费| 欧美喷潮久久久XXXXx| 亚洲av日韩精品久久久久久a| 精品久久久无码人妻中文字幕| 精品多毛少妇人妻AV免费久久| 中文成人久久久久影院免费观看| 久久99精品久久久久久秒播|