简体   繁体   中英

c++ irc bot only receives data once

I'm creating a simple IRC bot in C++ but I'm coming across some issues I can't seem to resolve. My code is based on Beej's Networking Guide .

Also have I used following code to try and fix my problem.

main.cpp snippet

        // as long we don't lose connection, keep receiving data
        while(true) {
            num_receives++;

            if(num_receives == 3) {
                snd("NICK q1w2\r\n");
                            snd("USER q1w2 0 * :q\r\n");
            }

            if(num_receives == 4) {
                snd("JOIN q1w2\r\n");
            }

            num_bytes = recv(s, buf, MAXDATASIZE-1, 0);
            buf[num_bytes]='\0';

            std::cout << num_receives << "\t" << buf;   

            if(num_bytes == -1) {
                            perror("ERROR receiving");
                            break;
                    } 
            if(num_bytes == 0) {
                            perror("DISCONNECTED");
                break;
                    }
        }

and the output is (first line is command to run the bot)

~$ ./bot irc.snt.utwente.nl 6667 

1   :irc.snt.utwente.nl 020 * :Please wait while we process your connection.
2   ERROR :Closing Link: [unknown@91.177.66.186] (Ping timeout)
DISCONNECTED: Network is unreachable

So, the problem is that it only receives data once. Even though it's placed in a while loop. I don't really see what's blocking it from looping or receiving. If I place a cout inside the while loop you can see it only loops once and a second time in the end when it disconnects.

Edit: I have it working. The commands send to the severs should be put outside the while loop. Because inside the loop it's waiting to be initialized. But as w don't receive anything we just lose connection because we didn't send anything either.

[correct] main.cpp snippet

        snd("NICK q1w2\r\n");
        snd("USER q1w2 0 * :q\r\n");
        snd("JOIN q1w2\r\n");

        // as long we don't lose connection, keep receiving data
        while(true) {
            num_receives++;

            num_bytes = recv(s, buf, MAXDATASIZE-1, 0);
            buf[num_bytes]='\0';

            std::cout << num_receives << "\t" << buf;   

            if(num_bytes == -1) {
                            perror("ERROR receiving");
                            break;
                    } 
            if(num_bytes == 0) {
                            perror("DISCONNECTED");
                break;
                    }
        }

You forgot to implement the IRC protocol.

Your code assumes that each call to the TCP connection's recv function will return exactly one IRC message. This assumption is totally and completely invalid. The TCP implementation has no idea what an IRC message is. You have to implement the IRC protocol and the IRC protocol's definition of a message if you want to count how many messages you've received.

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