Произошла ошибка во время JOIN и USER, она говорит мне, что мне нужно "сначала зарегистрироваться". Три: PASS, 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.