Why do we ned to close listen socket in child process in a concurrent server ?

Hi Experts,
My question is " What happens  if we don't close listen socketin child process?. It is mandatory ?
Generally we write in  following manner.
bind(listensock, (struct sockaddr *) &sAddr, sizeof(sAddr));
Afterward, we put the socket into listening mode to listen for incoming connections.
    listen(listensock, 5);
We then call accept() and allow it to block waiting for connection requests from
 clients. After accept returns, we call fork() to create a new process. If it returns 0, then we are in the child process; otherwise, the PID of the new child is returned.
     while (1) {
      newsock = accept(listensock, NULL, NULL);
      if ((pid = fork()) = = 0) {
     printf("%s\n", buffer);
Once in the child process, we close the listening socket.
We know  that all descriptors are copied from the parent process to the child.
The child process does not need the listening socket any longer, so we close the
child's reference on that socket.
     close(listensock);
     nread = recv(newsock, buffer, 25, 0);
     buffer[nread] = '\0';
     send(newsock, buffer, nread, 0);
     close(newsock);
     exit(0);
    }


LVL 3
DineshJolaniaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ravenplCommented:
> After accept returns, we call fork() to create a new process.
fork may return -1 as well, which is error (no resources)

> The child process does not need the listening socket any longer, so we close the child's reference on that socket.
And the father sould close the accepted socket as well.

> My question is " What happens  if we don't close listen socketin child process?. It is mandatory ?
Well, if You will not use it, nothing wrong should happen, but close to avoid errors and confusions.

It's like - neverthless the system will close all sockets and free all memory(except shared one), you should do it by Yourself before exit - right?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DineshJolaniaAuthor Commented:
Thanks  a lot ravenpl. I carried this confusion for years.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.