CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  网络编程

高分.怎样才能使SERVER端接受多个连接??用的是socket套接字,最好是异步的.谢谢.

楼主microgiant(技术已死,有事烧纸)2004-12-01 22:46:32 在 VC/MFC / 网络编程 提问

RT,最好能够给些代码.给帮我实现的,另外开贴再给100分,谢谢,小弟比较急.不好意思.先到先得 问题点数:100、回复次数:14Top

1 楼HunterForPig(留着口水的猪)回复于 2004-12-01 23:28:25 得分 10

SOCKET   clientSocket[64];  
  int   i=0;  
  while(true)  
  {  
   
  clientSocket[i]=accept(...);   //当有连接是存入  
  i++;  
   
  }  
  任何模型都用类此发接受连接!Top

2 楼microgiant(技术已死,有事烧纸)回复于 2004-12-01 23:56:11 得分 0

再请教一下SELECT的用法,不好意思,谢谢了Top

3 楼microgiant(技术已死,有事烧纸)回复于 2004-12-01 23:57:07 得分 0

不仅仅是SELECT,是SELECT系列,比如WSAAsyncSelect等,谢谢Top

4 楼fu256(大圣)回复于 2004-12-02 00:24:39 得分 50

我刚写了一个WSAEventSelect模式的一个Tcp类,  
  能接受多个连接,方便通用。  
  上QQ:86363405     或告速我你的Email  
  我发给你。Top

5 楼Zeki_CN(Zeki_CN)回复于 2004-12-02 08:52:50 得分 20

运气好,我这儿有个例子。哈哈。  
  //   Module   Name:   select.cpp  
  //  
  //   Description:  
  //  
  //         This   sample   illustrates   how   to   develop   a   simple   echo   server   Winsock  
  //         application   using   the   select()   API   I/O   model.   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   select   select.cpp   ws2_32.lib  
  //  
  //   Command   Line   Options:  
  //  
  //         select.exe    
  //  
  //         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;  
        OVERLAPPED   Overlapped;  
        DWORD   BytesSEND;  
        DWORD   BytesRECV;  
  }   SOCKET_INFORMATION,   *   LPSOCKET_INFORMATION;  
   
  BOOL   CreateSocketInformation(SOCKET   s);  
  void   FreeSocketInformation(DWORD   Index);  
   
  DWORD   TotalSockets   =   0;  
  LPSOCKET_INFORMATION   SocketArray[FD_SETSIZE];  
   
  void   main(void)  
  {  
        SOCKET   ListenSocket;  
        SOCKET   AcceptSocket;  
        SOCKADDR_IN   InternetAddr;  
        WSADATA   wsaData;  
        INT   Ret;  
        FD_SET   WriteSet;  
        FD_SET   ReadSet;  
        DWORD   i;  
        DWORD   Total;  
        ULONG   NonBlock;  
        DWORD   Flags;  
        DWORD   SendBytes;  
        DWORD   RecvBytes;  
   
   
        if   ((Ret   =   WSAStartup(0x0202,&wsaData))   !=   0)  
        {  
              printf("WSAStartup()   failed   with   error   %d\n",   Ret);  
              WSACleanup();  
              return;  
        }  
   
        //   Prepare   a   socket   to   listen   for   connections.  
   
        if   ((ListenSocket   =   WSASocket(AF_INET,   SOCK_STREAM,   0,   NULL,   0,  
              WSA_FLAG_OVERLAPPED))   ==   INVALID_SOCKET)    
        {  
              printf("WSASocket()   failed   with   error   %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;  
        }  
   
        //   Change   the   socket   mode   on   the   listening   socket   from   blocking   to  
        //   non-block   so   the   application   will   not   block   waiting   for   requests.  
   
        NonBlock   =   1;  
        if   (ioctlsocket(ListenSocket,   FIONBIO,   &NonBlock)   ==   SOCKET_ERROR)  
        {  
              printf("ioctlsocket()   failed   with   error   %d\n",   WSAGetLastError());  
              return;  
        }  
   
        while(TRUE)  
        {  
              //   Prepare   the   Read   and   Write   socket   sets   for   network   I/O   notification.  
              FD_ZERO(&ReadSet);  
              FD_ZERO(&WriteSet);  
   
              //   Always   look   for   connection   attempts.  
   
              FD_SET(ListenSocket,   &ReadSet);  
   
              //   Set   Read   and   Write   notification   for   each   socket   based   on   the  
              //   current   state   the   buffer.     If   there   is   data   remaining   in   the  
              //   buffer   then   set   the   Write   set   otherwise   the   Read   set.  
   
              for   (i   =   0;   i   <   TotalSockets;   i++)  
                    if   (SocketArray[i]->BytesRECV   >   SocketArray[i]->BytesSEND)  
                          FD_SET(SocketArray[i]->Socket,   &WriteSet);  
                    else  
                          FD_SET(SocketArray[i]->Socket,   &ReadSet);  
   
              if   ((Total   =   select(0,   &ReadSet,   &WriteSet,   NULL,   NULL))   ==   SOCKET_ERROR)  
              {  
                    printf("select()   returned   with   error   %d\n",   WSAGetLastError());  
                    return;  
              }  
   
              //   Check   for   arriving   connections   on   the   listening   socket.  
              if   (FD_ISSET(ListenSocket,   &ReadSet))  
              {  
                    Total--;  
                    if   ((AcceptSocket   =   accept(ListenSocket,   NULL,   NULL))   !=   INVALID_SOCKET)  
                    {  
   
                          //   Set   the   accepted   socket   to   non-blocking   mode   so   the   server   will  
                          //   not   get   caught   in   a   blocked   condition   on   WSASends  
   
                          NonBlock   =   1;  
                          if   (ioctlsocket(AcceptSocket,   FIONBIO,   &NonBlock)   ==   SOCKET_ERROR)  
                          {  
                                printf("ioctlsocket()   failed   with   error   %d\n",   WSAGetLastError());  
                                return;  
                          }  
   
                          if   (CreateSocketInformation(AcceptSocket)   ==   FALSE)  
                                return;  
   
                    }  
                    else  
                    {  
                          if   (WSAGetLastError()   !=   WSAEWOULDBLOCK)  
                          {  
                                printf("accept()   failed   with   error   %d\n",   WSAGetLastError());  
                                return;  
                          }  
                    }  
              }  
   
              //   Check   each   socket   for   Read   and   Write   notification   until   the   number  
              //   of   sockets   in   Total   is   satisfied.  
   
              for   (i   =   0;   Total   >   0   &&   i   <   TotalSockets;   i++)  
              {  
                    LPSOCKET_INFORMATION   SocketInfo   =   SocketArray[i];  
   
                    //   If   the   ReadSet   is   marked   for   this   socket   then   this   means   data  
                    //   is   available   to   be   read   on   the   socket.  
   
                    if   (FD_ISSET(SocketInfo->Socket,   &ReadSet))  
                    {  
                          Total--;  
   
                          SocketInfo->DataBuf.buf   =   SocketInfo->Buffer;  
                          SocketInfo->DataBuf.len   =   DATA_BUFSIZE;  
   
                          Flags   =   0;  
                          if   (WSARecv(SocketInfo->Socket,   &(SocketInfo->DataBuf),   1,   &RecvBytes,  
                                &Flags,   NULL,   NULL)   ==   SOCKET_ERROR)  
                          {  
                                if   (WSAGetLastError()   !=   WSAEWOULDBLOCK)  
                                {  
                                      printf("WSARecv()   failed   with   error   %d\n",   WSAGetLastError());  
   
                                      FreeSocketInformation(i);  
                                }  
   
                                continue;  
                          }    
                          else  
                          {  
                                SocketInfo->BytesRECV   =   RecvBytes;  
   
                                //   If   zero   bytes   are   received,   this   indicates   the   peer   closed   the  
                                //   connection.  
                                if   (RecvBytes   ==   0)  
                                {  
                                      FreeSocketInformation(i);  
                                      continue;  
                                }  
                          }  
                    }  
   
   
                    //   If   the   WriteSet   is   marked   on   this   socket   then   this   means   the   internal  
                    //   data   buffers   are   available   for   more   data.  
   
                    if   (FD_ISSET(SocketInfo->Socket,   &WriteSet))  
                    {  
                          Total--;  
   
                          SocketInfo->DataBuf.buf   =   SocketInfo->Buffer   +   SocketInfo->BytesSEND;  
                          SocketInfo->DataBuf.len   =   SocketInfo->BytesRECV   -   SocketInfo->BytesSEND;  
   
                          if   (WSASend(SocketInfo->Socket,   &(SocketInfo->DataBuf),   1,   &SendBytes,   0,  
                                NULL,   NULL)   ==   SOCKET_ERROR)  
                          {  
                                if   (WSAGetLastError()   !=   WSAEWOULDBLOCK)  
                                {  
                                      printf("WSASend()   failed   with   error   %d\n",   WSAGetLastError());  
   
                                      FreeSocketInformation(i);  
                                }  
   
                                continue;  
                          }  
                          else  
                          {  
                                SocketInfo->BytesSEND   +=   SendBytes;  
   
                                if   (SocketInfo->BytesSEND   ==   SocketInfo->BytesRECV)  
                                {  
                                      SocketInfo->BytesSEND   =   0;  
                                      SocketInfo->BytesRECV   =   0;  
                                }  
                          }  
                    }  
              }  
        }  
  }  
   
  Top

6 楼Zeki_CN(Zeki_CN)回复于 2004-12-02 08:53:13 得分 0

接上:  
  BOOL   CreateSocketInformation(SOCKET   s)  
  {  
        LPSOCKET_INFORMATION   SI;  
               
        printf("Accepted   socket   number   %d\n",   s);  
   
        if   ((SI   =   (LPSOCKET_INFORMATION)   GlobalAlloc(GPTR,  
              sizeof(SOCKET_INFORMATION)))   ==   NULL)  
        {  
              printf("GlobalAlloc()   failed   with   error   %d\n",   GetLastError());  
              return   FALSE;  
        }  
   
        //   Prepare   SocketInfo   structure   for   use.  
   
        SI->Socket   =   s;  
        SI->BytesSEND   =   0;  
        SI->BytesRECV   =   0;  
   
        SocketArray[TotalSockets]   =   SI;  
   
        TotalSockets++;  
   
        return(TRUE);  
  }  
   
  void   FreeSocketInformation(DWORD   Index)  
  {  
        LPSOCKET_INFORMATION   SI   =   SocketArray[Index];  
        DWORD   i;  
   
        closesocket(SI->Socket);  
   
        printf("Closing   socket   number   %d\n",   SI->Socket);  
   
        GlobalFree(SI);  
   
        //   Squash   the   socket   array  
   
        for   (i   =   Index;   i   <   TotalSockets;   i++)  
        {  
              SocketArray[i]   =   SocketArray[i   +   1];  
        }  
   
        TotalSockets--;  
  }  
  Top

7 楼DentistryDoctor(不在无聊中无奈,就在沉默中变态)回复于 2004-12-02 09:00:25 得分 0

建议楼主阅读<Windows网络编程>,此书第二版有电子书下载。Top

8 楼alec626(月吻长河Blog:spaces.msn.com/filebase)回复于 2004-12-02 19:29:27 得分 0

楼主的问题是典型的Select应用。Top

9 楼laiyiling(陌生人[MVP])回复于 2004-12-08 09:54:27 得分 10

http://www.vckbase.com/code/listcode.asp?mclsid=9&sclsid=901Top

10 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2004-12-08 11:11:59 得分 0

参考Windows网络编程,上面有各种模型Top

11 楼microgiant(技术已死,有事烧纸)回复于 2004-12-08 13:02:47 得分 0

我晕,谢谢各位,我只是想知道,怎么能在WSAAsyncSelect实现服务器的多个连接.Top

12 楼nuaawenlin(飘人)回复于 2004-12-08 13:07:24 得分 0

WSAAsyncSelect(Accept,this->m_hWnd,WM_SOCKET,FD_READ|FD_CLOSE);Top

13 楼c4313848(从自己做起,让抵制日货成为一种时尚,成为一种潮流)回复于 2004-12-13 11:11:02 得分 10

就是在ACCEPT之后,把SOCKET保存起来,放到链表还是数组就随便了Top

14 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2004-12-13 11:31:29 得分 0

保存多个socketTop

相关问题

  • socket 异步通讯server求教
  • 高分求教:c# socket 异步单线程SOCKET SERVER
  • 异步socket问题
  • socket 同步 or 异步?
  • socket异步传输数据
  • 异步方式socket问题
  • 异步socket通信问题
  • 异步Socket关闭出错
  • 关于异步Socket传输问题
  • 关于异步socket的回调

关键词

  • socket
  • 连接
  • socketinfo
  • bytessend
  • socketarray
  • wsagetlasterror
  • listensocket
  • bytesrecv
  • freesocketinformation
  • lpsocket

得分解答快速导航

  • 帖主:microgiant
  • HunterForPig
  • fu256
  • Zeki_CN
  • laiyiling
  • c4313848

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo