06 May, 2008, Impacatus wrote in the 1st comment:
Votes: 0
Hello everyone. I recently developed an interest in developing mud-like applications. I have very little experience with programming, but I'm learning. I've read beej's guide and I'm starting to understand how sockets work. I realize that the general advice is to start by modifying an existing codebase, but I find I have trouble dissecting a program if it's too big and complex. Therefore, I'm starting with the simplest I could find, socketmud, and the c++ version because that's what I'm most familiar with.

I'm trying to add commands to the socketmud base, but I'm really stuck for some reason. Here's the section I'm working with (part of socketmud.cpp):
// echo everything that each socket has sent to us
for (iSocket = socketList.begin(); iSocket != socketList.end(); ) {
pSocket = *iSocket++;

pSocket->Write(pSocket->GetInBuffer());
pSocket->ClrInBuffer();
}


I try adding an if statement, so that if the user inputs "Hello", the server responds with "World" (Note: I'm typing these just now, not copy/pasting.):
// echo everything that each socket has sent to us
for (iSocket = socketList.begin(); iSocket != socketList.end(); ) {
pSocket = *iSocket++;

if (pSocket->GetInBuffer() == "Hello") {
pSocket->Write("World");
}

pSocket->Write(pSocket->GetInBuffer());
pSocket->ClrInBuffer();
}


However, when I try compiling/running it, telnetting in, and sending "Hello", it just echoes it without sending "World". I've also tried:
if (pSocket->GetInBuffer().compare("Hello"==0) {
pSocket->Write("World");
}


and
if (pSocket->GetInBuffer().compare("Hello\0"==0) {
pSocket->Write("World");
}


Those doesn't work either. What am I doing wrong? Why doesn't the input match? Thanks in advance for any help.
06 May, 2008, Davion wrote in the 2nd comment:
Votes: 0
It seems Hello and Hello\r\n aren't the same things! I got it to work using

if(pSocket->GetInBuffer().compare("Hello\r\n") == 0 )
pSocket->Write("World");


Might want to trim the string immediately after it's read or just don't add the \r\n to begin with.
06 May, 2008, David Haley wrote in the 3rd comment:
Votes: 0
Also note that you don't want the == 0 to be inside the parameter list of "compare" – you don't want to compare GetInBuffer() with ("Hello" == 0), you want to see if the comparison of GetInBuffer() with "Hello" is 0.
07 May, 2008, Impacatus wrote in the 4th comment:
Votes: 0
Davion said:
It seems Hello and Hello\r\n aren't the same things! I got it to work using

if(pSocket->GetInBuffer().compare("Hello\r\n") == 0 )
pSocket->Write("World");


Might want to trim the string immediately after it's read or just don't add the \r\n to begin with.


Tried adding \r\n, still didn't work I'm afraid.

Edit: I just learned that in my OS, \n is the equivalent of \r\n. I tried it like that and it worked, thanks.

Quote
Also note that you don't want the == 0 to be inside the parameter list of "compare" – you don't want to compare GetInBuffer() with ("Hello" == 0), you want to see if the comparison of GetInBuffer() with "Hello" is 0.

As I said, I was typing that from memory. That error was not present in the original.

Thanks to both of you for your help. I figured out I might be able to use the string find function to achieve the same thing, so I'm going to experiment with that instead. Initial tests look promising. Thanks again.
0.0/4