title: accept function loop in thread c++ socket
body: <p>I have 2 questions:</p>
  
  <p>first - I try to connect one client to server each time. (the accept function is in thread) but the connection fail in accept - return always -1 (instead of zero).</p>
  
  <p>the second question - how I can add timeout for to accepting client?</p>
  
  <p>server.h</p>
  
  <pre><code>class Server{
  public:
      void open(int port, object&amp;);
      void startThreadOPeration();
      void stop(){};
      ~Server();
  private:
      sockaddr_in address;
      int socketfd;
      Object* obj; 
      void startThread();
      std::thread acceptClient;
  };
  </code></pre>
  
  <p>server.cpp</p>
  
  <pre><code>void Server::open(int port,Object&amp; obj) {
      this-&gt;obj = &amp;obj;
      int socketfd = socket(AF_INET, SOCK_STREAM, 0);
      if (socketfd == -1) {
          throw "Could not create a socket";
      }
      this-&gt;socketfd = socketfd;
  
      sockaddr_in address;
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = INADDR_ANY; 
      address.sin_port = htons(port);
      this-&gt;address = address;
  
      if (bind(socketfd, (struct sockaddr *) &amp;this-&gt;address, sizeof(this-&gt;address)) == -1) {
          throw "Could not bind the socket to an IP";
      }
  
  
      if (listen(this-&gt;socketfd, 5) == -1) { 
          throw "Error during listening command";
      } else {
          std::cout&lt;&lt;"Server is now listening ..."&lt;&lt;std::endl;
      }
  
  
      startThread();
  }
  
  
  void Server::startThreadOPeration() {
      while (!side_server::stop) {
          // accepting a client
          int client_socket = accept(this-&gt;socketfd, (struct sockaddr *) &amp;this-&gt;address,
                                     (socklen_t *) &amp;this-&gt;address); // Return -1
          if (client_socket == -1) {
              throw "Error accepting client";
          }
          //do operation
      }
  
  
  
  void Server::startThread() {
  
  
  
   acceptClient = std::thread(&amp;Server::startThreadOPeration,this);
  }
  
  Server::~Server(){
      close(this-&gt;socketfd);
      if(acceptClient.joinable()){
          this-&gt;acceptClient.join();
      }
  }
  </code></pre>
  
  <p>thank you for help!</p>