Thread: c irc bot

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    c irc bot

    Uhm, just wondering what's wrong with the code:
    Code:
    #include <stdio.h>
    #include <winsock.h>
    
    int main(int argc, char *argv[])
    {
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
            return 0;
    
        SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (hSocket == INVALID_SOCKET)
        {
            WSACleanup();
            return 0;
        }
        
        SOCKADDR_IN sockAddr;
        sockAddr.sin_port = htons(6667);
        sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        sockAddr.sin_family = AF_INET;
        
        if (connect(hSocket, (SOCKADDR *)&sockAddr, sizeof(sockAddr)) != 0)
        {
            WSACleanup();
            return 0;
        }
        
        char buffer[260], temp[260];
        
        while (1)
        {
            int bytesRcv = recv(hSocket, buffer, sizeof(buffer), 0);
            
            if (bytesRcv != 0 || bytesRcv != SOCKET_ERROR)
            {
                printf("%s", buffer);
                
                if (strstr(buffer, "No ident response") != NULL)
                {
                    sprintf(temp, "USER something something something :something");
                    send(hSocket, temp, strlen(temp), 0);
    
                    sprintf(temp, "NICK something");
                    send(hSocket, temp, strlen(temp), 0);
                }
            } else break;
        }
        
        return 0;
    }
    Btw, I have my own ircd that I downloaded..
    After receiving the message "No ident response", it should send a "user" and "nick" message and then after that the server should send me back a "ping" message..
    Thanks in advance..

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by niqo View Post
    Uhm, just wondering what's wrong with the code:
    Isn't it your job to tell us what's wrong with the code, and our job to tell you how to fix it? I'm confused now.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    All IRC commands must be terminated with \r\n.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Yupz. . Got it working a while ago.. I added the \r\n. Hehe Anyways, am I writing the code correctly? Cause I noticed in one source code that it did a memset before putting another value at my variable buffer or temp.. Do i need to add that?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by niqo View Post
    Yupz. . Got it working a while ago.. I added the \r\n. Hehe Anyways, am I writing the code correctly? Cause I noticed in one source code that it did a memset before putting another value at my variable buffer or temp.. Do i need to add that?
    if you check the return value of the recv and work according to it - you do not need the memset beforehead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    should not this be posted in network section?

    anyway. does anybody know why this happened:

    i found my ip by visiting a site (modem based 64kbps net)
    then wrote a simple server and asked my brother to connect to me using winxp telnet!
    but he could not connect! why??

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you'll want to read this thoroughly.
    http://cboard.cprogramming.com/showthread.php?t=41926

    Edit:
    And moved, as noted by manav.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design guidelines advice - IRC Bot class
    By IceDane in forum C# Programming
    Replies: 2
    Last Post: 12-05-2008, 01:21 AM
  2. Need some help with making irc bot
    By kazz in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 11:47 AM
  3. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  4. Whats wrong with my IRC Bot??
    By peradox in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-23-2005, 05:16 PM