954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Winsock send interrupted by whitespaces

Hey everyone,
i've been trying to send some words seperated by spaces to a server and only the first word arrives.
The connection stuff is all fine but i have absolutely no idea whatsoever how to make it send more than one word at once.
This is what i'm talking about:

std::string foo = "i am a messager\r\n";
send(socket, foo._cstr(), foo.length(), 0);

all that arrives at the server is "i"

also tried size() instead of length but that makes no difference.

I'd be really gratefull for any hint.

The hole thing runs on windows btw.

replic
Light Poster
47 posts since Nov 2008
Reputation Points: 27
Solved Threads: 6
 

What about looking at the return result of send() to see how much you actually sent?
- assuming that send() always sends the whole thing in one call is a bug.

What about the recv() end as well?
Messages can be fragmented in transit, and it's your job to reassemble them.


hole == whole

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Well here is the strange part about it all...
the whole thing is part of an IRC client and if i send

std::string foo = "JOIN #channel\r\n";
send(socket, foo.c_str(), foo.length(), 0);

it works fine but if i send

std::string foo = "PRIVMSG #channel some text\r\n";
send(socket, foo.c_str(), foo.length(), 0);

only "some" is received.

replic
Light Poster
47 posts since Nov 2008
Reputation Points: 27
Solved Threads: 6
 

So just because your luck ran out, you decide that there is a problem with the universe?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

No need to get sarcastic now. I'm just saying that i don't have a clue why this is not working. According to the RFC1459 there are no special things you need to do in order to send stuff using PRIVMSG.
I never said a damn thing about the universe nor am i whining about this.
I am merely asking for a hint from people who are more experienced than me.

replic
Light Poster
47 posts since Nov 2008
Reputation Points: 27
Solved Threads: 6
 

IRC delimits arguments by whitespace, the error is in your string formatting. For final arguments that contain spaces (multiple tokens), prefix the token list with a colon.

"PRIVMSG foo :This is a message\r\n"

Corwinoid
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

Yep! it works thanks a bunch.

replic
Light Poster
47 posts since Nov 2008
Reputation Points: 27
Solved Threads: 6
 

The RFCs don't mean squat if you don't know how to write network code to begin with.
http://beej.us/guide/bgnet/

Here's an example of what you need to be doing instead of your "fire and pray" approach.

int mySend ( int socket, std::string &s ) {
  const char *p = s.c_str();
  size_t      len = s.length();
  ssize_t     n;
  while ( len > 0 &&
          (n = send( socket, p, len, 0 )) > 0 ) {
    // successfully sent some (possibly all) of the message
    // if it was partially successful, advance down the string
    // to the bit which didn't get sent, and try again
    len -= n;
    p   += n;
    n = 1;
  }
  if ( n == 0 ) {
    // a send call failed to make any progress through the data
    // or perhaps len itself was 0 to start with.
  }
  if ( n < 0 ) {
    // look at errno to determine what went wrong
    // some values like EAGAIN and EINTR may be worth a 2nd attempt
  }
  return n;
}


Elsewhere, you do

if ( mySend(socket, foo) > 0 ) {
  // more stuff
}

// or
if ( mySend(socket, foo) <= 0 ) {
  // panic and die
}


Now write something similar for recv(), which waits around until it has filled a buffer ending with \r\n (as defined by your RFC).

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: