简体   繁体   中英

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());
        closesocket(bobSocket);
        WSACleanup();
        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());
 Send(b);
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM