Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ IRC Bot Help


ghost's Avatar
0 0

So yeah, my friend recommended a socket tutorial, and I, of course, went and learned it. So i'm trying to get the PING/PONG down for the intial connect. It compiles fine, but when I try to run it, it says theres an error, and it automatically shuts down. Now, i'm pretty much sure that its a error with the Ping/Pong (One, because its poorly written, and i'm not sure how to make it better, and Two, I made it connect to me using netcat, and everything else works fine if you don't ping it)

Here's the code…

#include <stdlib.h>
#include <iostream>
#include <winsock2.h>

int main()
{ 
    //Initialize Sockets API
    WSADATA wsaData;
    WSAStartup(MAKEWORD(1, 1), &wsaData);
    
    struct sockaddr_in sockstruct;
    int sockfd = socket(PF_INET, SOCK_STREAM, 0);
    
    sockstruct.sin_family = AF_INET;
    sockstruct.sin_port = htons(6667);
    sockstruct.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(&(sockstruct.sin_zero), '\0', 8);
    
    connect(sockfd, (struct sockaddr *)&sockstruct, sizeof(struct sockaddr));
    char *recvdata;
    int bytes_sent;
    
    char *senddata = "NICK Test\n";
    int len = strlen(senddata);
    bytes_sent = send(sockfd, senddata, len, 0);
    recv(sockfd, recvdata, len, 0);
    if (strncmp(recvdata,"PING",4) == 255)
          {
          char *pong = strstr(recvdata,":");
          sprintf(senddata,"PONG %s\n",pong);
          len = strlen(senddata);
          send(sockfd, senddata, len, 0);
          senddata = "USER z0mgh41 127.0.0.1 elite.phatlogic.com :h41z0r\n";
          len = strlen(senddata);
          bytes_sent = send(sockfd, senddata, len, 0);
          senddata = "JOIN #Test\n";
          len = strlen(senddata);
          bytes_sent = send(sockfd, senddata, len, 0);
}
else {
  senddata = "USER Test 127.0.0.1 elite.phatlogic.com :h41z0r\n";
    len = strlen(senddata);
    bytes_sent = send(sockfd, senddata, len, 0);
    senddata = "JOIN #Test\n";
    len = strlen(senddata);
    bytes_sent = send(sockfd, senddata, len, 0);   
}
    while (1)
    {
          char *recvdata;
          len = 255;
    }
}

Oh, and sorry about the ;) that are forced on there. Any help/explanation of the problem would be very welcome.

Thank you!


ghost's Avatar
0 0

First I suggest whether you're going to write this in C or C++, not both. After that, it'd help if you could explain the error you receive on shutdown and perhaps stick in some error checking to narrow down the problem or do some debugging before I look through your code.

Edit: On second thought… I glanced over your code and noticed wild pointers attempting to be assigned a value and incorrect setup in the connection procedure and the setting up of the sockaddr_in structure.

Edit 2: Your code just all around makes no sense and sucks. Perhaps you should look over the links I posted in your previous thread.

Cheers, ~T


ghost's Avatar
0 0

Fatigue wrote: Oh, and sorry about the ;) that are forced on there.

This isnt true, check the box that says Disable Smileys in this Post when you post.

@T-Metal-Risen a little harsh…not without warrant however

@fatigue Continue learning about c++ sockets, they are very useful. Here's a great site found within a minute of googling http://www.codeproject.com/useritems/beginningtcp_cpp.asp. In the meantime try to code something a little simpler before you try something like this.