2015-04-05 172 views
0

最近一直在努力创建IRC bot,虽然它似乎连接&与大多数IRC服务器一起工作,但似乎与Twitch IRC服务器(irc.twitch.tv)有关。连接到IRC服务器的问题

当连接到另一台服务器时,收到的数据&没有发出任何问题,但是使用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; 
} 

如果需要,可能会造成这种情况,可能会提供更多细节。

+0

'fPass'是否为空? IIRC一些IRC服务器需要'PASS'来包含一些东西,即使服务器不需要密码。在这种情况下,我已经输入“none”作为密码。 – Galik 2015-04-05 14:12:49

+0

我以与其他一切相同的方式通过密码。 – user3815037 2015-04-05 15:22:23

+0

您使用的是真实密码还是OAuth令牌? – szatmary 2015-04-06 15:18:35

回答

0

你的用户信息看起来并不像什么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 
+0

我通过相同的方式为服务器/端口/用户的密码,所以我不认为这是问题。我也尝试改变USER消息以符合RFC2812标准,但仍然没有成功。 – user3815037 2015-04-05 15:27:44

+0

@ user3815037贵'clear_buffer()'函数填充零的缓冲器?你是否也考虑到行结尾是LFCR(“\ r \ n”)? – Galik 2015-04-05 15:47:23

+0

clear_buffer()只是调用缓存memset的(),并用零填充它。至于发送消息,它会在最后附加“\ r \ n”。它似乎可以在其他IRC服务器上正常工作,而不是irc.twitch.tv。 – user3815037 2015-04-05 23:49:30