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