简体   繁体   中英

c++ irc server doesnt send ping

I trying to make a simple irc client with c++. i am sending PASS, NICK and USER messages but server doesnt send me PING. I cant register...

this is the code:

#include <iostream>
#include <string>
#include <WS2tcpip.h>

#pragma comment(lib,"ws2_32.lib")


using namespace std;
string ipadress = "91.217.189.58";
int port = 6667;
WSADATA ws_data;
SOCKET Skt;
int main()
{
    int ws_result = WSAStartup(MAKEWORD(2, 2), &ws_data);
    if (ws_result != 0)
        cout << "socket cannot be initialized\n";
    else
        cout << "Soket initialized!\n";

    Skt = socket(AF_INET, SOCK_STREAM, 0);

    if (Skt == INVALID_SOCKET)
        cout << "socket not created\n";
    else
        cout << "Socket created!\n";

    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(6667);
    inet_pton(AF_INET, ipadress.c_str(), &hint.sin_addr);

    int connection_result = connect(Skt, (sockaddr*)&hint, sizeof(hint));

    if (connection_result == SOCKET_ERROR)
        cout << "Socket could not connect\n";
    else
        cout << "Socket Connected!\n";
    string channel = "JOIN #dikes\r\n";
        string Pass = "PASS PASSRE";
        string user = "USER guest 0 * :IRCbot\r\n";
        string nick = "NICK botzzz\r\n";

        char buffer[4096];//buffer to recieve messages from irc server

        send(Skt, Pass.c_str(), Pass.size(), 0);

        send(Skt, nick.c_str(), nick.size() , 0);

        send(Skt, user.c_str(), user.size(), 0);

        while (true)
        {
            string Pong = "PONG";

            ZeroMemory(buffer, 4096);
            int bytes_recieved = recv(Skt, buffer, 4096, 0);
            string msg = string(buffer, 0, bytes_recieved);
            cout << msg;
            if (msg == "PING")
            {
                send(Skt, Pong.c_str(), Pong.size() + 1, 0);
            }
            else if (msg == "001")
            {
                send(Skt, channel.c_str(), channel.size(), 0);

            }
        }

this is the output of this code:

Soket initialized!
Socket created!
Socket Connected!
:irc.portlane.se 020 * :Please wait while we process your connection.
ERROR :Closing Link: [unknown@176.43.204.170] (Ping timeout)
  1. Your checks don't account for \\r\\n

  2. Your checks don't account for the nick parameter on the PING command

  3. Your PASS command isn't terminated by a \\r\\n

  4. Your PONG response isn't terminated by a \\r\\n

  5. You've assumed a one-to-one mapping of "received data" and "lines". This is not guaranteed. It is quite possible (likely, even) that a single call to recv fills the buffer with data from zero to many complete commands/messages, possibly followed by an incomplete message!

    TCP/IP doesn't know about the IRC protocol; it doesn't care about its concept of a "command" and it does not break up packets into those itemised pieces for you. You have to do that. TCP/IP just streams bytes at you .

    You need to add the received bytes to a secondary buffer when they are received, then iteratively parse that buffer to extract any complete lines that have become available. (Do this properly and it'll also take care of #1)

(However, I would still expect to see the ping request in your output, so something else must also be wrong.)

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