我想在C++中編寫一個irc bot,並且已經考慮到連接和設置我的暱稱以及身份,ping也在運行。我遇到的唯一問題是加入一個頻道。服務器繼續發送回「JOIN:未知的命令」我相信下面的線是最相關的:爲什麼irc發送「命令未知」?
#define JOIN "JOIN #test\r\n"
send(IrcSocket, JOIN, strlen(JOIN),0);
上關的機會,沒有任何東西可以從我包括這可能是有點的entrie程序來解決一個眼睛。
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <string>
#pragma comment(lib, "Ws2_32.lib")
int main()
{
WSADATA wsaData;
int iresult=WSAStartup(MAKEWORD(2,2),&wsaData);
if(iresult!=0)
{
printf("WSAStartup Failed: %d \n", iresult);
return 1;
}
struct addrinfo *result=NULL, *ptr=NULL, hints;
ZeroMemory(&hints,sizeof(hints));
hints.ai_family=AF_INET;
hints.ai_socktype=SOCK_STREAM;
hints.ai_protocol=IPPROTO_TCP;
#define DEFAULT_PORT "6667"
#define DEFAULT_IP "192.168.0.10"
iresult = getaddrinfo(DEFAULT_IP,DEFAULT_PORT,&hints,&result);
if(iresult!=0)
{
printf("getaddrinfo failed: %d\n", iresult);
WSACleanup();
return 1;
}
SOCKET IrcSocket= INVALID_SOCKET;
ptr=result;
IrcSocket=socket(ptr->ai_family, ptr->ai_socktype,ptr->ai_protocol);
if (IrcSocket == INVALID_SOCKET)
{
printf("Error at socket(): %d \n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
iresult=connect(IrcSocket,ptr->ai_addr,ptr->ai_addrlen);
if(iresult==SOCKET_ERROR)
{
closesocket(IrcSocket);
IrcSocket=INVALID_SOCKET;
freeaddrinfo(result);
}
if(IrcSocket == INVALID_SOCKET)
{
printf("Unable to connect to irc server!");
WSACleanup();
return 1;
}
std::cout<<("connection made\n");
#define NICK "NICK bottester\r\n"
#define IDENT "USER bottester 0 0 bottester\r\n"
#define JOIN "JOIN #test\r\n"
char stringbuffer[256];
iresult=send(IrcSocket,NICK, strlen(NICK),0);
if(iresult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(IrcSocket);
WSACleanup();
std::cin.get();
return 1;
}
iresult=send(IrcSocket, IDENT, strlen(IDENT),0);
if(iresult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(IrcSocket);
WSACleanup();
std::cin.get();
return 1;
}
std::cout<<"entering ping loop\n";
char pingnumber[128];
char pinganswer[17]="PONG :";
#define PRMSG "PRIVMSG test :TESTBOT IS HERE!\r\n"
for(;;)
{
memset(stringbuffer,0,256);
recv(IrcSocket,stringbuffer,sizeof(stringbuffer),0);
for(int j=0;j<strlen(stringbuffer);j++)
{
std::cout<<j<<stringbuffer[j]<<"\n";
if(stringbuffer[j]=='P' && stringbuffer[j+1]=='I' && stringbuffer[j+2]=='N' && stringbuffer[j+3]=='G')
{
memset(pingnumber,0,10);
std::memcpy(pingnumber, stringbuffer+j+6,10);
std::memcpy(pinganswer+6,pingnumber,10);
std::memcpy(pinganswer+16, "\r\n",strlen("\r\n"));
send(IrcSocket,pinganswer,strlen(pinganswer),0);
std::cout<< "PINGED<"<<pingnumber<<"\n";
iresult=send(IrcSocket, JOIN, strlen(JOIN),0);
std::cout<< "JOINED\n";
send(IrcSocket, PRMSG, strlen(PRMSG),0);
}
}
}
}
連接命令不是以'/'開始的嗎? – Petesh 2013-04-07 22:20:28
當你使用客戶端是的,當它只是協議,我不相信如此。 – 2013-04-07 23:42:18
在加入之前,您不會處理任何回覆 - 之前的某些內容可能會失敗,例如USER或NICK請求。 – Petesh 2013-04-08 10:15:53