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.

Recommended Answers

All 7 Replies

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

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.

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

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.

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"

Yep! it works thanks a bunch.

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.