c - IRC 机器人故障

标签 c network-programming bots irc

我试图用 C 语言制作一个 IRC 机器人。当机器人试图连接到一个 IRC 服务器时,它进入了一个无限循环,在那里它什么也没收到。 我不确定这是因为我加入 IRC 服务器的过程格式不正确,还是我丢失了一些应该发送/接收的数据。

#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>


#define MAXSIZE 4096

void delay(int milliseconds)
{
    long pause;
    clock_t now;

    pause = milliseconds*(CLOCKS_PER_SEC/1000);//set delay using
    now = clock();
    while( now < pause )
        now = clock();
}

int send_data(int sockfd, char message[])
{
    send(sockfd, message, strlen(message), 0);
    printf("OUT: %s\n", message);
    return 1;
}

int recv_data(int sockfd, char *message)
{
    int n;
    n = recv(sockfd, message, MAXSIZE, 0);
    printf("IN: %s\n", message);
    return n;
}

int tcp_connect(int *sockfd, char server[], char port[])
{
    //declare variables
    struct addrinfo hints, *res;

    //zero out structures
    memset(&hints,0,sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    //query DNS server for IP address and port
    getaddrinfo(server,port,&hints,&res);

    //create socket for data transmission
    *sockfd = socket(res->ai_family,res->ai_socktype,0);
    if (*sockfd < 0)
    {
        printf("failure to create socket\n");
        return 0;
    }

    //connect to server side port using created socket
    if (connect(*sockfd, res->ai_addr, res->ai_addrlen)!= 0)
    {
        printf("failure to connect to port\n");
        return 0;
    }

    freeaddrinfo(res);
    return 1;
}

int irc_auth(int sockfd)
{
    //create and start clock
    clock_t start_t;
    start_t = clock();

    //seed RNG with clock output
    srand(start_t);

    //generate necessary variables
    char name[15] = "bot";
    char user[35] = "USER ";
    char nick[20] = "NICK ";
    char join[20] = "JOIN #randChat\r\n";
    int i,id;

    //generate random character for ID tag A-Z
    for(i=0; i<5; i++)
    {
        id = rand() % 91;
        if(id < 65)
        {
            while(id < 65)
            {
                id = rand() % 91;
            }
        }
        name[strlen(name)] = id;
    }
    //append return and null to string
    strcat(nick,name);
    strcat(nick,"\r\n");

    //append to finish creating USER IRC command
    strcat(user,name);
    strcat(user," 8 * :");
    strcat(user,name);
    strcat(user,"\r\n");

    //send data to server
    send_data(sockfd,user);
    delay(1000);
    send_data(sockfd,nick);
    delay(1000);
    send_data(sockfd,join);

    return 1;
}
int main (int argc, char *argv)
{
//variables
    int sockfd, n, flag;
    char *mesg_in = malloc(sizeof(char) * MAXSIZE); 
    char *pos;
    char nick[30];

    char *mesg_out = malloc(sizeof(char) * MAXSIZE);

    //connect to port 6667 of irc.freenode.org using tcp
    while(flag<1)
    {
        if(tcp_connect(&sockfd,"irc.freenode.org","6667") == 1)
        {
            flag = 1;
        }
    }   

    //IRC channel authentication
    irc_auth(sockfd);

    //command loop
    while(1)
    {
        mesg_in[0] = 0;// zero out message
        //memset(mesg_in,0,strlen(mesg_in));
        n = recv_data(sockfd,mesg_in);// pull message from channel

        if (n > 0)// check to see if it recieved a command
        {
            mesg_in[n] = 0;// set null at the end of recieved data

            //respond to ping commands from server
            if(strstr(mesg_in,"PING") != NULL)
            {
                mesg_out[0] = 0;// zero out message
                pos = strstr(mesg_in," ")+1;// point to data needed
                //append to out bound message
                sprintf(mesg_out,"PONG %s\r\n",pos);
                //send outbound message
                send_data(sockfd,mesg_out);
            }
        }
    }
}

我们将不胜感激任何帮助

最佳答案

无论可能存在什么其他问题,delay() 都是其中之一。你在这个测试程序中的函数等待两秒钟,然后同时打印 1 2 3,因为它只考虑从程序开始耗时,而不是从当前时刻开始的时间。

#include <stdio.h>
#include <time.h>

void delay(int milliseconds)
{
    long pause;
    clock_t now;
    pause = milliseconds*(CLOCKS_PER_SEC/1000);//set delay using
    now = clock();
    while( now < pause )
        now = clock();
}

int main (void)
{  
    delay(2000);
    printf("1\n");

    delay(2000);
    printf("2\n");

    delay(2000);
    printf("3\n");

    return 0;
}

此版本以两秒为间隔打印 1 2 3

#include <stdio.h>
#include <time.h>

void delay(clock_t milliseconds)
{
    clock_t elapsed, pause, stamp;
    stamp = clock();
    pause = milliseconds * CLOCKS_PER_SEC / 1000;
    while ((elapsed = clock() - stamp) < pause);
}

int main (void)
{  
    delay(2000);
    printf("1\n");

    delay(2000);
    printf("2\n");

    delay(2000);
    printf("3\n");

    return 0;
}

另请注意,在整数运算中,我先乘法再除法。

关于c - IRC 机器人故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368466/

相关文章:

c++ - 如何在Windows中提取目录属性?

c - C中转换符号的名称

java - 是否可以使用java套接字从客户端发送sslv2hello消息

c++ - Linux网络编程。我可以从什么开始?

.htaccess - Google 使用 BLEXBot 爬虫吗?

c - 错误 : invalid use of undefined type struct in C

c - 如何用 ASCII 码移动终端光标?

java - Socket Exception - 如何从 Socket Exception 对象获取原始套接字

redis - 如何在聊天机器人中管理上下文/状态?

raspberry-pi - 如何在 Raspberry Pi 上运行 robocorp 或 robotsframework 文件 (.robot)?