Home > Software design >  How to Multi-Client in UDP socket communication
How to Multi-Client in UDP socket communication

Time:06-20

I want to have multiple clients connect, and I want to do recvfrom and sendto. I have used std:vector as follows, but every time I recvfrom, I put information, so I wrote an if statement, but '==' gets an error.(no operator == matches these operands)

Is there a way not to add it to the vector when the same client 'recvfrom'?

std::vector<SOCKADDR_IN> udpClient_list;

addrlength = sizeof(dlg->client_addr);
recv_size = recvfrom(dlg->server_sock, reinterpret_cast<char*>(buf), BUFFER_SIZE, 0, (SOCKADDR*)&dlg->client_addr, &addrlength);

for (int i = 0; i == udpClient_list.size(); i  )
    {
        if (udpClient_list[i] != &dlg->client_addr) 
        {
            udpClient_list.push_back(dlg->client_addr);
            break;
        }
     }

CodePudding user response:

First, your loop condition is wrong. i should be size_t, and you need to use < instead of == when comparing i to udpClient_list.size().

Second, you are comparing a SOCKADDR_IN instance to a SOCKADDR_IN* pointer, which obviously won't work. Get rid of &. But even then, you can't compare SOCKADDR_IN instances using operator== unless you manually define that operator yourself.

Third, you need to add a new entry to the vector only if ALL of the existing entries don't match. But you are adding the new entry if ANY entry doesn't match.

Try something more like this:

bool operator==(const SOCKADDR_IN &addr1, const SOCKADDR_IN &addr2)
{
    return (addr1.sin_family == AF_INET &&
        addr1.sin_family == addr2.sin_family &&
        addr1.sin_port == addr2.sin_port &&
        addr1.sin_addr.s_addr == addr2.sin_addr.s_addr);
}

std::vector<SOCKADDR_IN> udpClient_list;

...

addrlength = sizeof(dlg->client_addr);
recv_size = recvfrom(dlg->server_sock, reinterpret_cast<char*>(buf), BUFFER_SIZE, 0, (SOCKADDR*)&dlg->client_addr, &addrlength);
if (recv_size < 0) ... // error handling

bool exists = false;
for (size_t i = 0; i < udpClient_list.size();   i)
{
    if (udpClient_list[i] == dlg->client_addr) 
    {
        exists = true;
        break;
    }
}

if (!exists)
    udpClient_list.push_back(dlg->client_addr);

With that said, consider using std::find() instead, eg:

bool operator==(const SOCKADDR_IN &addr1, const SOCKADDR_IN &addr2)
{
    return (addr1.sin_family == AF_INET &&
        addr1.sin_family == addr2.sin_family &&
        addr1.sin_port == addr2.sin_port &&
        addr1.sin_addr.s_addr == addr2.sin_addr.s_addr);
}

std::vector<SOCKADDR_IN> udpClient_list;

...

addrlength = sizeof(dlg->client_addr);
recv_size = recvfrom(dlg->server_sock, reinterpret_cast<char*>(buf), BUFFER_SIZE, 0, (SOCKADDR*)&dlg->client_addr, &addrlength);
if (recv_size < 0) ... // error handling

if (std::find(udpClient_list.begin(), udpClient_list.end(), dlg->client_addr) == udpClient_list.end())
    udpClient_list.push_back(dlg->client_addr);
  • Related