title: Closing server using select function
body: <p>So, I am new to socket programming in C and am using the <code>select</code> function to communicate with multiple clients on a server.  The server essentially just echos a buffer back to a client based on a requirest.  I have used <a href="http://beej.us/guide/bgnet/html/multi/index.html" rel="nofollow noreferrer">Beej's guide to network programming</a> as the model for my server.  What is not clear to me is whether I am exiting the server properly when a command is sent to exit.  The code for handling the <code>select</code> function looks like:</p>
  
  <pre><code>for (;;)
  {
      read_fds = master; // Copy the master fds to the basic read...
  
      // Check to see if any flags have been set for reading
      if (select(fdmax + 1, &amp;read_fds, NULL, NULL, NULL) == -1)
      {
          perror("select");
          exit(4);
      }
      for (i = 0; i &lt;= fdmax; i++)
      {
          if (FD_ISSET(i, &amp;read_fds))
          {
              if (i == listener)
              { // need to add new connnection here
                  addrlen = sizeof remote_addr;
                  newfd = accept(listener, (struct sockaddr *)&amp;remote_addr, &amp;addrlen);
                  if (newfd == -1)
                  {
                      perror("accept");
                  }
                  else
                  {
                      FD_SET(newfd, &amp;master);
                      if (newfd &gt; fdmax)
                      {
                          fdmax = newfd;
                      }
                  }
  
              } // end add new listener
              else
              {
                  /*if (i == 0)
                  {
                      printf("Input received from stdin\n");
                      continue;
                  } */ 
                  // handle data from existing client
  
                  if ((nbytes = recv(i, input_buffer, sizeof input_buffer, 0)) &lt;= 0)
                  { // Remove connection if there is a hangup...
                      if (nbytes == 0)
                      {
                          printf("selectserver: socket%d hung up\n", i);
                      }
                      else
                      {
                          perror("recv");
                      }
                      close(i);
                      FD_CLR(i, &amp;master);
                  } // no bytes error or port closed - remove from fdset
                  else
                  {
                      if (strchr(input_buffer,'\r') == NULL){
                          printf("we have a problem\n");
  
                      }
                      if (strcmp(input_buffer, "exit")){
  
                          printf("Exit requested...\n");
                          close(listener);
                          exit(0);
                      }
  
                      for (j = 0; j &lt;= fdmax; j++)
                      {
                          if (FD_ISSET(j, &amp;master))
                          {
                              if (j != listener &amp;&amp; j != 0)
                              {
                                  if (send(j, input_buffer, nbytes, 0) == -1)
                                  {
                                      error_msg = strerror(errno);
                                      printf("%s\n", error_msg);
                                      //perror("send");
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>and the code I am specifically concerned about is </p>
  
  <pre><code>if (strcmp(input_buffer, "exit")){
  
      printf("Exit requested...\n");
      close(listener);
      exit(0);
  }
  </code></pre>
  
  <p>where <code>listener</code> is the file descriptor for the listening socket.  Is this the correct way of exiting this loop or is there a better way to handle this?  </p>