title: Chat Program in C
body: <p>First off, this is homework, so please no outright answers. I am writing a back and forth chat program in C. I'm extremely new to C (just started learning for this class). Currently I have three files:</p>
  
  <p>server.c</p>
  
  <pre><code>#include &lt;stdio.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;
  #include &lt;stdlib.h&gt;  
  #include &lt;strings.h&gt;
  #include &lt;string.h&gt; 
  #include "chat.h"
  
  #define SERVER_PORT 1725
  #define MAX_PENDING 5
  #define MAX_LINE 256
  
  int main()
  {
      struct sockaddr_in sin;
      char buf[MAX_LINE];
      int len;
      int s, new_s;
      struct chat_packet packet;
  
      /* build address data structure */
      bzero((char *)&amp;sin, sizeof(sin));
      sin.sin_family = AF_INET;
      sin.sin_addr.s_addr = INADDR_ANY;
      sin.sin_port = htons(SERVER_PORT);
  
      /* setup passive open */
      if ((s = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) 
      {
          perror("simplex-talk: socket");
          exit(1);
      }
      if ((bind(s, (struct sockaddr *)&amp;sin, sizeof(sin))) &lt; 0) 
      {
          perror("simplex-talk: bind");
          exit(1);
      }
      listen(s, MAX_PENDING);
  
      /* wait for connection, then receive and print text */
      while(1) 
      {
          if ((new_s = accept(s, (struct sockaddr *)&amp;sin, &amp;len)) &lt; 0) 
          {
              perror("simplex-talk: accept");
              exit(1);
          }   
  
          /* Stay in the following loop until CTRL+C */
          while (len = recv(new_s, &amp;packet, sizeof(packet), 0)) 
          {
              fputs(packet.sender_name, stdout);
              fputs(": ", stdout);
              fputs(packet.data, stdout); 
              fputs("\nYou: ", stdout);
  
              while (fgets(buf, sizeof(buf), stdin)) 
              {
                  if(strlen(buf) &gt; 144)
                  {
                      printf("Your message is too long. Please enter a new message.\n");
                      continue;                                   
                  }
  
                  else
                  {
                      buf[MAX_LINE-1] = '\0';
  
                      strncpy(packet.data,buf,144);
                      char sender[8] = "Mason"; /*should be argv[index of name]*/
                      strncpy(packet.sender_name, sender, 8);
  
                      send(new_s, &amp;packet, sizeof(packet),0);
                  }
              }
          }
  
          close(new_s);
      }
  }
  </code></pre>
  
  <p>client.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt; 
  #include &lt;strings.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;
  #include "chat.h"
  
  #define SERVER_PORT 1725
  #define MAX_LINE 256
  
  int main(int argc, char * argv[])
  {
      FILE *fp;
      struct hostent *hp;
      struct sockaddr_in sin;
      char *host;
      char buf[MAX_LINE];
      int s;
      int len;
      struct chat_packet packet;
  
      if (argc==2) 
      {
          host = argv[1];
      }
      else 
      {
          fprintf(stderr, "usage: simplex-talk host\n");
          exit(1);
      }
  
      /* translate host name into peer's IP address */
      hp = gethostbyname(host);
      if (!hp) {
          fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
          exit(1);
      }
      /* build address data structure */
      bzero((char *)&amp;sin, sizeof(sin));
      sin.sin_family = AF_INET;
      bcopy(hp-&gt;h_addr, (char *)&amp;sin.sin_addr, hp-&gt;h_length);
      sin.sin_port = htons(SERVER_PORT);
      /* active open */
      if ((s = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) 
      {
          perror("simplex-talk: socket");
          exit(1);
      }
      if (connect(s, (struct sockaddr *)&amp;sin, sizeof(sin)) &lt; 0) 
      {
          perror("simplex-talk: connect");
          close(s);
          exit(1);
      }
      /* main loop: get and send lines of text */
      while (fgets(buf, sizeof(buf), stdin)) 
      {
          if(strlen(buf) &gt; 144)
          {
              printf("Your message is too long. Please enter a new message.\n");
              continue;                                   /*This allows the user to re-enter a message post-error*/
          }
          else
          {
              buf[MAX_LINE-1] = '\0';
  
              strncpy(packet.data, buf, 144);
              char sender[8] = "Abby"; /*should be argv[index of name]*/
              strncpy(packet.sender_name, sender, 8);
  
              send(s, &amp;packet, sizeof(packet), 0);    
              recv(s, &amp;packet, sizeof(packet),0); 
  
              fputs(packet.sender_name, stdout);
              fputs(": ", stdout);
              fputs(packet.data, stdout);
              fputs("\nYou: ", stdout);
          }
      }
  }
  </code></pre>
  
  <p>chat.h</p>
  
  <pre><code>#include &lt;stdint.h&gt; /* Needed for unsigned types */
  
  #define MAX_DATA_LEN 144 /* So we are on 16-bit boundary */
  #define USER_NAME_LEN 8
  
  /* You must send this packet across the socket.  Notice there are
   * no pointers inside this packet. Why?*/
  struct chat_packet {
      u_short version; /* 16 bits -- Set to version 2 in code */
      char sender_name[8]; /* 64 bits */
      char data[MAX_DATA_LEN]; /* Message goes in here */
  };
  </code></pre>
  
  <p>Everything except what is in the client and server while loops were given to me by my instructor. The base part of the assignment is getting back-and-forth chat functionality. I'm running everything in PuTTY using the command line. I duplicate the session and run client in one and server in the other. To run:</p>
  
  <p>./client serverName</p>
  
  <p>./server</p>
  
  <p>I am able to go back and forth one time, and then nothing else sends or receives. I am still able to type, but the two sessions cannot see each other's messages past the first back and forth. I am not sure where my code is wrong. Any advice would be appreciated, as I'm very new to the language. Thanks in advance! </p>