IRC套接字连接问题

时间:2011-07-11 15:22:18

标签: c++ sockets connection irc

在JOIN和USER期间发生错误,它告诉我需要“先注册”。三:通过,NICK正常工作(可能)。知道这怎么可能?我遵循了相关的RFC http://tools.ietf.org/html/rfc1459#section-4.1

代码:

#include <iostream>
#include <winsock2.h>
#include <string>
#include <cstring>
#include <fstream>

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

using namespace std;
unsigned long resolveHost(const string &host){
    LPHOSTENT entryHost = gethostbyname(host.c_str());

    if(!entryHost){
        unsigned int addr = inet_addr(host.c_str());
        entryHost = gethostbyaddr((char*)&addr, 4, AF_INET);
        if(!entryHost)  return 0;
    }
    return *((int*)*entryHost->h_addr_list);
}

int main(){
    WSADATA         wsaData;
    SOCKADDR_IN     saddr;
    SOCKET          sock;
    char            buffer[1024*8] = {"0"};
    string          adres_hosta = "irc.example.com";
    string          sbuffer;
    string          nick = "johnsmith";
    string          kanal = "#channel";
    string          password = "p@55w0rd";
    string          auth = "/AuthServ auth johnsmith p@55w0rd";
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    saddr.sin_addr.S_un.S_addr = resolveHost(adres_hosta);
    saddr.sin_port =  htons(6667);
    saddr.sin_family = AF_INET;
    if(connect(sock, (sockaddr*)&saddr, sizeof(sockaddr)) == SOCKET_ERROR){
        cout << "Nie udalo sie nawiazac polaczenia z " << adres_hosta << ".";
        return 0;
    }
    recv(sock, buffer, 1024*8, 0);
    cout << buffer << endl << endl << flush;
    /*
           1. Pass message
           2. Nick message
           3. User message
    */
    sbuffer ="PASS " + password + "\r\n";
    send(sock, sbuffer.c_str(), strlen(sbuffer.c_str()), 0);
    recv(sock, buffer, 1024*8, 0);
    cout << sbuffer.c_str() << endl << buffer << endl << endl;

    sbuffer = "NICK " + nick + "\r\n";
    send(sock, sbuffer.c_str(), strlen(sbuffer.c_str()), 0);
    recv(sock, buffer, 1024*8, 0);
    cout << sbuffer.c_str() << endl << buffer << endl << endl;

    sbuffer = "USER " + nick + " " + adres_hosta + " " + nick + " :" + nick + "\r\n";
    send(sock, sbuffer.c_str(), strlen(sbuffer.c_str()), 0);
    recv(sock, buffer, 1024*8, 0);
    cout << sbuffer.c_str() << endl << buffer << endl << endl;

    sbuffer = "JOIN " + kanal + "\r\n";
    send(sock, sbuffer.c_str(), strlen(sbuffer.c_str()), 0);
    recv(sock, buffer, 1024*8, 0);
    cout << sbuffer.c_str() << endl << buffer << endl << endl;

    while(true){
        recv(sock, buffer, 1024*8, 0);
        cout << buffer << endl << endl;
        if(buffer[0] == 'PING'){
            sbuffer = "PONG :" + adres_hosta + "\r\n";
            send(sock, sbuffer.c_str(), strlen(sbuffer.c_str()), 0);
            cout << sbuffer.c_str() << endl << buffer << endl << endl;
        }
    }
    return 1;
}

连接日志:

NOTICE AUTH :*** Looking up your hostname


PASS p@55w0rd

NOTICE AUTH :*** Checking Ident
NOTICE AUTH :*** Found your hostname


NICK johnsmith

PING :229860947
Checking Ident
NOTICE AUTH :*** Found your hostname


USER johnsmith johnSmith test :johnSmith

NOTICE AUTH :*** No ident response
ICE AUTH :*** Found your hostname


JOIN #channel

:server.example.com 451 johnSmith johnSmith :Register first.

1 个答案:

答案 0 :(得分:1)

我猜测,要加入该特定频道,您的昵称必须在NickServ或同等的IRC服务中注册。你可能需要注册你正在使用NickServ的昵称(我建议使用常规的IRC客户端来做到这一点,以便更容易)然后,从你的代码,PRIVMSG NickServ与某种auth命令“登录“在它允许你加入该频道之前。可能存在一种控制是否允许非授权用户加入的频道模式。

如果您还没有,我会建议您使用空的未注册频道进行测试。它有助于控制测试环境,并且您没有好奇的人试图在您开发软件时破坏您的软件。