title: C socket: problem with connect() and/or accept() between clients. 111: Connection refused
body: <p>Hello ladies and gents, I'm having a bit of problem with <code>accept()</code>. I have a multiple clients and one server. The clients can connect and communicate just fine with server. But at one point, I need some clients to be directly connected to each other and I'm having a bit of difficulty there. The clients have bunch of threads going on, where one of them is <code>handle_connection()</code> and it has a <code>while(1)</code>, looping forever to <code>listen()</code> and <code>accept()</code> whatever incoming connections. Whenever a client tries to <code>connect()</code> to other client, <code>connect()</code> returns an error, 111: Connection Refused. I know I have the right IP address and right port (I have specified a port just for between-client connections). The client that is waiting for connection doesn't notice anything, no new connection, nada. I copied some parts of the code, in hopes that someone can point out what I'm doing wrong.  Thanks for any inputs!</p>
  
  <p>This is all client side code.
  <code>void * handle_connections(void * arg)</code> is a thread that loops forever to <code>accept()</code> any incoming connections. My server has a very similar thang going on and it works very well. (not sure why it doesn't work here..) This is the part of client that is waiting for a new incoming connection.
  <code>int handle_request(void * arg, struct message * msg)</code> is called at one point during program and tries to connect to a client that is specified in <code>struct message * msg</code> which includes <code>struct sockaddr_in</code> with IP address and port number and whatever.</p>
  
  <pre><code>#define SERVER_PORT 10000
  #define CLIENT_PORT     3456
  #define MAX_CONNECTION  20
  #define MAX_MSG     50
  
  void * handle_connections(void * arg)
  {
      struct fd_info * info;
      struct sockaddr_in client_address;
      struct timeval timeout;
      fd_set readset, copyset;
  
      bzero((char * ) &amp;client_address, sizeof(client_address)); // copy zeroes into string
      client_address.sin_family = AF_INET;
      client_address.sin_addr.s_addr = htonl(INADDR_ANY);
      client_address.sin_port = htons(CLIENT_PORT);
  
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      rv = listen(sockfd,MAX_CONNECTION);
  
  
      while(1)
      {
          new_sockfd = accept(sockfd, (struct sockaddr *) &amp;client_address, &amp;client_addr_len); //blocks
  
          if (new_sockfd &lt; 0) {
              printf("C: ERROR accept() %i: %s \n", errno, strerror(errno));
              sleep(2);
          } else {
              printf("C: accepted\n");
              FD_SET(new_sockfd, &amp;readset);   // sets bit for new_sockfd to list of sockets to watch out for
              if (maxfd &lt; new_sockfd)
                  maxfd = new_sockfd;
              if (minfd &gt; new_sockfd)
                  minfd = new_sockfd;
          }       //end if else (new_sockfd)
  
      }   // end of the forever while loop
  }
  
  int handle_request(void * arg, struct message * msg)
  {
      char * cname, gname, payload;
      char * command[3];
  
      int i, rv, sockfd, client_addr_len;
  
      struct sockaddr_in client_address;
      struct fd_info * info;
  
      info = (struct fd_info *) arg;
      sockfd = info-&gt;sock_fd;
  
  
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd == -1)
      {
          printf("HR: ERROR socket() %i: %s \n", errno, strerror(errno));
          break;
      }
      else if (sockfd &gt; 0)
      {
          printf("HR: new socks is %i \n", sockfd);
          printf("HR: sin_family is %i: %i\n", msg-&gt;peer.client_address.sin_family, msg-&gt;peer.client_address.sin_port);
          //*************************************************************
          //this is the part that returns error 111: Connection refused!!!
          //*************************************************************
          rv = connect(sockfd, (struct sockaddr *) &amp;msg-&gt;peer.client_address, sizeof(struct sockaddr));
          if (rv == -1) 
          {
              printf("HR: ERROR: connect() %i: %s \n", errno, strerror(errno));
              printf("HR: at %li \n", msg-&gt;peer.client_address.sin_addr.s_addr);
              break;
          }
          else if (rv &gt; 0)
          {
              info-&gt;max_fd = sockfd;
              printf("HR: connected successfully!! \n");
          }
      }   
  }
  </code></pre>