String comparison Question

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
bool readline(int socketfd, string &line) {
  int n; 
  char buffer[1025]; 

  line = "";

  while (line.find('\n') == string::npos) {
    int n = read(socketfd, buffer, 1024); // ler do socket
    if (n == 0) return false; // nada para ser lido -> socket fechado
    buffer[n] = 0; // colocar o \0 no fim do buffer
    line += buffer; // acrescentar os dados lidos à string
  }
  // Retirar o \n (lemos uma linha mas não precisamos do \n)
  line.erase(line.end() - 1);
  
  return true;  
}

void* cliente(void* args) {
  int sockfd = *(int*)args;
  string line;
  
  PGconn *conn = PQconnectdb("host ='***' user='***' password='***'");
  if(!conn){ cout << "Failed to connect to database." << endl; exit(-1);}
  if(PQstatus(conn) != CONNECTION_OK) { cout << "Failed to connect to database" << endl; exit(-1);}
  else if(PQstatus(conn) == CONNECTION_OK) cout << "You have connected sucefully to the Database." << endl;
  string one;
  connect_to_schema(conn);
  
  clients.insert(sockfd);
  cout << "Client connected: " << sockfd << endl;
  
  login(sockfd, conn);
  
  string one;
  readline(sockfd, one);
  cout << one << endl;
  if(one == "hello") cout << "testehello" << endl;
  
  while (readline(sockfd, line)) {
    one = line;
    int n = one.find("/");
    if(one == "find") {cout << "testefind" << endl;}
    cout << "Socket " << sockfd << " said: " << line << endl;
    broadcast(sockfd,line,0);
  }
  
  cout << "Client disconnected: " << sockfd << endl;
  clients.erase(sockfd);
  close(sockfd);
}


The purpose of this is to read a string from a socket and compare to another string. Basically, I want to know why this:

if(one == "hello") cout << "testehello" << endl;

doesn't work, especially when I have the same logic here:

1
2
3
4
5
6
7
readline(sockfd, comando);
  int s1 = comando.find(" ");
  string login = comando.substr(0,s1);
  string password = comando.substr(s1+1);
  password.erase(password.end() - 1);
  comando.erase(comando.end() - 1);
  if (comando == "/help"){callhelp(sockfd); login2(sockfd,conn);}


And this one works. The 'one' string prints hello, both to the terminal and the socket, but the comparison isn't working.
It does work. The error is somewhere else. (Btw: you declare one twice).
I just now noticed that I do this
comando.erase(comando.end() - 1);
in the second portion but forgot on the first. I'll try again.
Topic archived. No new replies allowed.