简体   繁体   中英

how to tag clients with winsock

I have a simple server client winsock program running. And wanna know and add 2 things to it.

How do I tag/ID clients?

And be able to send ALL clients data after wards?

This is the connection loop I have at the moment. The "client_id[a]" is something I tried to tag/id the clients but probably incorrect.

for(;;)
{
 if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
  {
    std::cout<<"\nConnection was reached";    
    a = a +1;
    client_id[a] = accept(Listen, (SOCKADDR*)&Server, &size) ???
  }
}

Using C/C++ and windows.

Hopefully someone can help me solve this problem.

Thanks.

Each client connected to your server has already its own identifier SOCKET. do not use your custom identifier. Also use dynamic array -> std::vector or map in order to keep your clients.

Also you have double accept you will lose every second connection.

std::vector< SOCKET > clients;
...
for(;;)
{
 if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
  {
    std::cout<<"\nConnection was reached";    
    clients.push_back( Connect )
  }
}

In case of map you have something like

std::map< SOCKET, YouClientClass > clients;
...
for(;;)
{
 if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
  {
    std::cout<<"\nConnection was reached";    
    clients.insert( std::make_pair( Connect, YourClientClass(Connect))); 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM