title: connect: Cannot assign requested address in linux
body: <p>I have 2 programs. Basically I am trying to see TCP (SYN, FIN).</p>
  
  <h1><strong>server.c</strong></h1>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  
  /*
   * error - wrapper for perror
   */
  void error(char *msg) {
      perror(msg);
      exit(1);
  }
  
  int main(int argc, char **argv) {
      int parentfd; /* parent socket */
      int childfd; /* child socket */
      int portno; /* port to listen on */
      int clientlen; /* byte size of client's address */
      struct sockaddr_in serveraddr; /* server's addr */
      struct sockaddr_in clientaddr; /* client addr */
      struct hostent *hostp; /* client host info */
      char buf[BUFSIZE]; /* message buffer */
      char *hostaddrp; /* dotted decimal host addr string */
      int optval; /* flag value for setsockopt */
      int n; /* message byte size */
      static int cnt;
  
      /*
       * check command line arguments
       */
      if (argc != 2) {
          fprintf(stderr, "usage: %s &lt;port&gt;\n", argv[0]);
          exit(1);
      }
      portno = atoi(argv[1]);
  
      /*
       * socket: create the parent socket
       */
      parentfd = socket(AF_INET, SOCK_STREAM, 0);
      if (parentfd &lt; 0)
          error("ERROR opening socket");
  
      /* setsockopt: Handy debugging trick that lets
       * us rerun the server immediately after we kill it;
       * otherwise we have to wait about 20 secs.
       * Eliminates "ERROR on binding: Address already in use" error.
       */
      optval = 1;
      setsockopt(parentfd, SOL_SOCKET, SO_REUSEADDR,
              (const void *)&amp;optval , sizeof(int));
  
      /*
       * build the server's Internet address
       */
      bzero((char *) &amp;serveraddr, sizeof(serveraddr));
  
      /* this is an Internet address */
      serveraddr.sin_family = AF_INET;
  
      /* let the system figure out our IP address */
      serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  
      /* this is the port we will listen on */
      serveraddr.sin_port = htons((unsigned short)portno);
  
      /*
       * bind: associate the parent socket with a port
       */
      if (bind(parentfd, (struct sockaddr *) &amp;serveraddr,
                  sizeof(serveraddr)) &lt; 0)
          error("ERROR on binding");
  
      /*
       * listen: make this socket ready to accept connection requests
       */
      if (listen(parentfd, 5) &lt; 0) /* allow 5 requests to queue up */
          error("ERROR on listen");
  
      /*
       * main loop: wait for a connection request, echo input line,
       * then close connection.
       */
      clientlen = sizeof(clientaddr);
      while (1) {
  
          /*
           * accept: wait for a connection request
           */
          childfd = accept(parentfd, (struct sockaddr *) &amp;clientaddr, &amp;clientlen);
          if (childfd &lt; 0)
              error("ERROR on accept");
  
          printf("%d: Connected \n", ++cnt);
          close(childfd);
      }
  }
  </code></pre>
  
  <p>=================================================================================</p>
  
  <h1><strong>Client.c</strong></h1>
  
  <pre><code>#include &lt;sys/types.h&gt;          /* See NOTES */
  #include &lt;sys/socket.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  
  void tcpConnectComplete(int fd)
  {
      uint32_t             index       = 0;
      int                  max_fd, res, optval;
      socklen_t            optlen;
      struct               timeval timeout;
      int                  timeoutCount = 0;
      fd_set               wr_fd;
      while(1)
      {
          max_fd = res = 0;
          optval = -1;
          timeout.tv_sec  = 0;
          timeout.tv_usec = ((5 * 1000) / 3 );
  
          FD_ZERO(&amp;wr_fd);
          FD_SET(fd, &amp;wr_fd);
  
          res = select(fd+1, NULL, &amp;wr_fd, NULL, &amp;timeout);
  
          if(res == 0 || (res &lt; 0 &amp;&amp; errno == EINTR))
          {
              if(++timeoutCount == 3)
              {
                  break;
              }
              continue;
          }
          else if(res &lt; 0)
          {
              break;
          }
  
          if(0 == FD_ISSET(fd, &amp;wr_fd))
          {
              continue;
          }
  
          optval = -1;
          optlen = (socklen_t)sizeof(optval);
          if(-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, &amp;optval, &amp;optlen))
          {
              printf("TCP Connect failed\n");
              return;
          }
          else
          {
              if(0 == optval)
              {
                  printf("TCP Connect success\n");
              }
              else
              {
                  printf("TCP Connect failed: optval=%d\n", optval);
              }
              return;
          }
      }
  }
  
  void tcpConnect(void)
  {
      struct sockaddr_in  addr;
      int                 sockFd = 0;
      int                 enable = 1;
      int                 flags;
  
      /* Created a socket */
      if(-1 == (sockFd = socket(AF_INET, SOCK_STREAM, 0)))
      {
          perror("setsockopt");
          return;
      }
  
      if(setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &amp;enable, sizeof(int)) &lt; 0)
      {
          perror("setsockopt");
          return;
      }
      /* Set non block mode */
      flags = fcntl(sockFd, F_GETFL, NULL);
      if (-1 == fcntl(sockFd, F_SETFL, flags | O_NONBLOCK))
      {
          perror("fcntl");
          return;
      }
  
      /* Bind to source ip and port */
      memset(&amp;addr, 0, sizeof(addr));
      addr.sin_family      = AF_INET;
      addr.sin_port        = htons(39589);
      addr.sin_addr.s_addr = inet_addr("192.168.56.101");
      //addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  
      if(-1 == bind(sockFd, (struct sockaddr*)&amp;addr, sizeof(addr)))
      {
          perror("bind");
          return;
      }
  
  
      /* TCP Connect */
      memset(&amp;addr, 0, sizeof(addr));
      addr.sin_family = AF_INET;
      addr.sin_port = htons(39591);
      addr.sin_addr.s_addr = inet_addr("192.168.56.102");
      //addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  
      if(-1 == connect(sockFd, (struct sockaddr*) &amp;addr, sizeof(addr)))
      {
          perror("connect");
          if (errno == EINPROGRESS)
              tcpConnectComplete(sockFd);
      }
      else
      {
          printf("Connect success\n");
      }
      close(sockFd);
      close(0);
  }
  
  int main(void)
  {
      int i;
  
      for (i = 0; i &lt; 10; i++)  {
          tcpConnect();
      };
  
      return 0;
  }
  </code></pre>
  
  <p>If if run both server and client programs I see the following output (Did not paste the server output since no strange thing was there):</p>
  
  <pre><code>./client 
  connect: Operation now in progress
  TCP Connect success
  connect: Cannot assign requested address
  connect: Operation now in progress
  TCP Connect success
  connect: Cannot assign requested address
  connect: Cannot assign requested address
  connect: Operation now in progress
  TCP Connect success
  connect: Cannot assign requested address
  connect: Cannot assign requested address
  connect: Cannot assign requested address
  connect: Cannot assign requested address
  </code></pre>
  
  <p>My question is why there is a connect failure with 'Cannot assign requested address'? What is the problem with the program?
  Please need help to understand this.</p>