各位大哥,小弟想问一个问题:我得做一个服务器程序,这个服务器要实时的接收客户端发来的数据然后存放在数据库中。而且通讯高峰期的连接数很可能很大  1000都有可能。现在想问的就是用套接字做这个东西的最佳方案,或者说典型的方案。
新手在这谢了!!!!

解决方案 »

  1.   

    IO Completed Port, http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancediomethod5i.html
      

  2.   

    服务器端代码,你参考下
      #include   <iostream.h>   
      #include   <unistd.h>   
      #include   <sys/socket.h>   
      #include   <netinet/in.h>   
      #include   <arpa/inet.h>   
      #include   <netdb.h>   
      #include   <pthread.h>   
      int   port=8000;   
      void   *task(void   *arg);   
      int   ReadFile(const   char   *filename,char   *pFile,int   &   nLen);   
      int   WriteFile(const   char   *filename,const   char   *pFile,const   int   nLen);   
      int   main()   
      {   
                      struct   sockaddr_in   sin;   
                      struct   sockaddr_in   pin;   
                      int   sock_descriptor;   
                      int   temp_sock_descriptor;   
                      int   address_size;   
                      //char   buf[16384];   
                      int   i,len;   
                      pthread_t   thread;   
                      sock_descriptor=socket(AF_INET,SOCK_STREAM,0);//建立主套接字   
                      if(sock_descriptor==-1)   
                      {   
                                      perror("call   to   socket");   
                                      exit(1);   
                      }   
                      bzero(&sin,sizeof(sin));   
                      sin.sin_family=AF_INET;   
                      sin.sin_addr.s_addr=INADDR_ANY;   
                      sin.sin_port=htons(port);   
                                                                                                                                                                                                                                                                                                      
                      if(bind(sock_descriptor,(struct   sockaddr   *)&sin,sizeof(sin))==-1)//绑定地址   
                      {   
                                      perror("call   to   bind");   
                                      exit(1);   
                      }   
            if(listen(sock_descriptor,20)==-1)   
                      {   
                                      perror("call   to   listen");   
                                      exit(1);   
                      }   
                      cout<<"Accepting   connections........"<<endl;   
                      while(1)   
                      {   
                                      temp_sock_descriptor=accept(sock_descriptor,(struct   sockaddr   *)&pin,(socklen_t   *)&address_size);//阻塞接收连接   
                                      if(temp_sock_descriptor==-1)   
                                      {   
                                                      perror("call   to   accept");   
                                                      exit(1);   
                                      }   
      //创建一个新的线程来处理通信,以accept到的临时套接字作为参数   
                                      if(pthread_create(&thread,NULL,task,(void   *)&temp_sock_descriptor)!=0)   
                                      {   
                                                      perror("pthread_create:task");   
                                                      exit(1);   
                                      }   
                                      if(pthread_detach(thread)!=0)   
                                      {   
                                                      perror("pthread_detach");   
                                                      exit(1);   
                                      }   
                      }   
      }   
      void   *task(void   *arg)   
      {   
                      int   tmpsock;   
                      int   len;   
                      char   *pFile=NULL;   
                      tmpsock=*(int   *)arg;   
                    //读取长度   
                      if(recv(tmpsock,&len,sizeof(int),0)==-1)   
                      {   
                                      perror("call   to   recv");   
                                      exit(1);   
                      }   
                    //分配接收缓存   
                      if(len>0)   
                      {   
                                      pFile=new   char[len];   
                      }   
                    //接收真正的数据   
                      for(int   i=0;i<len/8192+1;i++)   
                      {   
                                      if(recv(tmpsock,pFile+i*8192,8192,0)==-1)   
                                      {   
                                                      perror("recv   error");   
                                                      exit(-1);   
                                      }   
                      }   
                      //把接收到的数据写到本地文件中   
                      if(WriteFile("testfile.txt",pFile,len)!=0)   
                      {   
                                      printf("exit   when   server   Write   File\n");   
                                      perror("Write   file   error");   
                                      exit(-1);   
                      }   
                      //给客户端发送接收完毕确认   
                      if(send(tmpsock,"Recieved   OK",11,0)==-1)   
                      {   
                                      printf("exit   when   server   send\n");   
                                      perror("call   to   send");   
                                      exit(1);   
                      }   
                      if(pFile!=NULL)   
                                      delete   pFile;   
                      close(tmpsock);//关闭临时套接字   
                                                                                                                                                                                                                                                                                                      
      }   
      int   ReadFile(const   char   *filename,char   *pFile,int   &   nLen)   
      {   
                      FILE   *stream=NULL;   
                      char   buf[100];   
                      int   count=0;   
                                                                                                                                                                                                                                                                                                      
                      if(pFile==NULL)   
                      {   
                                      nLen=0;   
                                      stream=fopen(filename,"r");   
                                      if(stream==NULL)   
                                      {   
                                                      perror("Open   file   error");   
                                                      return   -1;   
                                      }   
                                      while(!feof(stream))   
                                      {   
                                                      count=fread(buf,sizeof(char),100,stream);   
                                                      nLen+=count;   
                                      }   
                                      fclose(stream);   
                                      return   0;   
                      }   
                      stream=fopen(filename,"r");   
                      fread(pFile,sizeof(char),nLen,stream);   
                      fclose(stream);   
                      return   0;   
      }   
      int   WriteFile(const   char   *filename,const   char   *pFile,const   int   nLen)   
      {   
                      FILE   *stream=NULL;   
                      stream=fopen(filename,"w+");   
                      if(stream==NULL)   
                      {   
                                      perror("Open   file   error");   
                                      return   -1;   
                      }   
                      if(fwrite((void   *)pFile,1,nLen,stream)!=nLen)   
                      {   
                                      perror("Write   file   error");   
                                      fclose(stream);   
                                      return   -1;   
                      }   
                      fclose(stream);   
                      return   0;   
      }   
      

  3.   

    给你个现成的http://www.codeproject.com/KB/IP/winsockiocp.asp
      

  4.   

    1000个连接并不算高, 你得选个合适的SOCKET I/0 模型, 重叠模型,完成模型 1000连接都比较轻松搞定
    SOCKET接收线程只单纯接包,包的处理,如果比较复杂的话, 另外用线程处理 
      

  5.   

    建议楼主还是看下基础的socket模型,然后在结合多线程,内存管理方面的书籍,可以使用iocp了。
      

  6.   

    boost::asio,网络部分由这个来做,只管考虑怎样高效的处理收到的数据吧
      

  7.   

    参看下面网址:
    http://www.joyvc.cn/NetworkAndCommunication/NetworkAndCommunicationGroup00221.html
      

  8.   

    如果有时间的话,ACE也可以看看
      

  9.   

    1000的并发量,用SELCECT模型都可以搞定。
    在PC机上,我测试过4K连接的.。。
      

  10.   

    昨天小弟进行机器人并发网络压力测试,IOCP轻松过万~