send or recv breaking char* on whitespace??

the strings i'm getting back from recv aren't what I thought they would be...
On the client side, I gather input to send over a socket with this simple code:
1
2
3
		printf("Type your command: ");
		scanf("%s", buf);
		send(s, buf, strlen(buf), 0);

And then on the server side, I want to perform some operations depending on the message. Like if the user types "login hisUsername hisPass" I want to run a function that checks out the username and pass against registered users. So on the server side I recv the messages from the client like so:
[core] int len = recv( s, buf, MAX_LINE, 0 );
buf[len] = 0;
gotmessage(buf,s);[/code]
Easy enough. Gotmessage goes through and parses the incoming string, which correctly identifies 'login', but it also prints out the entire string passed to it in buf. and whenever i type anything with a space, it prints it out as two different lines (meaning recv returned once with the first piece until the whitespace, then again at the next whitespace, etc).

I tried to put MSG_WAITALL in the flags, maybe I didn't do it right, but that made recv return instantly when there wasn't even anything coming in on the socket..

I put a breakpoint on the recv loop on the server, then sent it the message "AAA BBB CCC DDD" from the client. The first time it went through it got only "AAA" as the message. Then it got "BBBCCCDDD" and looks like it ignored whitespace. How do I get it to NOT ignore whitespace? And to wait on the whole thing without spasing out? Is send doing this or is recv?
FYI, recv returns a character at a time.

What kind of loop are you using for getting data from recv()?
no it doesn't... so far when i send "AAA BBB CCC DDD" and I don't have any break points (so nothing gets in the queue it's all just spat out) it returns "AAA" then "BBB" then "CCC" then "DDD". If I put a break point in after it gets AAA, even if i just immediately continue regular operation, that gives it enough time to let "BBB CCC DDD" stack up on the queue, and it would print out "AAA" and then "BBBCCCDDD" with no whitespace. I had a typo in my post above, but i put the code for recv in there. It is:

1
2
3
int len = recv( s, buf, MAX_LINE, 0 );
buf[len] = 0;
gotmessage(buf,s);


Where at this point all got message does is printf("%s",buf);. And I need buf to be "AAA BBB CCC DDD" not "BBBCCCDDD" and not each word split up! :(
Well, buf[len] = 0 is illegal code, you cannot dynamically allocate an array like that...it might be why gotmessage() is screwing up. Also, what is the code for gotmessage(), it might be how you are handling the string that causes the problems. Also, recv() should be hanging until is gets MAX_LINE characters...it shouldn't be returning immediately.

What you should do is make buffer an array of two characters. Then, looping until you get a new line character, append one character to a string...then you can split that string however you want.
Last edited on
no the buf[len] is fine. You want to terminate the string with null char (0 or "\0" same thing) so that it works like a string.

turns out it's something with scanf before sending on the client side. If I use "gets" instead of "scanf" it works fine. not sure why... Thats all I changed and it works fine now.
Ahhhh, ok, you were setting '\0' AT len, not trying to make a new array...XP

Also, another thing you might want to consider, if you are using Telnet to connect, it sends the characters as they are input...so you can't just use recv() to get them all. Telnet sends a backspace as '\b', so you would have to handle that somehow. I don't know how you would be able to deal with that using that code.
Topic archived. No new replies allowed.