Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows
Hi Code project,
 
I am coding a simple program which connects to a IRC channel and reads out messages. Please explain how i can do that ?
 
I can code a program which connects to a host but not to web servers or IRC servers o you see the problem.
Thanks in advance.
#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 << "Failed" << 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;
}
Posted 6-Dec-12 9:21am
Edited 6-Dec-12 9:22am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This looks promising: "Simple C++ IRC Bot Template"[^].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 320
1 OriginalGriff 235
2 Afzaal Ahmad Zeeshan 154
3 Sergey Alexandrovich Kryukov 147
4 BillWoodruff 135
0 OriginalGriff 7,510
1 DamithSL 5,494
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100