title: What is a simple C or C++ TCP server and client example?
body: <p>I need to quickly implement a very small C or C++ TCP server/client solution. This is simply to transfer literally an array of bytes from one computer to another - doesn't need to be scalable / over-complicated. The simpler the better. Quick and dirty if you can.</p>
  
  <p>I tried to use the code from this tutorial, but I couldn't get it to build using g++ in Linux: <a href="http://www.linuxhowtos.org/C_C++/socket.htm" rel="noreferrer">http://www.linuxhowtos.org/C_C++/socket.htm</a></p>
  
  <p>If possible, I'd like to avoid 3rd party libraries, as the system I'm running this on is quite restricted. This must be C or C++ as the existing application is already implemented.</p>
  
  <p>Thanks to <strong>emg-2</strong>'s answer, I managed to make the above mentioned code sample compatible with C++ using the following steps:</p>
  
  <p>Add these headers to both client and server:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  #include &lt;unistd.h&gt;
  </code></pre>
  
  <p>In <a href="http://www.linuxhowtos.org/data/6/server.c" rel="noreferrer">server.c</a>, change the type of clilen to socklen_t.</p>
  
  <pre><code>int sockfd, newsockfd, portno/*, clilen*/;
  socklen_t clilen;
  </code></pre>
  
  <p>In <a href="http://www.linuxhowtos.org/data/6/client.c" rel="noreferrer">client.c</a>, change the following line:</p>
  
  <pre><code>if (connect(sockfd,&amp;serv_addr,sizeof(serv_addr)) &lt; 0) { ... }
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>if (connect(sockfd,(const sockaddr*)&amp;serv_addr,sizeof(serv_addr)) &lt; 0)
  </code></pre>