socket file descriptor leak?

Discussion in 'iOS Development' started by jbonedev, Sep 17, 2009.

  1. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    It seems that after I close() an fd returned by accept() this fd is never reused.

    I've got the following code, which when I test on a linux box works as I expect. On my ipod accept() keeps returning higher and higher file descriptors until it reaches 256 and then seems to lock up.

    I'm doing (error handling omitted):

    Code:
    sock = socket(AF_INET, SOCK_STREAM, 0)
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
                                            &so_param, sizeof(int)
    bind(sock, (struct sockaddr *)&server_addr,
                                sizeof(struct sockaddr)
    listen(sock, 5)
    
    while(1) {
        i = accept(sock, NULL, NULL);
        // Do some stuff read and write to socket
    
        // Call shutdown()
        shutdown(i, SHUT_RDWR)
                        
                        // Read rest of packets...
                        while(1)
                        {
                            int bytes = recv( i, recv_data, BUFF_SIZE, 0 );
                            if ( bytes > 0 ) {
                                // could do something with data if we wanted...
                            } else if ( bytes == 0 ) {
                                break;
                            } else {
                                // Error reading bytes...
                                break;
                            }
                        }
        // Call close
        close(i)
    }
    
    
    I added shutdown() and the read-rest-of-packets stuff after some googling, but still I never see the close()'ed fds reused.

    Can anyone tell me what I'm doing wrong here?

Share This Page