title: Unable to create Socket after creating 1020 times to communicate
body: <p>I am following the socket programming tutorial from this link <a href="http://www.thegeekstuff.com/2011/12/c-socket-programming/" rel="nofollow noreferrer">Socket programming</a>. and trying to develop an application where client will receive Network Time from Server and print the time .  Here is how the program works-</p>
  <p>Server is waiting for incoming connection and while client try to connect with server , server accept the connection and server sends the time to client. After receiving through socket client print the time.</p>
  <p>After every message send to client , server close the connection and wait for next. <strong>Client is able to communicate with server</strong> <strong>for 1020 times</strong>. <strong>If I try to communicate  more than 1020 time from client to server then I got</strong>  <strong>Error- Unable to Create Socket</strong></p>
  <p>I am trying to develop the application where client can communicate with server <strong>infinite times</strong> or <strong>atleast 20000 times.</strong></p>
  <p><strong>I am suspecting , may be memory is not freed while creating socket in client side.</strong></p>
  <pre><code>Sample output :
  
  counter=1017 Sun Jul  6 03:19:48 2014
  counter=1018 Sun Jul  6 03:19:48 2014
  counter=1019 Sun Jul  6 03:19:48 2014
  counter=1020 Sun Jul  6 03:19:48 2014
  </code></pre>
  <p><em><strong>Error : Could not create socket</strong></em> (WHY ??)</p>
  <p>Here is my code</p>
  <h1>Server.c</h1>
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #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;time.h&gt; 
  
  int main(int argc, char *argv[])
  {
      int listenfd = 0, connfd = 0;
      struct sockaddr_in serv_addr; 
  
      char sendBuff[1025];
      time_t ticks; 
  
      listenfd = socket(AF_INET, SOCK_STREAM, 0);
      memset(&amp;serv_addr, '0', sizeof(serv_addr));
      memset(sendBuff, '0', sizeof(sendBuff)); 
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      serv_addr.sin_port = htons(5000); 
  
      bind(listenfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)); 
  
      listen(listenfd, 10); 
  
      while(1)
      {
          connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 
  
          ticks = time(NULL);
          snprintf(sendBuff, sizeof(sendBuff), &quot;%.24s\r\n&quot;, ctime(&amp;ticks));
          write(connfd, sendBuff, strlen(sendBuff)); 
  
          close(connfd);
        //  sleep(1);
       }
  }
  </code></pre>
  <blockquote>
  <h2>Client.c</h2>
  </blockquote>
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;arpa/inet.h&gt; 
  
  int main(int argc, char *argv[])
  {
      int sockfd = 0, n = 0;
      char recvBuff[1024];
      struct sockaddr_in serv_addr; 
  
      if(argc != 2)
      {
          printf(&quot;\n Usage: %s &lt;ip of server&gt; \n&quot;,argv[0]);
          return 1;
      } 
  
  int counter=0;
  
  while(counter &lt;2000)
  {
      memset(recvBuff, '0',sizeof(recvBuff));
      if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      {
          printf(&quot;\n Error : Could not create socket \n&quot;);
          return 1;
      } 
  
      memset(&amp;serv_addr, '0', sizeof(serv_addr)); 
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(5000); 
  
      if(inet_pton(AF_INET, argv[1], &amp;serv_addr.sin_addr)&lt;=0)
      {
          printf(&quot;\n inet_pton error occured\n&quot;);
          return 1;
      } 
  
      if( connect(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0)
      {
         printf(&quot;\n Error : Connect Failed \n&quot;);
         return 1;
      } 
  
      while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) &gt; 0)
      {
          recvBuff[n] = 0;
          printf(&quot;counter=%d &quot;,counter);
          fputs(recvBuff, stdout);
         /*
          if(fputs(recvBuff, stdout) == EOF)
          {
              printf(&quot;\n Error : Fputs error\n&quot;);
          }
          */ 
      } 
      
  counter++;
  }
      if(n &lt; 0)
      {
          printf(&quot;\n Read error \n&quot;);
      } 
  
      return 0;
  }
  </code></pre>
  <p>I am using gcc under Ubuntu. Any help to understand the reason and resolve and run the program for around 20000 will be highly appreciated.  Thanks in advance.</p>