title: Simple socket programming code working
body: <p>I am trying to study and understand BSD socket programming using these simple example C++ code for TCP-IP server and client. I have read the standard APIs like socket(), bind(), listen(), accept() and read/recv() and even got code below to compile on g++/Linux. </p>
  
  <p>What I want to do is see it working in real-life, I mean run the server code, and then connect to it using the client and send data from client-to-server and vice-a-versa and verify the data  received. All this within two linux boxes(Ubuntu) in my same network segment. I have private IPv4 addresses given to those two Linux machines.</p>
  
  <p>What should be the setup to achieve this and what code changes in the server and client code below,  would be needed to achieve this over the network setup described above? I want to really see it working over network in real time?</p>
  
  <p>Also any further pointers to code, blog articles to study hands-on socket programming/network programming would help.</p>
  
  <pre><code>//TCP SERVER
  #include&lt;sys/socket.h&gt; 
  #include&lt;netinet/in.h&gt; 
  #include&lt;stdio.h&gt; 
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt; 
  #include &lt;arpa/inet.h&gt;
  //#include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  
  main()
  { 
      char buf[100]; 
      socklen_t len; 
      int k,sock_desc,temp_sock_desc; 
      struct sockaddr_in client,server; 
      memset(&amp;client,0,sizeof(client)); 
      memset(&amp;server,0,sizeof(server)); 
      sock_desc = socket(AF_INET,SOCK_STREAM,0); 
      server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); 
      server.sin_port = 7777; 
      k = bind(sock_desc,(struct sockaddr*)&amp;server,sizeof(server)); 
      k = listen(sock_desc,20); len = sizeof(client);
      temp_sock_desc = accept(sock_desc,(struct sockaddr*)&amp;client,&amp;len); 
      while(1)
      {     
          k = recv(temp_sock_desc,buf,100,0);     
          if(strcmp(buf,"exit")==0)        
              break;     
          if(k&gt;0)         
              printf("%s",buf); 
      } close(sock_desc); 
      close(temp_sock_desc); 
      return 0; 
  }
  
  //TCP CLIENT
  #include&lt;sys/socket.h&gt; 
  #include&lt;netinet/in.h&gt; 
  #include&lt;stdio.h&gt; 
  #include&lt;string.h&gt; 
  #include&lt;stdlib.h&gt;
  #include &lt;arpa/inet.h&gt;
  //#include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  
  main()
  { 
      char buf[100]; 
      struct sockaddr_in client; 
      int sock_desc,k; 
      sock_desc = socket(AF_INET,SOCK_STREAM,0);
      memset(&amp;client,0,sizeof(client)); 
      client.sin_family = AF_INET; 
      client.sin_addr.s_addr = inet_addr("127.0.0.1"); 
      client.sin_port = 7777; 
      k = connect(sock_desc,(struct sockaddr*)&amp;client,sizeof(client)); 
      while(1)
      {     
          gets(buf);     
          k = send(sock_desc,buf,100,0);     
          if(strcmp(buf,"exit")==0)         
              break; 
      } 
      close(sock_desc); 
      return 0; 
  }
  </code></pre>