2015-04-05 148 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