Hey everyone I am back with a question! woo! haha Well I have been learning network sockets in C and I have tried to create an IRC bot for iadnah's irc server/channel. the problem is that It fails to work properly even though it compiles with out a hitch. when I run the bot it goes straight to command prompt or terminal instead of the bot setting the nick and joining the channel. This bot is just a test bot to test my skills. Once I get it up and running I will design a new permenate bot to take its place.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT "6667"
int main(int argc, char *argv[])
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char *botnick = "USER testbot";
char *botnick2 = "NICK testbot";
char *channel = "JOIN #uplinklounge";
if (argc != 2){
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen == -1)) {
close(sockfd);
perror("client: connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
return 2;
}
send(sockfd,&botnick,strlen(botnick),0);
send(sockfd,&botnick2,strlen(botnick2),0);
send(sockfd,&channel,strlen(channel),0);
close(sockfd);
return 0;
}