title: How to connect Arduino to C++ using sockets?
body: <p>My C++ client side socket code only collects the very first line of the output provided by an Arduino socket server (I guess I can call it that). Can you please tell me where I am going wrong with my C++ code and how to fix it? If the question is too verbose please jump to the C++ code at the bottom.</p>
  
  <p><strong>Hardware setup</strong>: Arduino Mega with an Ethernet card (shield) and an Intel NUC with Ubuntu 16.04. The two devices are connected using cable and an unmanaged switch.</p>
  
  <p><a href="https://i.stack.imgur.com/piMU3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/piMU3.png" alt="enter image description here"></a></p>
  
  <p><strong>Arduino Side</strong>: I started out with a web server example from the Arduino Ethernet library, and modified the code until I was able to collect the status of all I/O, process the I/O data, and make results available to a web client. Image below shows a snapshot of the HTML my Arduino dishes out. 
  <a href="https://i.stack.imgur.com/2QSgl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2QSgl.png" alt="enter image description here"></a></p>
  
  <p>The Arduino code that handles the socket server side of things is as follows (everything works, but thought to include to show everything):</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  #include &lt;Ethernet.h&gt;
  
  byte mac[] = {0xBA, 0xDA, 0x55, 0x12, 0x34, 0x56};
  
  IPAddress ip(192, 168, 0, 21);
  
  EthernetServer server(80);
  
  void setup() 
  {
    Ethernet.begin(mac, ip);
  
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) 
    {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware.");
     while (true) 
     {
        delay(1); // do nothing, no point running without Ethernet hardware
     }
  }
  
  if (Ethernet.linkStatus() == LinkOFF) 
  {
      Serial.println("Ethernet cable is not connected.");
  }
  
  server.begin();
  }
  
  void loop()
  {
      //true when there is an incoming connection
     if (client) 
     {
       Serial.println("new client");
       // an http request ends with a blank line
       boolean currentLineIsBlank = true;
       while (client.connected()) 
       {
         if (client.available()) 
         {
            //the next two lines print the client HTTP GET request to serial.
            char c = client.read();
            Serial.write(c);
            // if you've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so you can send a reply
            if (c == '\n' &amp;&amp; currentLineIsBlank) 
            {
               // send a standard http response header
               client.println("HTTP/1.1 200 OK");
               client.println("Content-Type: text/html");
               client.println("Connection: close");  
               client.println("Refresh: 1");  
               client.println();
               client.println("&lt;!DOCTYPE HTML&gt;");
               client.println("&lt;html&gt;");
  
               //whole bunch of client.println("...."); to dish out a web page.
  
                client.println("&lt;/html&gt;");
                break;
              }
  
              if (c == '\n') 
              {
                 // you're starting a new line
                 currentLineIsBlank = true;
              } else if (c != '\r') 
              {
                // you've gotten a character on the current line
                currentLineIsBlank = false;
              }
           }
        }
       // give the web browser time to receive the data
       //delay(1);
       // close the connection:
       client.stop();
       Serial.println("client disconnected");
    }
  }
  </code></pre>
  
  <p><strong>Intel NUC with Ubuntu 16.04 and C++11</strong>: I have been following a tutorial (<a href="https://www.binarytides.com/socket-programming-c-linux-tutorial/" rel="nofollow noreferrer">https://www.binarytides.com/socket-programming-c-linux-tutorial/</a>) to figure out the socket client side, and learning C++ from <a href="https://www.learncpp.com" rel="nofollow noreferrer">https://www.learncpp.com</a>. So far I am able to send a request to a Google server and collect the HTML page using my socket, and print the HTML to the terminal:
  <a href="https://i.stack.imgur.com/bqMLg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bqMLg.png" alt="enter image description here"></a></p>
  
  <p>Here is my C++ code:</p>
  
  <pre><code>#include &lt;iostream&gt; //for std::cout &amp; std::endl
  #include&lt;arpa/inet.h&gt; //inet_addr
  #include&lt;string.h&gt; // for strlen
  
  int main()
  {
      int my_socket = socket(AF_INET , SOCK_STREAM , 0);
  
      //make sure the socket we got is OK
      if (my_socket == -1)
      {
           std::cout &lt;&lt; "problem creating a socket." &lt;&lt; std::endl;
      }
  
      struct sockaddr_in connectionToServer;
  
      connectionToServer.sin_addr.s_addr = inet_addr("172.217.2.99");//google IP
      connectionToServer.sin_family = AF_INET; //type of IP addres. where AF_INET is IPv4
      connectionToServer.sin_port = htons(80); //port is set via a method
  
  
      if (connect(my_socket , (struct sockaddr *)&amp;connectionToServer , sizeof(connectionToServer)) &lt; 0)
      {
          std::cout &lt;&lt; "connect error" &lt;&lt; std::endl;
          return 1;
      }
  
      std::cout &lt;&lt; "Connected" &lt;&lt; std::endl;
  
      //send a request to get a page
      char *message = "GET / HTTP/1.1\r\n\r\n";
      if( send(my_socket , message , strlen(message) , 0) &lt; 0)
      {
          std::cout &lt;&lt; "Send failed" &lt;&lt; std::endl;
          return 1;
      }
  
      std::cout &lt;&lt; "Data Sent\n" &lt;&lt; std::endl;
  
  
      char buffer [20000] = {};
      if( recv(my_socket, buffer , sizeof(buffer) , 0) &lt; 0)
      {
          std::cout &lt;&lt; "recv failed" &lt;&lt; std::endl;
      }
  
      for(int i=0; i&lt;20000 ; i++)
      {
          std::cout&lt;&lt; buffer[i];
      }
  
      return 0;
   }
  </code></pre>
  
  <p><strong>Problem</strong>: when I change the IP address from Google to my Arduino in my C++ program, the cpp client socket program only collects the very first line that the Arduino socket server outputs. I know it is the very first line as I modified the first line the Arduino server dishes out by adding "..but it doesn't matter" and the change showed up in the standard output window of the c++ program. I need the C++ program to collect the entire output, not just the first line. for the life of me I can't figure out how.</p>
  
  <p><a href="https://i.stack.imgur.com/Ttjfr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ttjfr.png" alt="enter image description here"></a></p>
  
  <p>Could you please help me with the following questions:</p>
  
  <ol>
  <li>How do I collect the entire Arduino message (and not just the first line)? What modification do I need to do to my C++ program? The system needs to be able to pass data from one device to another.  </li>
  <li>The entire objective of this setup is to pass 2 floating point numbers and 6 integers from the Arduino to my C++ program. Very soon I am going to do away with the whole HTML thing. What kind of protocol would you recommend I use when passing the data over? I was thinking of padding each value in letters. Ex: “Aint1A Bint2B Cint3C Dfloat1D ...” and so on. Can you recommend some post/tutorial/web page that suggests the best way to package and process data that arrives to a C++ program via a socket?</li>
  </ol>
  
  <p>I apologize in advance for the 'fix-my-codez' question, but all the questions I have read are a bit too advanced for me as they address buffer overflows, security, endian issues, error handling, malformed messages and so on, which is way beyond my needs (ability is probably more accurate). Thank you very much for your time and help. </p>