Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6170905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:07:16+00:00 2026-05-23T23:07:16+00:00

I’m doing a multiclient server that accepts a connection, forks, and gives the connection

  • 0

I’m doing a multiclient server that accepts a connection, forks, and gives the connection to the child so it can handle it. It’s a multiclient server, so it has multiple children.

The main process is in an infinite while which makes a select to find out if there is a new incoming connection or if a child is trying to communicate.

The problem arises when I close a client (which is connected to a son of the main server):
it randomly happens that the client connection is closed, and the select gets unblocked because supposedly the internal socket (which handles the incoming connection between the children and the main server) was modified, but as far as I am concerned that is not true.
What actually happened was that the client closed the connection and the child just died.

Can anyone give me a clue of what it’s going on here? I’m quite lost.

This is the code of the infinite loop in the main server:

while (1) {
    /*inicializo variables para el select*/
    sflag = 0;
    FD_ZERO(&readfds);
    FD_SET(sockfd, &readfds);
    FD_SET(sockifd,&readfds);
    max = (sockfd > sockifd) ? sockfd : sockifd;
    for(aux = isockets; aux != NULL; aux = aux -> next){
        FD_SET(aux -> sd, &readfds);
        max = (max > aux -> sd) ? max : aux -> sd;
    }

    printf("pre-select\n");
    select(max + 1, &readfds, NULL, NULL, NULL);
    /*checkeo si salio por actividad en un socket interno*/
    for (aux = isockets; aux != NULL; aux = aux -> next){
        if (FD_ISSET(aux -> sd, &readfds)){
            printf("comunicacion con el socket: %d\n", aux -> sd);
            sflag = 1;
            actsocket = aux -> sd;
            break;
        }
    }
    if (sflag == 1){//mensaje de un hijo
        n = recv(actsocket, buffer, sizeof(buffer), 0);
        if (n == 0) {
            printf("conexion cerrada con el socket interno: %d\n", actsocket);
            close(actsocket);
            isockets = free_sock(isockets, actsocket);
            printf("isockets: %p\n", isockets);
        }
        else if(n < 0) error ("ERROR en comunicacion interna");
        else printf("mensaje del boludon: %s\n", buffer);
    }   
    else if (FD_ISSET(sockifd, &readfds)){// un hijo inicia conexion interna
        printf("antes de accpet interno\n");
        newisockfd = accept(sockifd, (struct sockaddr *) &ucli_addr, &uclilen);
        printf("nueva conexion interna, socketfd: %d\n", newisockfd);
        isockets = add_socket(isockets,newisockfd, 0);
        recorre(isockets);
        if (newisockfd < 0) error ("ERROR en accept unix, padre");
    }
    else if (FD_ISSET (sockfd, &readfds)){/*conexion entrante*/
        printf("conexion entrante\n");
        newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
            if (newsockfd < 0) error("ERROR on accept");
            pid = fork();

        if (pid < 0) error("ERROR on fork");
            if (pid == 0){//hijo
                    close(sockfd);
            dostuff(newsockfd, path, tm,fd[0]);
                    exit(0);

            }
            else {  //padre
            printf("conexion aceptada, pid hijo %d\n", pid);
            close(newsockfd);
        }
    }
    }

So, randomly, when I close a connection, the select unblock as if “sockifd” was modified, but it wasn’t. Don’t know why is doing that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:07:17+00:00Added an answer on May 23, 2026 at 11:07 pm

    One thing that is wrong in your code is that you’re not checking the return value of select.

    If select is interrupted by a signal (returns -1 with errno = EINTR, for example SIGCHLD if one of the children died), then the contents of &readfds is undefined, and thus must not be read. (See for example the Linux man page for select.)

    So check the return value if select, and loop right back to it without going through the &readfds processing if there’s a temporary error like EINTR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.