ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
well firstly using a cin the way you did is going to cause a world of pain to the user... cin is going to block the execution of the program. In other words you wont get any text from IRC during the cin stage. Another problem is the fact that you need to terminate strings sent to the server with "\r\n"
I dont see where you are getting any input from the server either or even where you are going to log onto the server.
as I said, I know nothing about programming IRC clients, as of now all I've made it to do is send information to the server. what do I do to actually connect to the server?
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define SERVER "irc.efnet.nl"
#define PORT 6667
#define IDENT "ircbot"
#define NAME "Stack"
#define NICK "Stack--"
#define CHANNEL "#linuxworld"
#define MAX 100
void Send(int rsock,char* sendbuffer,struct sockaddr_in ip);
int main()
{
int rsock,bytes,i;
char buffer[MAX];
char sendbuffer[MAX];
struct sockaddr_in ip;
struct hostent *hp;
if((hp=gethostbyname(SERVER))==NULL)
{
printf("Error: gethostbyname()\n");
exit(1);
}
else
printf("Server resolved to %s\n",inet_ntoa(*((struct in_addr *)hp->h_addr)));
if ((rsock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Error: socket()\n");
exit(1);
}
ip.sin_family = AF_INET;
ip.sin_port = htons(PORT);
ip.sin_addr = *((struct in_addr *)hp->h_addr);
memset(&(ip.sin_zero), '\0', 8);
printf("Connecting to %s on port %d\n",SERVER,PORT);
if (connect(rsock, (struct sockaddr *)&ip,sizeof(struct sockaddr)) == -1)
{
printf("Error: connect()\n");
exit(1);
}
while((bytes=recv(rsock,buffer,MAX-1,0))!=-1)
{
buffer[bytes] = '\0';
printf("%s",buffer);
if(strcmp("NOTICE AUTH :*** Looking up your hostname...\r\n\0",buffer)==0)
{
sprintf(sendbuffer,"USER %s 0 %s :%s\r\n",IDENT,SERVER,NAME);
printf("Sending to %s : %s",SERVER,sendbuffer);
Send(rsock,sendbuffer,ip);
sprintf(sendbuffer,"NICK %s\r\n",NICK);
printf("Sending to %s : %s",SERVER,sendbuffer);
Send(rsock,sendbuffer,ip);
sprintf(sendbuffer,"JOIN %s\r\n",CHANNEL);
printf("Sending to %s : %s",SERVER,sendbuffer);
Send(rsock,sendbuffer,ip);
}
else if(strncmp(buffer,"PING",4)==0)
{
sprintf(sendbuffer,"PONG");
for(i=4;i<strlen(buffer)+1;i++)
sendbuffer[i]=buffer[i];
printf("Sending to %s : %s",SERVER,sendbuffer);
Send(rsock,sendbuffer,ip);
}
}
close(rsock);
return 0;
}
void Send(int rsock,char* sendbuffer,struct sockaddr_in ip)
{
int bytes;
if((bytes=sendto(rsock,sendbuffer,strlen(sendbuffer),0,(struct sockaddr *)&ip, sizeof(struct sockaddr))) == -1)
{
printf("Error: sendto()\n");
exit(1);
}
}
well anyways here is a little something to play with... a nice and simple little ircbot... it should explain to you though how to properly connect to the irc server and yes it is all in C and i apologize in advanced for the slightly hackish code. heh not to bad for 15 minutes though...
should I be getting a segmenation fault... what is a segmentation fault?
It is an error that occurs at runtime because your program is trying to access memory that was not allocated to it.
Anyways i have no clue how or why you get this error seeing as it compiles and runs perfectly on both my freebsd machine and a remote redhat box... try to remove "sstream.h" though...
Code:
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <iostream>
using std::cout;
using std::endl;
if your using g++ those should be the only headers you need...
Yeah, i don't get a Ident responce, I've checked other irc programs to see what it looked like... nothing. I've tried ircbot, but that doesn't work either.
If your under a *nix operating system you are going to have to enable an ident server (pidentd on redhat i believe)... This is to be expected, an irc bot will not have an ident responce unless you run an ident server.
Quote:
And how do I create a place for input that won't interupt the flow of the program?
Well there are a bunch of ways to do this you could rewrite it to use non blocking sockets or try to open a thread...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.