title: Loopback example using INADDR_LOOPBACK does not work
body: <p>I am trying to set a loopback socket in C but nothing works. I'm trying to make a function that opens a socket with the loopback address ,send data to socket and from another function read the data but nothing works. I believe that I don't know how to use the functions related to connections. Here is what I accomplished so far:</p>
  
  <pre><code>#include &lt;sys/wait.h&gt;       
  #include &lt;sys/types.h&gt;       
  #include &lt;sys/socket.h&gt;     
  #include &lt;netinet/in.h&gt;       
  #include &lt;netdb.h&gt;           
  #include &lt;unistd.h&gt;             
  #include &lt;stdlib.h&gt;         
  #include &lt;ctype.h&gt;          
  #include &lt;signal.h&gt;         
  #include &lt;iostream&gt;
  #include &lt;cerrno&gt;
  #include &lt;pthread.h&gt;
  
  int internal_s;
  
  void function1(){
      if ((internal_s = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
          perror_exit("socket");
  
      /* Find server address */
      struct sockaddr_in loopback;
      struct sockaddr *serverptr = (struct sockaddr*)&amp;loopback; 
  
      /*Convert port number to integer*/        
      loopback.sin_family = AF_INET;       /* Internet domain */
      loopback.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
      loopback.sin_port=htons(10000);
  
      /* Initiate connection */
      if (bind(internal_s,serverptr, sizeof(loopback))&lt;0)
          perro("bind");
  
      int test=1;
      err=write(internal_s,&amp;test,sizeof(int));
      if(err&lt;0)
          perror(write);
  }
  
  void Open_Internal_sock(int socket_s){
      struct sockaddr_in loopback;
      struct sockaddr *serverptr = (struct sockaddr*)&amp;loopback; 
  
      /*Convert port number to integer*/        
      loopback.sin_family = AF_INET;       /* Internet domain */
      loopback.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
      loopback.sin_port=htons(10000);
  
      /* Initiate connection */
      if (bind(socket_s,serverptr, sizeof(loopback))&lt;0)
          perror("bind");//Invalid argument
      int test;
      if(read(socket_s,&amp;test,sizeof(int))&lt;0)
          perror("read");//herer it prints:Transport endpoint is not connected
  }
  
  int main(){
      function1(i);
      Open_Internal_sock(internal_s);
  }
  </code></pre>