问题连接到IRC服务器

时间:2015-04-05 13:47:31

标签: c++ irc twitch

最近一直致力于创建一个IRC机器人,虽然它似乎连接&与大多数IRC服务器一起使用时,Twitch IRC服务器(irc.twitch.tv)似乎存在问题。

当连接到另一台服务器时,会收到数据&发送没有任何问题,但通过抽搐连接,我无法发送或接收数据。

初​​始化:

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;
}

如果需要,可以提供更多细节。

。完全难以理解可能导致此问题的原因。

1 个答案:

答案 0 :(得分:0)

您的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