title: Get access to the file descriptor of a socket
body: <p>This is part of a .c file that in included into a tcp server-client message service that has one client per thread; I want to use select inside the server thread to handle new incoming connections or data being written in a buffer but the problem I have is that I need the socket file descriptor to do that of the clients and of the server. </p>
  
  <p>In this part of the code the socket descriptor of the server is inside s->sd but I can seem to access it outside this function because it returns (socket).</p>
  
  <p>What can I do? (large parts of code are omitted to make it readable)</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  typedef struct {
    int sd; //id of the socket??
    char *ip_addr;
    int port;   
    } MySocket;   
  
  Socket tcp_passive_open(int port) 
  {                                                                                      
    MySocket *s = (MySocket *)malloc( sizeof(MySocket) );
    struct sockaddr_in addr;
    s-&gt;sd = socket(PROTOCOLFAMILY, TYPE, PROTOCOL);
  
    memset(&amp;addr, 0, sizeof(struct sockaddr_in));
    addr.sin_family = PROTOCOLFAMILY;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);
  
    if ( bind(s-&gt;sd, (struct sockaddr *)&amp;addr, sizeof(addr)) != 0 ) {
      die("tcp_open_server failed(): bind() failed"); 
    }
  
    s-&gt;port = port;
    s-&gt;ip_addr = NULL;  //INADDR_ANY ...  
    return (Socket)s;  
  }
  </code></pre>
  
  <p>main:</p>
  
  <pre><code>Socket server = tcp_passive_open( PORT );
  FD_SET(SOCKETDESCRIPTOR_SERVER??,&amp;master_socketDescriptors);
  </code></pre>