当前位置: 首页>>代码示例>>C++>>正文


C++ SocketServer::GetNConnections方法代码示例

本文整理汇总了C++中SocketServer::GetNConnections方法的典型用法代码示例。如果您正苦于以下问题:C++ SocketServer::GetNConnections方法的具体用法?C++ SocketServer::GetNConnections怎么用?C++ SocketServer::GetNConnections使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SocketServer的用法示例。


在下文中一共展示了SocketServer::GetNConnections方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

//Note, fd = file descriptor
int main(int argc, char* argv[])
{
  SocketServer MyServer;
  MyServer.SetupConnection(PortNo);
  while(true)
  { 
    MyServer.WaitForAction();
    vector<int> ReadSockets = MyServer.ListReadableSockets();
    for(vector<int>::iterator it=ReadSockets.begin();it!=ReadSockets.end(); it++)
    { 
      if(MyServer.IsNewConnection(*it))
      {
        string WelcomeMessage;
        if(MyServer.GetNConnections() > 0) //There are people already here
        {
           WelcomeMessage="Welcome the other connected users are: ";
           vector<string> Names = MyServer.ListNames();
           for(vector<string>::iterator it = Names.begin(); it!=Names.end();it++)
           {
              WelcomeMessage.append(*it);
              WelcomeMessage.append(", ");
           }
           //Remove the trailing comma and space;
           WelcomeMessage.pop_back();
           WelcomeMessage.pop_back();
          //Who gives a fuck about an oxford comma
          if(Names.size() > 1)
          {
             size_t commaPos = WelcomeMessage.find_last_of(',');
             WelcomeMessage[commaPos] = ' ';
             WelcomeMessage.insert(commaPos+1,"&");
          }
        }
        else
        {
          WelcomeMessage="Welcome, you are currently the only connected user.";
        }
        int new_fd = MyServer.RecieveConnection();
        string name = MyServer.RecieveMessage(new_fd);
        MyServer.NamesMap[new_fd] = name;
        MyServer.SendMessage(WelcomeMessage, new_fd);
        string ConnectionAlert = name;
        ConnectionAlert.append(" just connected\n");
        MyServer.SendToEveryoneExcept(ConnectionAlert,new_fd,false);
      }
      else
      {
        string message = MyServer.RecieveMessage(*it);
        if(message.size() >0)
        {
          cout<<"Recieved message from "<<MyServer.NamesMap[*it]<<endl;
          MyServer.SendToEveryoneExcept(message,*it);
        }
        else if(message == "")
        {
          cout<<"Someone closed a connection\n";
          MyServer.CloseConnection(*it);    
        }
      } 
    }  //End loop over sockets in set  
  }  //End while(true)
  return 0;
}
开发者ID:OrderOfMagnitude,项目名称:ImportantThings,代码行数:64,代码来源:MyServer.C


注:本文中的SocketServer::GetNConnections方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。