连接到IRC服务器时出现问题

Issue connecting to IRC Server

本文关键字:问题 服务器 IRC 连接      更新时间:2023-10-16

最近一直在创建一个IRC机器人程序,虽然它似乎连接了&与大多数IRC服务器一起工作时,Twitch IRC服务器(IRC.Twitch.tv)似乎有问题

当连接到另一个服务器时,数据被接收&发送时没有任何问题,但是使用twitch连接,我无法发送或接收数据。

初始化:

Connection::Connection(char* server, int port, std::string nick, std::string pass)
{
    fServer = server;
    fPort = port;
    fNick = nick;
    fPass = pass;
    fChanCount = 0;
    clear_buffer();
    fSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    cout << "Attempting to resolve host: " << fServer << endl;
    fHost = resolve_host(fServer);
    fSaddr.sin_addr.S_un.S_addr = fHost;
    fSaddr.sin_port = htons(fPort);
    fSaddr.sin_family = AF_INET;
    cout << "Resolved to: " << fHost << endl;
}

开放连接:

int Connection::Connect()
{
    if (connect(fSock, (sockaddr*)&fSaddr, sizeof(fSaddr)) == SOCKET_ERROR)
    {
        cout << "Can't connect to " << fServer << endl;
        return 0;
    }
    recv(fSock, fBuffer, 1024 * 8, 0);
    cout << fBuffer << endl << endl << flush;
    send_message("PASS " + fPass);
    send_message("NICK " + fNick);
    send_message("USER " + fNick + " 0 * :" + fNick);
    return 1;
}

清除缓冲区:

void Connection::clear_buffer()
{
    memset(fBuffer, 0, sizeof(fBuffer));
}

接收:

string Connection::receive_message()
{
    clear_buffer();
    recv(fSock, fBuffer, 1024 * 8, 0);
    return fBuffer;
}

完全弄不清楚是什么原因导致了这种情况,如果需要,可以提供更多细节。

您的USER消息看起来不像RFC2812规范所要求的那样。

3.1.3 User message
      Command: USER
   Parameters: <user> <mode> <unused> <realname>
   The USER command is used at the beginning of connection to specify
   the username, hostname and realname of a new user.
   The <mode> parameter should be a numeric, and can be used to
   automatically set user modes when registering with the server.  This
   parameter is a bitmask, with only 2 bits having any signification: if
   the bit 2 is set, the user mode 'w' will be set and if the bit 3 is
   set, the user mode 'i' will be set.  (See Section 3.1.5 "User
   Modes").
   The <realname> may contain space characters.
   Numeric Replies:
           ERR_NEEDMOREPARAMS              ERR_ALREADYREGISTRED
   Example:
   USER guest 0 * :Ronnie Reagan   ; User registering themselves with a
                                   username of "guest" and real name
                                   "Ronnie Reagan".
   USER guest 8 * :Ronnie Reagan   ; User registering themselves with a
                                   username of "guest" and real name
                                   "Ronnie Reagan", and asking to be set
                                   invisible.

也许这就是问题的原因?

否则,我发现一些服务器需要PASS消息中的密码,即使服务器没有配置为需要密码。在这些情况下,我发送:

PASS none