newuser9282928292 newuser9282928292 - 1 year ago 191
C++ Question

WinSock Send Function not sending

so I'm writing an IRC bot in C++, and I wrote my own send function

int Send(char* MessageToSend) {
initResult = send(bobSocket, MessageToSend, (int)strlen(MessageToSend), 0);
if (initResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
return 1;

and whenever I use it in my sendmessage function (which loop checks for messages and then runs all the input to send) kind of like this

while(1 > 0) {
string b;
cin >> b;
char* INPUT = const_cast<char*>(b.c_str());

but when I put PRIVMSG ##channel :test into the console, I don't see anything on the other client, so, can anybody like help? I'm fairly new to stackoverflow, and C++, so any feedback is greatly appreciated.

Answer Source

If send() returns 7, 9, 7 for PRIVMSG ##channel :hello, it means you are sending that one line in three different sends, and you're losing the internal spaces, and sending the trailing newline. So the problem may be that you're parsing the input incorrectly. What you are actually sending is:

PRIVMSG (7 bytes)
##channel (9 bytes)
:hello\n (7 bytes)

and note again that you have lost the internal spaces.

I believe you should be reading lines from the console, not words.

And note that I am starting this answer with 'if', as your responses to comments have been far from clear.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download