views:

340

answers:

2

I have a question on sockets. I have this code:

while(bytes = recv(sClient, cClientMessage, 599, 0)){

This puts the message it recives into cClientMessage and the message is always "Message". How I made an if statement like if(cClientMessage == "Message"){//do func}. Now this code will not do the function I want. I think this is because it's not receiving the message right. Can someone help me?

+2  A: 

First there is a bug in the code you wrote:

while(bytes = recv(sClient, cClientMessage, 599, 0)){

This is wrong because recv will return non zero if there is a socket error and your code will lead to an infinite loop. In particular you want to check for > 0

char cClientMessage[599];
while((bytes = recv(sClient, cClientMessage, sizeof(cClientMessage), 0)) > 0)
{
  if(strlen("Message") == bytes && !strncmp("Message", cClientMessage, bytes))
  {
    //cClientMesssage contains "Message"
  }
} 

if(bytes == 0)
{
  //socket was gracefully closed
}
else if(bytes < 0)
{
  //socket error occurred
}

The problem with what you did: cClientMessage == "Message" is that if you compare a char* to a string literal, or a char[] to a string literal, then you will be comparing the pointer addresses and not the actual content.

Brian R. Bondy
The memset is wasteful. Just say cClientMessage[bytes] = 0; bytes will always range from 0 to 598 based on the parameters to recv.
jmucchiello
You should use strncmp instead of strcmp. Also, you're not checking if the message is shorter than 8 characters -- you might get false positives if you received the 4 bytes "Mess", e.g., and your buffer already head "...age\0" in it.
Adam Rosenfield
Thanks adam i took care of that too
Brian R. Bondy
joe: with strcmp you need the memset on the whole buffer as I had it. I changed to strncmp though to avoid the memset altogether.
Brian R. Bondy
+4  A: 

Try:

if( strcmp( cClientMessage, "Message")) == 0 ) {
   // do something
}

Edit, following suggestion from strager:

A better solution, which does not depend on the received data being null terminated is to use memcmp:

if( memcmp( cClientMessage, "Message", strlen( "Message") )) == 0 ) {
   // do something
}
anon
I'm thinking more memcpy if the \0 is not present, but I guess this may work.
strager
(I wonder who -1'd this...)
strager
memcmp, rather than memcpy I think?
anon
Er, woops. Thanks for the correction, @Neil.
strager
+1 for the clear exposition of the asked question. Bondy's point about the socket usage is important, however.
RBerteig
The memcmp is unnecessary. "Message" is null terminated so the strcmp will never test more than 8 characters of data. If you know cClientMessage is guarenteed to longer than 8 characters, there is no need for memcmp. (Premature optimization, I know.)
jmucchiello
I think strncmp would be preferable to memcmp. Also, you're not checking if the message is shorter than 8 characters -- you might get false positives if you received the 4 bytes "Mess", e.g., and your buffer already head "...age\0" in it.
Adam Rosenfield
This has a bug in it if you receive something that is a superset of what you are comparing to. Please see my solution.
Brian R. Bondy