title: Is there a connection limitation using LWIP？
body: <p>I'm writing a TCP server using Lwip Protocol, normally the server can connect to multiple TCP clients. However I have a problem , the server can't connect more than three active connections. The 4th or more clients could not connect for unknown reasons. I looked around the forums and tried increasing the number limit for <code>MEMP_NUM_TCP_PCB</code> in opt.h (lwip-x86_64\include\lwip\opt.h) but it didn't help.</p>
  
  <p>Following is my code, and my questions is whether is there a max connection limitation using LWIP ? Maximum number of connections is 3 or more?</p>
  
  <pre class="lang-c prettyprint-override"><code>//server code 
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.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;
  #define MYPORT 1234    // the port users will be connecting to
  #define BACKLOG 2// how many pending connections queue will hold
  #define BUF_SIZE 1024
  int fd_A[BACKLOG];    // accepted connection fd
  int conn_amount;      // current connection amount
  void showclient()
  {
      int i;
      printf("-----&gt; client amount: %d\n", conn_amount);
      for (i = 0; i &lt; BACKLOG; i++) {
      printf("BACKLOG%d ---&gt; fd = %d\n", i, fd_A[i]);
      }
      printf("\n\n");
  }
  int main(void)
  {
      sleep(1);
      printf("start server\n");
      int sock_fd, new_fd;             // listen on sock_fd, new connection on new_fd
      struct sockaddr_in server_addr;  // server address information
      struct sockaddr_in client_addr;  // connector's address information
      socklen_t sin_size;
      int yes = 1;
      char buf[BUF_SIZE];
      int ret;
      int i;
      if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("socket");
      exit(1);
      }
      //if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int)) == -1) {
      /*if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int)) == -1) {
      //perror("setsockopt");
      //exit(1);
      }
      if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) == -1) {
      printf("Set server socket nonblock failed\n");
      exit(1);
      }*/
      memset(&amp;server_addr, 0, sizeof(server_addr));
      server_addr.sin_family = AF_INET;         // host byte order
      server_addr.sin_port = htons(MYPORT);     // short, network byte order
      server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // automatically fill with my IP
      //memset(server_addr.sin_zero, '0', sizeof(server_addr.sin_zero));
      if (bind(sock_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) == -1) {
      perror("bind");
      exit(1);
      }
      printf("listen...\n");
      if (listen(sock_fd, BACKLOG) == -1) {
      perror("listen");
      exit(1);
      }
      printf("listen port %d\n", MYPORT);
      fd_set fdsr, wfds, efds;
      int maxsock;
      struct timeval tv;
      conn_amount = 0;
      sin_size = sizeof(client_addr);
      maxsock = sock_fd;
      while (1) 
      {
      sleep(1);  // it's necessary
      // initialize file descriptor set
      FD_ZERO(&amp;fdsr);
      FD_ZERO(&amp;wfds);
      //FD_ZERO(&amp;efds);
      FD_SET(sock_fd, &amp;fdsr);  // add fd
      FD_SET(sock_fd, &amp;wfds);  // add fd
      //FD_SET(sock_fd, &amp;efds);  // add fd
      // timeout setting
      tv.tv_sec = 30;
      tv.tv_usec = 0;
      // add active connection to fd set
      for (i = 0; i &lt; BACKLOG; i++) {
          if (fd_A[i] != 0) {
              FD_SET(fd_A[i], &amp;fdsr);
          }
      }
      //printf("before select!!!!!!!!!!!! ret = %d\n", ret);
      if ((select(maxsock + 1, &amp;fdsr, &amp;wfds, (fd_set*) 0, (struct timeval*) 0)) &lt; 0) {
          perror("select");
          break;
      } 
  
      // check every fd in the set
      for (i = 0; i &lt; conn_amount; i++) 
      {
          if (FD_ISSET(fd_A[i], &amp;fdsr)) // check which fd is ready
          {
              ret = recv(fd_A[i], buf, sizeof(buf), 0);
              if (ret &lt;= 0) 
              {        // client close
                  printf("ret : %d and client[%d] close\n", ret, i);
                  close(fd_A[i]);
                  FD_CLR(fd_A[i], &amp;fdsr);  // delete fd 
                  fd_A[i] = 0;
                  conn_amount--;
              }
              else 
              {        // receive data
                  if (ret &lt; BUF_SIZE)
                      memset(&amp;buf[ret], '\0', 1); // add NULL('/0')
                  printf("client[%d] send:%s\n", i, buf);
              }
          }
      }
      // check whether a new connection comes
      if (FD_ISSET(sock_fd, &amp;fdsr))  // accept new connection 
      {
          new_fd = accept(sock_fd, (struct sockaddr *)&amp;client_addr, &amp;sin_size);
          if (new_fd &lt;= 0) 
          {
              perror("accept");
              continue;
          }
          // add to fd queue
          if (conn_amount &lt; BACKLOG) 
          {
              fd_A[conn_amount++] = new_fd;
              printf("------&gt; new connection client[%d] %s:%d\n", conn_amount,
                      inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
              if (new_fd &gt; maxsock)  // update the maxsock fd for select function
                  maxsock = new_fd;
          }
          else 
          {
              printf("max connections arrive, exit\n");
              send(new_fd, "bye", 4, 0);
              close(new_fd);
              break;   
          }
      }
      //showclient();
      }
      // close other connections
      for (i = 0; i &lt; BACKLOG; i++) 
      {
      if (fd_A[i] != 0) 
      {
          close(fd_A[i]);
      }
      }
      exit(0);
  }
  
  //client code
  #include   &lt;sys/stat.h&gt;   
  #include   &lt;sys/types.h&gt;   
  #include   &lt;sys/socket.h&gt;   
  #include   &lt;stdio.h&gt;   
  #include   &lt;malloc.h&gt;   
  #include   &lt;netdb.h&gt;   
  #include   &lt;fcntl.h&gt;
  #include   &lt;unistd.h&gt;
  #include   &lt;netinet/in.h&gt;
  #include   &lt;arpa/inet.h&gt;
  #include   &lt;string.h&gt;
  #define    RES_LENGTH  10240 
  int     connect_socket(char * server,int serverPort);
  int     send_msg(int sockfd,char * sendBuff);
  char *  recv_msg(int sockfd);
  int     close_socket(int sockfd);
  int main(int argc, char ** argv)
  {
      int   sockfd[1024] = {0};
      char  sendMsg[30]="zhangchengfei\r\n\r";
      char* res;
      int   port = 1234;
      char  ip[128] = {0};
      strncpy(ip, "10.107.19.62", 128);
      if(argc &gt; 2)
      {
      strncpy(ip, argv[1], 128);
      port = atoi(argv[2]);
      printf("Input IP: %s, port : %d\n", ip, port);
      }
      else if(argc &gt; 1)
      {   
      //port = atoi(argv[1]);
      //printf("Input port : %d\n", port);
      //
      int num_client = atoi(argv[1]);
      int i = 0;
      for (i = 0; i &lt;= num_client; i++) {
      printf("start connect %d\n", i + 1);           
      sockfd[i]=connect_socket(ip, port);
      printf("connect %d OK\n", i + 1);   
      getchar();
      }
      for (i = 0; i &lt;= num_client; i++) {
      send_msg(sockfd[i], sendMsg);
      /* res=recv_msg(sockfd); */
  
      printf("client %d send msg = %s\n", i + 1, sendMsg);
      //printf(res);
      //free(res);
      getchar();
      }
      for (i = 0; i &lt;= num_client; i++) {
      close_socket(sockfd[i]);
      printf("close socket %d\n", i + 1);
      }
      }
      return 0;
  }
  
  int    connect_socket(char * server,int serverPort){
      int    sockfd=0;
      struct    sockaddr_in    addr;
      struct    hostent        * phost;
  
      if((sockfd=socket(AF_INET,SOCK_STREAM,0))&lt;0){
      herror("Init socket error!");
      return -1;
      }
      bzero(&amp;addr,sizeof(addr));
      addr.sin_family = AF_INET;
      addr.sin_port = htons(serverPort);
      addr.sin_addr.s_addr = inet_addr(server);
  
      if(addr.sin_addr.s_addr == INADDR_NONE){
      phost = (struct hostent*)gethostbyname(server);
      if(phost==NULL){
          herror("Init socket s_addr error!");
          return -1;
      }
      addr.sin_addr.s_addr =((struct in_addr*)phost-&gt;h_addr)-&gt;s_addr;
      }
      if(connect(sockfd,(struct sockaddr*)&amp;addr, sizeof(addr))&lt;0)
      {
      perror("Connect server fail!");
      return -1; 
      }
      else
      return sockfd;
  }
  
  
  int send_msg(int sockfd,char * sendBuff)
  {
      int sendSize=0;
      if((sendSize=send(sockfd,sendBuff,strlen(sendBuff),0))&lt;=0){
      herror("Send msg error!");
      return -1;
      }else
      return sendSize;
  }
  
  
  char* recv_msg(int sockfd){
      char * response;
      int  flag=0,recLenth=0;
      response=(char *)malloc(RES_LENGTH);
      memset(response,0,RES_LENGTH);
  
      for(flag=0;;)
      {
      printf("======recv data:\n");
      if(( recLenth=recv(sockfd,response+flag,RES_LENGTH-flag,0))==-1 )
      {
          free(response);
          printf("Return value : %d\n", recLenth);
          perror("Recv msg error : ");
          return NULL;
      }
      else if(recLenth==0)
          break;
      else
      {
          printf("%d char recieved data : %s.\n", recLenth, response+flag);
          flag+=recLenth;
          recLenth=0;
      }
      }
      printf("Return value : %d\n", recLenth);
      response[flag]='0';
      return response;
  }
  
  
  int close_socket(int sockfd)
  {
      close(sockfd);
      return 0;
  }
  </code></pre>