brewersfan1976

irc-read.c

Oct 12th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. /* irc chat program will display irc chatroom text from the irc server */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <netdb.h>
  11.  
  12. void error (const char *msg);
  13.  
  14. int main (int argc, char *argv[])
  15. {
  16.     /* Declare variables for the socket, port number and socket value */
  17.  
  18.     int sockfd, portno, n;
  19.    
  20.     /* Declare the socket server address structure */
  21.  
  22.     struct sockaddr_in serv_addr;
  23.  
  24.     /* Declare server host structure */
  25.  
  26.     struct hostent *server;
  27.  
  28.     /* Declare a variable for the text to be read from the server */
  29.  
  30.     char buffer[256];
  31.  
  32.     /* declare a character pointer to send the server a message if they ping us */
  33.  
  34.     char *send_message;
  35.  
  36.     /* prints error message if the input is invaild */
  37.  
  38.     if (argc < 3)
  39.     {
  40.       fprintf(stderr, "usage %s hostname port\n", argv[0]);
  41.       exit(0);
  42.     }
  43.  
  44.     /* converts string to integer and stores the port number */
  45.  
  46.     portno = atoi(argv[2]);
  47.  
  48.     /* open the socket */
  49.  
  50.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  51.  
  52.     /* prints error message if socket can't be opened */
  53.  
  54.     if (sockfd < 0)
  55.        error("ERROR opening socket");
  56.     else
  57.        printf("socket opened sucessfully\n");
  58.  
  59.     /* stores server name to connect to */
  60.  
  61.     server = gethostbyname(argv[1]);
  62.  
  63.     /* prints error message if host does not exist */
  64.  
  65.     if (server == NULL)
  66.     {
  67.        fprintf(stderr, "ERROR, no such host\n");
  68.        exit(0);
  69.     }
  70.     else
  71.         printf("Found host\n");
  72.  
  73.     /* clears the server address variable of data */
  74.  
  75.     bzero((char *) &serv_addr, sizeof(serv_addr));
  76.  
  77.     /* assigns server address to structure */
  78.  
  79.     serv_addr.sin_family = AF_INET;
  80.  
  81.     /* copy server address structure to the server host structure */
  82.  
  83.     bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  84.  
  85.     /* Assigns the port number to the port structure */
  86.  
  87.     serv_addr.sin_port = htons(portno);
  88.  
  89.     /* connect to the server */
  90.  
  91.     if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  92.        error ("ERROR connecting");
  93.     else
  94.        printf("connection sucessful\n");
  95.  
  96.     /* send nickname to the server */
  97.  
  98.     n = write(sockfd, "NICK unbuntu76\r\n", strlen("NICK unbuntu76\r\n"));
  99.  
  100.     /* send username to the server */
  101.  
  102.     n = write(sockfd, "USER unbuntu76 8 * :linux\r\n", strlen("USER unbuntu76 8 * :linux\r\n"));
  103.  
  104.     /* join the channel */
  105.  
  106.     printf("Enter channel to join ex. JOIN #<channel> ");
  107.     bzero(buffer, 256);
  108.     fgets(buffer, 255, stdin);
  109.     n = write(sockfd, buffer, strlen(buffer));
  110.  
  111.     while (buffer != "*quit")
  112.     {  
  113.          /* clear the buffer */
  114.  
  115.          bzero(buffer, 256);
  116.  
  117.          /* read data from server */
  118.  
  119.          n = read(sockfd, buffer, 255);
  120.  
  121.          /* if server sends ping we send pong and a message */
  122.          /* so our connection stays alive */
  123.  
  124.          if (strstr(buffer, "PING :") != NULL)
  125.          {
  126.            send_message = "PONG :Linux rocks !!";
  127.            write(sockfd, send_message, strlen(send_message));
  128.            printf("%s\n", send_message);
  129.          }
  130.  
  131.          /* print error message if it can't read the data */
  132.  
  133.          if (n < 0)
  134.             error("ERROR reading from socket");
  135.  
  136.          /* print data from server on the screen */
  137.    
  138.          printf("%s\n", buffer);
  139.       }
  140.  
  141.     /* close socket before exiting */
  142.    
  143.     close(sockfd);
  144.  
  145.     return 0;
  146. }
  147.  
  148. void error (const char *msg)
  149. {
  150.     perror(msg);
  151.     exit(0);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment