title: Bad file descriptor error while sending data through tcp socket
body: <p>I want to create data and then send to <code>server</code>. Data from information on each sensor should create in <code>Network.cpp</code> after new sensor was added (each sensor is an new network) and send with <code>client.cpp</code>, I use inheritance to use function add of <code>Network</code> class to <code>client</code> class or vice versa but I need help to use function correctly for my program work. When I run program I get this error:</p>
  
  <blockquote>
    <p>connect failed. Error: Bad file descriptor </p>
  </blockquote>
  
  <p>What am I missing?</p>
  
  <p><strong>Network.h:</strong></p>
  
  <pre><code>class Network :public client{
  public:
      Network();
      void add(Sensor new_sensor);
      virtual ~Network();
  private:
      vector&lt;Sensor&gt; sensors;
  }
  </code></pre>
  
  <p><strong>Network.cpp:</strong></p>
  
  <pre><code>void Network::add(const client &amp;a,Sensor new_sensor) {
      sensors.push_back(new_sensor);
      unsigned int Header[2] = {0xe1,0xe2};
      uint16_t u16;
      u16 = htons(Header[2]);
      memcpy(packet + 0, &amp;u16, 2);
      unsigned int SensorSn[4] = { 0x1e, 0x25, 0x71, 0x80 };
      uint32_t u32;
      u32 = htons(SensorSn[4]);
      memcpy(packet + 2, &amp;u32, 4);
      uint32_t a32;
      unsigned int SensorTemp[4] = { 0x00, 0x00, 0x00, 0x23 };
      a32 = htons(SensorTemp[4]);
      memcpy(packet + 6, &amp;a32, 4);
          a.send_data();
  }
  </code></pre>
  
  <p><strong>client.h:</strong></p>
  
  <pre><code>class client {
  public:
      client();
      void conn(string, int);
      void send_data() const;
      string receive(int);
      virtual ~client();
  private:
      int sock;
      struct sockaddr_in server_addr;
  protected:
      char packet[10];
  };
  
  } 
  </code></pre>
  
  <p><strong>client.cpp:</strong></p>
  
  <pre><code>void client::conn(string address, int port) {
      //create socket if it is not already created
      if (sock == -1) {
          //Create socket
          sock = socket(AF_INET, SOCK_STREAM, 0);
          if (sock == -1) {
              perror("Could not create socket");
          }
  
          cout &lt;&lt; "Socket created\n" &lt;&lt; endl;  //doesn't show this line!!!
      }
      server_addr.sin_family = AF_INET;
      server_addr.sin_port = htons(1234);
      server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      while(1){
      int m=connect(sock, (struct sockaddr *) &amp;server_addr, sizeof(server_addr));
      if (m==-1){
          perror("connect failed. Error");       
          continue;
      }
      }
      cout &lt;&lt; "Connected\n";  //doesn't show this line!!!
      close(sock);
  
  
  }
  void client::send_data()const {
      int n=send(sock, packet, sizeof(packet), 0)  ;
  if (n==-1){
      perror("send failed. Error");  ///my program stop in this line
              exit(1);
  
  }
      cout &lt;&lt; "Data send"&lt;&lt;endl;
  
      close(sock);
  }
  </code></pre>