matching a string to struct value in an array of structs

Currently I have a working heartbeat client that will echo a key to the server.
The issue i'm running into on the server side is matching the client's key to a list of keys on the server, once matched the setting a struct bool to true.

The keylist consists of multiple names and keys deliminated by ":" like so:

client1:client1skey
client2:client2skey
client3:client3skey

full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <ctime>
#include <fstream>

using namespace std;

void hblisten (int clientSocket){
    char buf[4096];
    memset(buf, 0, 4096);
    int clienthblisten = recv(clientSocket, buf, 4096, 0);
	
	int numofclients = 0;
    string lines;
    ifstream CLIENTK;
    CLIENTK.open(".ckeylist");
    while (getline(CLIENTK, lines))
        ++numofclients;
    CLIENTK.close();
	
struct clientservers{
    string CLIENT, CKEY;
    bool clientok = false;
    }clients[numofclients];
	
	ifstream CLIENTK1(".ckeylist", ios::in);  
	CLIENTK1.open(".ckeylist");
	for (int i=0; i < numofclients; i++) {
            getline(CLIENTK1, clients[i].CLIENT, ':');
            getline(CLIENTK1, clients[i].CKEY, '\n');
            ++numofclients;
	}
            CLIENTK1.close();
			
	
	
string clientkey = string(buf, 0, clienthblisten);

if (clienthblisten == -1) {
        cerr << "Error receiving client HB" << endl;
		exit(1);
}

if (clienthblisten == 0) {
	  cout << "Client disconnected" << endl;
}

if (clienthblisten >1){
	if(strcmp(clientkey.c_str(), clients[numofclients].CKEY.c_str()) ==0){
		clients[numofclients].clientok == true;
	}
}
if(clients[numofclients].clientok == true){
		do something();
}




The issue i'm having is on this part. If the client's key matches one of the keys in the list, then I want to set the struct bool "clientok" to true to work with later.

1
2
3
4
5
6
7
8
if (clienthblisten >1){
	if(strcmp(clientkey.c_str(), clients[numofclients].CKEY.c_str()) ==0){
		clients[numofclients].clientok == true;
	}
}
if(clients[numofclients].clientok == true){
		do something();
}
Hi,

First, use == instead of string::compare when you want to check if two string are exactly equals.

Second, please fix your indentation.

Third, you don't treat the case "1 client is listening".

NB : If you have 0 client you will get some troubles with this line :
 
if(clients[numofclients].clientok == true){
Last edited on
do you have any troubles with a last line?
1
2
3
4
struct clientservers{
    string CLIENT, CKEY;
    bool clientok = false;
    }clients[numofclients];
The following is not allowed in standard C++:
1
2
3
4
5
6
    while (getline(CLIENTK, lines))
        ++numofclients;
...	
struct clientservers{
...
    }clients[numofclients];


In C++ array sizes must be compile time constants, consider a std::vector instead.

Why are you trying to use strcmp() with C++ strings instead of just using C++ string comparisons?

And remember that there is a difference between the comparison operator== and the assignment operator=.
Topic archived. No new replies allowed.