LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2003, 09:29 AM   #1
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Rep: Reputation: 15
Talking IRC Client of Mine


Code:
// Socket-client.cpp
// drdroid

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <iostream.h>

//Write text to the socket given by file descriptor SOCKET_FD.

void write_text(int socket_fd, const char* text)
{
  // Write the number of bytes in the string, including NUL-termination

  int length = strlen(text)+1;
  write(socket_fd, &length, sizeof(length));
  

  // Write the string.
  write(socket_fd, text, length);
}

int main(int argc, char* const argv[])
{
  const char* const socket_name = argv[1];
  const char* const message = argv[2];
  int socket_fd;
  struct sockaddr_in name;
  struct hostent* hostinfo;


  // Create the socket.

  socket_fd=socket(PF_INET, SOCK_STREAM, 0);


  // Store the server's name in the socket address.

  name.sin_family=AF_INET;
  hostinfo=gethostbyname(socket_name);

  if(hostinfo==NULL)
    {
      cout << "Error Finding Host!" << endl;
      return 1;
    }

  else
    {
      //Find Host Info: Port etc.

      name.sin_addr=*((struct in_addr*)hostinfo->h_addr);
      name.sin_port=htons(6667);

      
      //Connect to the server
      
      if(connect(socket_fd, (struct sockaddr *)&name, sizeof(struct sockaddr_in)) == -1)
	{
	  perror("connect");
	  cout << "Error Connecting!" << endl;
	  return 1;
	}
      
      else
	{
	  //Write the text on the command line to the socket.
	  
	  char text[10];
	  while(strcmp(text,"q"))
	    {
	      cin >> text;
	      write_text(socket_fd, text);
	    }
	  close(socket_fd);
	}
    }
  return 0;
}
Should that even work at all? I based it on my other socket work, it is very glitched up.

Last edited by drdroid; 11-15-2003 at 11:27 AM.
 
Old 11-15-2003, 12:05 PM   #2
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
I have no idea of how to test this so any suggestiions or answers would be great.
 
Old 11-15-2003, 12:07 PM   #3
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Quote:
Should that even work at all?
nope...
 
Old 11-15-2003, 12:28 PM   #4
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
can you be more specific, there wasn't anything about irc in my book(advanced linux programming by codesourcery)
 
Old 11-15-2003, 12:37 PM   #5
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
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.
 
Old 11-15-2003, 01:18 PM   #6
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
...

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?
 
Old 11-15-2003, 01:26 PM   #7
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Code:
#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...

Last edited by Stack; 11-16-2003 at 01:04 AM.
 
Old 11-15-2003, 01:57 PM   #8
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
thank you sooo much, you have no idea how helpful this is. Thanks again, i'm going to print this out, break it apart, and examine it.
 
Old 11-15-2003, 10:13 PM   #9
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
should I be getting a segmenation fault... what is a segmentation fault?

Code:
// irc.cpp
// drdroid


// Note to self(auto_ptr):
// std::ostringstream ss; ss << "Hello " << 100 << ' ' << 1.5 << '\n';   
// ss.str() yields an std::string, ss.str.c_str() for a const char*

// Eg:
// ostringstream ss;
// ss << "text" << myinteger << "etc.";   
// std::string textinstream = ss.str();   
// const char* c_string = ss.str().c_str();


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <iostream.h>


#define SERVER "irc.nj.us.dal.net"
#define PORT 6667
#define IDENT "drdroid"
#define NAME "DrDroid"
#define NICK "drdroid"
#define CHANNEL "#linux"
#define MAX 100


void Send(int socket_fd, char* sendbuffer, struct sockaddr_in name)
{
        int bytes;
        if((bytes=sendto(socket_fd, sendbuffer, strlen(sendbuffer), 0, (struct sockaddr *)&name, sizeof(struct sockaddr))) == -1)
        {
                printf("Error: sendto()\n");
                exit(1);
        }
}

int main()
{
  // const char* const server_name = argv[1];
  // const char* const message = argv[2];
  
  int socket_fd;
  int bytes, i;
  char buffer[MAX];
  char sendbuffer[MAX];
  struct sockaddr_in name;
  struct hostent* hostinfo;


  // Create the socket and store the name under the socket address.

  socket_fd=socket(PF_INET, SOCK_STREAM, 0);
  name.sin_family=AF_INET;


  //Check for host.

  hostinfo=gethostbyname(SERVER);

  if(hostinfo==NULL)
    {
      cout << "Error Finding Host!" << endl;
      return 1;
    }


  else
    {
      //Find Host Info.

      name.sin_addr=*((struct in_addr*)hostinfo->h_addr);
      name.sin_port=htons(PORT);

      memset(&(name.sin_zero), '\0', 8);

      
      //Connect to the server
      
      if(connect(socket_fd, (struct sockaddr *)&name, sizeof(struct sockaddr_in)) == -1)
	{
	  perror("connect");
	  cout << "Error Connecting!" << endl;
	  return 1;
	}
      
      else
	{
	  //Write the text on the command line to the socket.
	  
	  while((bytes = recv(socket_fd,buffer,MAX-1,0)) != -1)
	    {
	      
                buffer[bytes] = '\0';
                cout << 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);
			cout << "Sending to " << SERVER << ": " << sendbuffer << endl;
			Send(socket_fd,sendbuffer,name);

                        sprintf(sendbuffer,"NICK %s\r\n",NICK);
                        cout << "Sending to " << SERVER << " : " << sendbuffer << endl;
                        Send(socket_fd,sendbuffer,name);

                        sprintf(sendbuffer,"JOIN %s\r\n",CHANNEL);
                        cout << "Sending to " << SERVER << " : " << sendbuffer << endl;
			Send(socket_fd,sendbuffer,name);
                }
                else if(strncmp(buffer,"PING",4)==0)
                {
                        sprintf(sendbuffer,"PONG");
                        for(i=4;i<strlen(buffer)+1;i++)
                                sendbuffer[i]=buffer[i];
                        cout << "Sending to " << SERVER << ": " << sendbuffer;
                        Send(socket_fd,sendbuffer,name);
                }
	    }
	  close(socket_fd);
	}
    }
  return 0;
}

Last edited by drdroid; 11-15-2003 at 10:14 PM.
 
Old 11-16-2003, 01:15 AM   #10
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Quote:
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...

Last edited by Stack; 11-16-2003 at 01:18 AM.
 
Old 11-16-2003, 09:06 AM   #11
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
what is ident? what is it supposed to look like? i keep getting 'no ident responce'.

And how do I create a place for input that won't interupt the flow of the program?

Last edited by drdroid; 11-16-2003 at 09:09 AM.
 
Old 11-16-2003, 02:14 PM   #12
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
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.
 
Old 11-16-2003, 03:22 PM   #13
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Quote:
Yeah, i don't get a Ident responce
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...
 
Old 11-16-2003, 03:27 PM   #14
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
a) so not having an ident responce is ok?

b) i'll figure something out about the flow thingy.
 
Old 11-16-2003, 03:34 PM   #15
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
Quote:
a) so not having an ident responce is ok?
Well join a channel and look for people with a ~ infront of their ident(no ident response). You will find most people dont run ident.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Irc Client Haggis Linux - Newbie 12 01-11-2005 11:15 AM
I need an IRC client zozeer General 5 12-03-2004 05:02 AM
non-x IRC client CletusJones Linux - Software 8 10-03-2004 09:10 PM
How speak irc client and irc server program? mech Linux - Networking 1 03-31-2004 05:23 PM
IRC Client Whitemagician Linux - Software 7 08-20-2003 02:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration