   Sockets and c_str
  Hi all,I skip between vb C and c++ according to my needs and sometimes I get the syntax and stuff mixed up, but I am having a problem.I setup some server code and when a client types 'exit' I want the exit code to run, but for some reason no matter how many times I type exit as a client it doesn't run the exit code. In the example below I just set it to printf to see if it is being run.I even tried if(r.c_str()=="exit") printf("break\n");as well but that didn't work either.I believe when you use c_str() it is just a pointer, but I don't totallyunderstand c++ strings as yet, even though I have googled lots heh heh.My code is below.#include "Socket.h"#include <process.h>#include <string>using namespace std;unsigned __stdcall Answer(void* a) {  Socket* s = (Socket*) a;  while (1) {    std::string r = s->ReceiveLine();    if (r.empty()) break; if(r=="exit") printf("break\n");    s->SendLine(r);    printf("%s",r.c_str()); //   MessageBox(NULL, r.c_str(), "Help", MB_OK);  }  delete s;  return 0;}int main(int argc, char* argv[]) {  SocketServer in(3000,5);  while (1) {    Socket* s=in.Accept();    unsigned ret;    _beginthreadex(0,0,Answer,(void*) s,0,&ret;  }  return 0;}Thanks in advanceAsimov500   
  Doing r == "exit" is correct.  Have you tried printing out r to see if it actually does equal "exit"?  Just add std::cout << "r: " << r before the if statement to see what r actually is.  Or step through it with the debugger.   ;  Hi Mike,In my code I had a printf("%s",r.c_str()); line which echoedit to my screen, but I did as  you suggested and put incout << "r: " << r;Of course I had to add #include <iostream.h> to make cout work.Anyway to cut a long story short when I type exit in my telnet windowit is being echoed to the screen on the server side without any problems, but my if statement is never triggered.To be honest I have never had a problem with if statements in the past, and I have finished a full game using c++ and SDL. My game is a two play game called Thomthello and I am basically going to add the network code to my game so that people can play my game over the network.I need to add the exit ability so that I can test out my game and I am able to skip to the next move.Asimov500   ;Quote:it is being echoed to the screen on the server side without any problems, but my if statement is never triggered.Perhaps the contents of "r" are "exit\n"? The extra character would cause the comparison to be false.   ;  I am wondering if it is something to do with return character.As when the strings are output on my server sideThey are output like this.dfdfsdfsddsfddfdexitexitinstead of this exitexitfddexit and I am not adding \n so I amwondering if that could be the problem?Asimov500   ;Quote:Original post by Asimov500Of course I had to add #include <iostream.h> to make cout work.BZZT, wrong.  The C++ standard was proclaimed 12 years ago, there's not excuse for using non-standard headers.  Use <iostream> not <iostream.h>.Quote:Anyway to cut a long story short when I type exit in my telnet windowit is being echoed to the screen on the server side without any problems, but my if statement is never triggered.Sounds like yor "r" variable contains, say, a newline or some other invisible character ate the end.  Try using r.compare(0, 4, "exit") or something instead.;  Hi Bregma,Thanks your solution worked. As for the .h on iostream you are totally correct. I sometimes forget. I never had this problem with z80 machine code and Amospro LOL.I did try strippng the character off the end once and that didn't work, but your compare line did the trick.Now I have got the server to work I can start adding to my game :)ThanksAsimov500Here is my new code#include "Socket.h"#include <process.h>#include <string>#include <iostream>using namespace std;unsigned __stdcall Answer(void* a) {  Socket* s = (Socket*) a;cout << "Asimov Server Has Started\n";cout << "(Type 'exit' to end session)\n\n";  while (1) {    std::string r = s->ReceiveLine();    if (r.empty()) break;cout << r; if (r.compare(0, 4, "exit")==0) {  printf("Ending");  break; }    s->SendLine(r); //   MessageBox(NULL, r.c_str(), "Help", MB_OK);  }  delete s;  return 0;}int main(int argc, char* argv[]) {  SocketServer in(3000,5);  while (1) {    Socket* s=in.Accept();    unsigned ret;    _beginthreadex(0,0,Answer,(void*) s,0,&ret;  }  return 0;};  A question is: are the users of the ReceiveLine method ever interested in knowing of any trailing newline characters? It sounds to me like ReceiveLine could strip of any trailing newline characters, and thence relieve the client code from having to handle it.   