We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

DineshJolania
on
Medium Priority
610 Views
Last Modified: 2013-12-26
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);
    }


Comment
Watch Question

Top Expert 2005
Commented:
> 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?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks  a lot ravenpl. I carried this confusion for years.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.