brewersfan1976

irc-write.c

Oct 12th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.75 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.  
  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 a message */
  33.     /* to the server if they ping us */
  34.  
  35.     char *send_message;
  36.  
  37.     /* prints error message if the input is invaild */
  38.  
  39.     if (argc < 3)
  40.     {
  41.       fprintf(stderr, "usage %s hostname port\n", argv[0]);
  42.       exit(0);
  43.     }
  44.  
  45.     /* converts string to integer and stores the port number */
  46.  
  47.     portno = atoi(argv[2]);
  48.  
  49.     /* open the socket */
  50.  
  51.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  52.  
  53.     /* prints error message if socket can't be opened */
  54.  
  55.     if (sockfd < 0)
  56.        error("ERROR opening socket");
  57.     else
  58.        printf("socket opened sucessfully\n");
  59.  
  60.     /* stores server name to connect to */
  61.  
  62.     server = gethostbyname(argv[1]);
  63.  
  64.     /* prints error message if host does not exist */
  65.  
  66.     if (server == NULL)
  67.     {
  68.        fprintf(stderr, "ERROR, no such host\n");
  69.        exit(0);
  70.     }
  71.     else
  72.         printf("Found host\n");
  73.  
  74.     /* clears the server address variable of data */
  75.  
  76.     bzero((char *) &serv_addr, sizeof(serv_addr));
  77.  
  78.     /* assigns server address to structure */
  79.  
  80.     serv_addr.sin_family = AF_INET;
  81.  
  82.     /* copy server address structure to the server host structure */
  83.  
  84.     bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  85.  
  86.     /* Assigns the port number to the port structure */
  87.  
  88.     serv_addr.sin_port = htons(portno);
  89.  
  90.     /* connect to the server */
  91.  
  92.     if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  93.        error ("ERROR connecting");
  94.     else
  95.        printf("connection sucessful\n");
  96.  
  97.     /* send nickname to the server */
  98.  
  99.     n = write(sockfd, "NICK csocket76\r\n", strlen("NICK csocket76\r\n"));
  100.  
  101.     /* send username to the server */
  102.  
  103.     n = write(sockfd, "USER csocket76 8 * :linux\r\n", strlen("USER csocket76 8 * :linux\r\n"));
  104.  
  105.     /* join the channel */
  106.  
  107.     printf("Enter channel to join ex. JOIN #<channel> ");
  108.     fgets(buffer, 255, stdin);
  109.     n = write(sockfd, buffer, strlen(buffer));
  110.  
  111.     while (buffer != "*quit")
  112.     {  
  113.          bzero(buffer, 256);
  114.  
  115.          n = read(sockfd, buffer, 255);
  116.  
  117.          /* if the server sends ping we send pong */
  118.          /* and a message to keep our connection alive */
  119.  
  120.          if (strstr(buffer, "PING :") != NULL) {
  121.            send_message = "PONG :Linux rocks !!";
  122.            write(sockfd, send_message, strlen(send_message));
  123.            printf("%s\n", send_message);
  124.         }
  125.          
  126.          printf("Type your message ex. PRIVMSG #linux :<message> ");
  127.  
  128.          /* clear the buffer */
  129.          
  130.          bzero(buffer,256);
  131.          
  132.          /* Input data from user */
  133.  
  134.          fgets(buffer, 255, stdin);
  135.  
  136.          /* write data to server */
  137.  
  138.          n = write(sockfd, buffer, strlen(buffer));
  139.  
  140.          /* print error message if it can't write the data */
  141.  
  142.          if (n < 0)
  143.             error("ERROR writing to socket");
  144.      
  145.       }
  146.  
  147.     /* close socket before exiting */
  148.    
  149.     close(sockfd);
  150.  
  151.     return 0;
  152. }
  153.  
  154. void error (const char *msg)
  155. {
  156.     perror(msg);
  157.     exit(0);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment