title: irc chat in C programming
body: <p>I wrote two programs for a simple IRC chat program in C. This is my first attempt at socket programming. The read program works just fine. The write program technically works but with one minor hitch. It will ping timeout if waiting on fgets to long without input. How do I send PONG if the server sends ping and wait on fgets at the same time. Basically I want to wait for input and read the socket at the same time.</p>
  
  <p>Thank you so much for your help.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>/* irc chat program will write to the irc chat server your text */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  
  void error (const char *msg);
  
  int main (int argc, char *argv[])
  {
  /* Declare variables for the socket, port number and socket value */
  
  int sockfd, portno, n;
  
  /* Declare the socket server address structure */
  
  struct sockaddr_in serv_addr;
  
  /* Declare server host structure */
  
  struct hostent *server;
  
  /* Declare a variable for the text to be read from the server */
  
  char buffer[256];
  
  /* Declare a character pointer to send a message */
  /* to the server if they ping us */
  
  char *send_message;
  
  /* prints error message if the input is invaild */
  
  if (argc &lt; 3)
  {
    fprintf(stderr, "usage %s hostname port\n", argv[0]);
    exit(0);
  }
  
  /* converts string to integer and stores the port number */
  
  portno = atoi(argv[2]);
  
  /* open the socket */
  
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  
  /* prints error message if socket can't be opened */
  
  if (sockfd &lt; 0)
     error("ERROR opening socket");
  else
     printf("socket opened sucessfully\n");
  
  /* stores server name to connect to */
  
  server = gethostbyname(argv[1]);
  
  /* prints error message if host does not exist */
  
  if (server == NULL)
  {
     fprintf(stderr, "ERROR, no such host\n");
     exit(0);
  }
  else
      printf("Found host\n");
  
  /* clears the server address variable of data */
  
  bzero((char *) &amp;serv_addr, sizeof(serv_addr));
  
  /* assigns server address to structure */
  
  serv_addr.sin_family = AF_INET;
  
  /* copy server address structure to the server host structure */
  
  bcopy((char *)server-&gt;h_addr, (char *)&amp;serv_addr.sin_addr.s_addr, server-&gt;h_length);
  
  /* Assigns the port number to the port structure */
  
  serv_addr.sin_port = htons(portno);
  
  /* connect to the server */
  
  if (connect(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0)
     error ("ERROR connecting");
  else
     printf("connection sucessful\n");
  
  /* send nickname to the server */
  
  n = write(sockfd, "NICK csocket76\r\n", strlen("NICK csocket76\r\n"));
  
  /* send username to the server */
  
  n = write(sockfd, "USER csocket76 8 * :linux\r\n", strlen("USER csocket76 8 * :linux\r\n"));
  
  /* join the channel */
  
  printf("Enter channel to join ex. JOIN #&lt;channel&gt; ");
  fgets(buffer, 255, stdin);
  n = write(sockfd, buffer, strlen(buffer));
  
  while (buffer != "*quit")
  {  
       bzero(buffer, 256);
  
       n = read(sockfd, buffer, 255);
  
       /* if the server sends ping we send pong */
       /* and a message to keep our connection alive */
  
       if (strstr(buffer, "PING :") != NULL) {
         send_message = "PONG :Linux rocks !!";
         write(sockfd, send_message, strlen(send_message));
         printf("%s\n", send_message);
      }
  
       printf("Type your message ex. PRIVMSG #linux :&lt;message&gt; ");
  
       /* clear the buffer */
  
       bzero(buffer,256);
  
       /* Input data from user */
  
       fgets(buffer, 255, stdin);
  
       /* write data to server */
  
       n = write(sockfd, buffer, strlen(buffer));
  
       /* print error message if it can't write the data */
  
       if (n &lt; 0)
          error("ERROR writing to socket");
  
    }
  
    /* close socket before exiting */
  
     close(sockfd);
  
    return 0;
    }
  
    void error (const char *msg)
    {
       perror(msg);
       exit(0);
    }
  </code></pre>