title: C, Sockets: Connection Refused error
body: <p>I've a data acquisition module from which I would like to collect data from the Ethernet port. <strong>I'm getting there in steps</strong>, currently I would like to just <code>connect</code> to server from a client. I've used Beej's guide to get the basic C code. But I just keep getting this connect error <code>connect: Connection refused.</code></p>
  
  <p><strong>This is what I do:</strong></p>
  
  <ol>
  <li><p>The network IP mentioned here is <strong>STATIC IP</strong> which I have configured.</p></li>
  <li><p>The port number is set to 50000 on Server side and from the client side I connect to this IP on the port 50000.</p></li>
  <li><p>I build and run the server side application and then try to connect to it by running a client side application.</p></li>
  </ol>
  
  <p>One doubt about server side, server side application <code>returns</code> before I start the client side application, so should I keep it running (<code>while(1);</code>) so that I can connect to it from the client side?</p>
  
  <p>What's going wrong am I forgetting something here? Help!</p>
  
  <p>I'm pasting the very slightly modified (IP and port numbers are different) Beej's C code for <strong>Server side and Client side</strong> here:</p>
  
  <p><strong>Server.c</strong></p>
  
  <pre><code>/*
  ** server.c
  */
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  
  
  
  int main(int argc, char *argv[])
  {
      // code for a server waiting for connections
      // namely a stream socket on port 3490, on this host's IP
      // either IPv4 or IPv6.
      int sockfd;
      struct addrinfo hints, *servinfo, *p;
      int rv;
      memset(&amp;hints, 0, sizeof hints);
      hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_flags = AI_PASSIVE; // use my IP address
  
      if ((rv = getaddrinfo(NULL, "50000", &amp;hints, &amp;servinfo)) != 0)
      {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
          exit(1);
      }
  
      // loop through all the results and bind to the first we can
      for(p = servinfo; p != NULL; p = p-&gt;ai_next) 
      {
          if ((sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype,
          p-&gt;ai_protocol)) == -1) 
          {
              perror("socket");
              continue;
          }
          if (bind(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1)
          {
              close(sockfd);
              perror("bind");
              continue;
          }
          break; // if we get here, we must have connected successfully
      }
  
      if (p == NULL) 
      {
          // looped off the end of the list with no successful bind
          fprintf(stderr, "failed to bind socket\n");
          exit(2);
      }
  
      freeaddrinfo(servinfo); // all done with this structure
  
  }
  </code></pre>
  
  <p><strong>Client.c</strong></p>
  
  <pre><code>/*
  ** client.c
  */
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  
  int main(int argc, char *argv[])
  {
      // code for a client connecting to a server
      // namely a stream socket to www.example.com on port 80 (http)
      // either IPv4 or IPv6
      int sockfd;
      struct addrinfo hints, *servinfo, *p;
      int rv;
      memset(&amp;hints, 0, sizeof hints);
      hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
      hints.ai_socktype = SOCK_STREAM;
      if ((rv = getaddrinfo("192.168.2.4", "50000", &amp;hints, &amp;servinfo)) != 0) 
      {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
          exit(1);
      }
      // loop through all the results and connect to the first we can
      for(p = servinfo; p != NULL; p = p-&gt;ai_next)
      {
          if ((sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype,
          p-&gt;ai_protocol)) == -1) 
          {
              perror("socket");
  
          continue;
          }
          if (connect(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1) 
          {
              close(sockfd);
              perror("connect");
              continue;
          }
          break; // if we get here, we must have connected successfully
      }
  
      if (p == NULL)
      {
          // looped off the end of the list with no connection
          fprintf(stderr, "failed to connect\n");
          exit(2);
      }
  
      freeaddrinfo(servinfo); // all done with this structure
  
  }
  </code></pre>