Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Socket descriptors not freed on close (http://www.go4expert.com/forums/socket-descriptors-freed-close-t29958/)

priyatendulkar 3Jan2014 13:03

Socket descriptors not freed on close
Hi All

I am facing a wired problem.The socket descriptor is not free (entry persists in /proc/pid/fd)
even though it is closed.

Code snippet is

int  ctrlSock = socket (AF_INET, SOCK_STREAM, 0);
    if (ctrlSock < 0)
        return (-1);

  struct sockaddr_in ctrlAddr;

    ctrlAddr.sin_family      = AF_INET;
    ctrlAddr.sin_addr.s_addr = INADDR_ANY;
    ctrlAddr.sin_port        = htons (0);
    if (bind (ctrlSock, (struct sockaddr *)&ctrlAddr, sizeof (ctrlAddr)) < 0)
        close (ctrlSock);
        return (-1);

  close (ctrlSock);

Here , even after close, the descriptor entry exits in /proc/pid/fd

priyatendulkar 7Jan2014 12:16

Re: Socket descriptors not freed on close

The issue has been resolved..
Actually the sockets were getting freed but same fd was getting reallocated immediately.

As the same socket fd were reused, it was thought as if the descriptor was not getting released...

It was verified using netstat, /proc/<pid>/fd,/proc/net/tcp,/proc/net/raw and strace.

/proc/<pid>/fd, /proc/net/tcp OR /proc/net/raw... (depending on the socket type) are very good debugging tools for verifying socket usage in process.

All times are GMT +5.5. The time now is 16:43.