我试图在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;
}

此版本每隔两秒打印一次
#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 bot故障,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41368466/

10-12 21:43