brewersfan1976

irc-write-select.c

Dec 24th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. /* irc chat program will write to the irc chat server your text */
  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. #include <sys/time.h>
  12.  
  13. void error (const char *msg);
  14.  
  15. int main (int argc, char *argv[])
  16. {
  17.     /* set up the read_fd_set structure */
  18.  
  19.     fd_set read_fd_set;
  20.     struct timeval tv;
  21.  
  22.     /* wait 5 seconds for input before leaving fgets */
  23.    
  24.     tv.tv_sec = 5;
  25.     tv.tv_usec = 0;
  26.        
  27.     /* Declare variables for the socket, port number and socket value */
  28.  
  29.     int sockfd, portno, n;
  30.  
  31.     /* Declare the socket server address structure */
  32.  
  33.     struct sockaddr_in serv_addr;
  34.  
  35.     /* Declare server host structure */
  36.  
  37.     struct hostent *server;
  38.  
  39.     /* Declare a variable for the text to be read from the server */
  40.  
  41.     char buffer1[256];
  42.     char buffer2[256];
  43.  
  44.     /* Declare a character pointer to send a message */
  45.     /* to the server if they ping us */
  46.  
  47.     char *send_message;
  48.  
  49.     /* prints error message if the input is invaild */
  50.  
  51.     if (argc < 3)
  52.     {
  53.       fprintf(stderr, "usage %s hostname port\n", argv[0]);
  54.       exit(0);
  55.     }
  56.  
  57.     /* converts string to integer and stores the port number */
  58.  
  59.     portno = atoi(argv[2]);
  60.  
  61.     /* open the socket */
  62.  
  63.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  64.    
  65.     /* prints error message if socket can't be opened */
  66.  
  67.     if (sockfd < 0)
  68.        error("ERROR opening socket");
  69.     else
  70.        printf("socket opened sucessfully\n");
  71.  
  72.     /* stores server name to connect to */
  73.  
  74.     server = gethostbyname(argv[1]);
  75.  
  76.     /* prints error message if host does not exist */
  77.  
  78.     if (server == NULL)
  79.     {
  80.        fprintf(stderr, "ERROR, no such host\n");
  81.        exit(0);
  82.     }
  83.     else
  84.         printf("Found host\n");
  85.  
  86.     /* clears the server address variable of data */
  87.  
  88.     bzero((char *) &serv_addr, sizeof(serv_addr));
  89.  
  90.     /* assigns server address to structure */
  91.  
  92.     serv_addr.sin_family = AF_INET;
  93.  
  94.     /* copy server address structure to the server host structure */
  95.  
  96.     bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  97.  
  98.     /* Assigns the port number to the port structure */
  99.  
  100.     serv_addr.sin_port = htons(portno);
  101.  
  102.     /* connect to the server */
  103.  
  104.     if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  105.        error ("ERROR connecting");
  106.     else
  107.        printf("connection sucessful\n");
  108.  
  109.     /* send nickname to the server */
  110.  
  111.     n = write(sockfd, "NICK linux2015\r\n", strlen("NICK linux2015\r\n"));
  112.  
  113.     /* send username to the server */
  114.  
  115.     n = write(sockfd, "USER linux2015 8 * :linux\r\n", strlen("USER linux2015 8 * :linux\r\n"));
  116.  
  117.     /* join the channel */
  118.  
  119.     printf("Enter channel to join ex. JOIN #<channel> ");
  120.     fgets(buffer2, 255, stdin);
  121.     n = write(sockfd, &buffer2, strlen(buffer2));
  122.  
  123.    
  124.  
  125.     printf("press enter to chat\n");
  126.    
  127.    
  128.     while (1)
  129.     {  
  130.         /* zero out the read_fd_set variable */
  131.  
  132.     FD_ZERO(&read_fd_set);
  133.  
  134.         /* setup I/O multiplexing for read and write */
  135.  
  136.     FD_SET(0, &read_fd_set);
  137.     FD_SET(sockfd, &read_fd_set);
  138.  
  139.         /*  create our select to know how long to wait for input on fgets */
  140.  
  141.         select(10, &read_fd_set, NULL, NULL, &tv);
  142.  
  143.         /* if user press enter there is input to be read */
  144.  
  145.     if (FD_ISSET(0, &read_fd_set))
  146.         {
  147.            printf("Type your message ex. PRIVMSG #linux :<message> ");
  148.  
  149.            /* clear the buffer */
  150.          
  151.            bzero(buffer2,256);
  152.  
  153.           /* get chat text from user */
  154.  
  155.            fgets(buffer2, 255, stdin);
  156.  
  157.            /* write data to server */
  158.  
  159.            n = write(sockfd, &buffer2, strlen(buffer2));
  160.  
  161.            /* print error message if it can't write the data */
  162.    
  163.            if (n < 0)
  164.               error("ERROR writing to socket");
  165.      }
  166.  
  167.          /* if user goes 5 seconds without input leave fgets and read for potential ping message */
  168.  
  169.      if (FD_ISSET(sockfd, &read_fd_set))
  170.          {
  171.              bzero(buffer1, 256);
  172.          
  173.              n = read(sockfd, &buffer1, 255);
  174.  
  175.              /* if the server sends ping we send pong */
  176.              /* and a message to keep our connection alive */
  177.  
  178.              if (strstr(buffer1, "PING :") != NULL) {
  179.                 send_message = "PONG :Linux rocks !!";
  180.                 write(sockfd, send_message, strlen(send_message));
  181.                 printf("%s\n", send_message);
  182.              }
  183.      }  
  184.     }                        
  185.  
  186.     /* close socket before exiting */
  187.    
  188.     close(sockfd);
  189.  
  190.     return 0;
  191. }
  192.  
  193. void error (const char *msg)
  194. {
  195.     perror(msg);
  196.     exit(0);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment