title: Select Always Returns 0 When run within different program
body: <p>I am facing a strange problem, maybe it's straightforward but my lack of much socket knowledge is not helping either.</p>
  
  <p>I have created a socket program and it works perfectly when run in a separate program i.e within main separately as an independent exe. As you can see in the code, the socket fd returned is always 3 when run in this separate program.</p>
  
  <p>When I copy the same piece of code and integrate this to another server program which which has it's own fd's opened, the select always returns 0 no matter what. The fd created in this case with my program is always 6.</p>
  
  <p>See my program below. there is no problem in this piece of code, it's just when this is been run inside another server, the select always returns 0.</p>
  
  <p>Can I create a new UDP socket fd when there is already one existing in the program. 
  Why is this happening when my socket fd is different then already opened within the program.</p>
  
  <p>Why is this happening, can anyone please guide me what to do or any pointers would be helpful.</p>
  
  <pre><code>#define MY_TIMEOUT_S    5   
      int mySock;
      if ((mySock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
          perror("Cannot create UDP socket");
          return -1;
      }
  
      struct sockaddr_in myAddr;
      myAddr.sin_family       = AF_INET;
      myAddr.sin_port         = 0;
      myAddr.sin_addr.s_addr  = INADDR_ANY;
      memset(&amp;(myAddr.sin_zero), '\0', 8);
      if (bind(mySock, (struct sockaddr*)&amp;myAddr, 
              sizeof(struct sockaddr)) == -1) {
          perror("Cannot bind UDP socket to desired port");
          close(mySock);
          return -1;
      }
  
      // here send to remote target host on udp socket and port 
  
      char buf[MAX_UDP_PACKET_SIZE];
      struct sockaddr_in theirAddr;
      int addrLen = sizeof(struct sockaddr);
  
      fd_set readSet, curReadSet;
      FD_ZERO(&amp;readSet);
      FD_SET(mySock, &amp;readSet);
      int maxFD = mySock;
      while (true) {
          struct timeval timeOutTV = {MY_TIMEOUT_S, 0};
  
          memcpy(&amp;curReadSet, &amp;readSet, sizeof(fd_set));
  
          int selectRet = select(maxFD+1, &amp;curReadSet, NULL, NULL, &amp;timeOutTV);
          cout&lt;&lt;"select value =" &lt;&lt;selectRet&lt;&lt;endl;
  
          if (selectRet == -1) {
              if (errno == EINTR) {
                  continue; // Interrupted by signal, retry
              }
              close(mySock);
              return -1;
  
          } else if (selectRet == 0) {
              close(mySock);
              return -1;
          }
          if (FD_ISSET(mySock, &amp;curReadSet)) {
              int numBytes = recvfrom(mySock, buf, MAX_UDP_PACKET_SIZE, 0,
                      (struct sockaddr*)&amp;theirAddr, (socklen_t*)&amp;addrLen);
              if (numBytes == -1) {
                  close(mySock);
              }   
  
          }
      }
  </code></pre>
  
  <p>Adding some details from lsof output</p>
  
  <p>1) This is the target where I have to send my data
   sudo lsof -a -p 16061</p>
  
  <p>target 16061 root    3r  FIFO    0,8      0t0 153467 pipe<br>
  target 16061 root    4u  IPv4 153470      0t0    UDP *:3290<br>
  target 16061 root    5u  sock    0,6      0t0 153471 can't identify protocol<br>
  target 16061 root    6u  IPv4 153472      0t0    TCP *:3290 (LISTEN)<br>
  target 16061 root    7u   raw             0t0 153473 00000000:0001->00000000:0000 st=07</p>
  
  <p>2) This is my  above piece of code when run in a separate exe, in which only this is the code running inside main
  as the FD=3 works fine</p>
  
  <p>sudo lsof -a -p 16112</p>
  
  <p>myProg 16112 root    0u   CHR  136,4      0t0      7 /dev/pts/4<br>
  myProg 16112 root    1u   CHR  136,4      0t0      7 /dev/pts/4<br>
  myProg 16112 root    2u   CHR  136,4      0t0      7 /dev/pts/4<br>
  myProg 16112 root    3u  IPv4 153564      0t0    UDP *:60503  </p>
  
  <p>3) This is the web server where I integrated my same above piece of code which is working independently, but not now</p>
  
  <p>this is the output of my web server running</p>
  
  <p>webServ 18431 root    3u  unix 0xc09f9b00      0t0 157975 socket<br>
  webServ 18431 root    4u  IPv4     157976      0t0    UDP *:8080<br>
  webServ 18431 root    5u  IPv4     157977      0t0    TCP *:8080 (LISTEN)  </p>
  
  <p>now I send request and on the server side my new integrated code is executed and I put an infinite loop and did not close the socket just to show the lsof output</p>
  
  <p>4) Output after my integration</p>
  
  <p>webServ 18449 root    3u  unix 0xc09f9b00      0t0 157975 socket<br>
  webServ 18449 root    4u  IPv4     157976      0t0    UDP *:8080<br>
  webServ 18449 root    5u  IPv4     157977      0t0    TCP *:8080 (LISTEN)<br>
  webServ 18449 root    6u  IPv4     158389      0t0    UDP *:48057  </p>
  
  <p>The only difference is when integrated to the server the socket FD is different that's the only difference.</p>
  
  <p>Hope this helps in answering my question.</p>
  
  <p>I am not sure why this is happening. Please help</p>