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

  • SEARCH
  • Home
  • 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 131975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:11:23+00:00 2026-05-11T06:11:23+00:00

I am writing a server program in C wherein every time a client connects,

  • 0

I am writing a server program in C wherein every time a client connects, I create a new pthread to handle the client’s requests.

When all of the threads exit, however, then my program exits, as though a call to exit() has been made. This is a problem – how can I overcome it?

Lets say the server is running, and 2 clients connect. Once those clients disconnect – and thus both threads exit – then my server process exits. What I want is for my server to keep accept()ing socket requests. Ususally this works when I use fork() and accept(). What am I doing wrong such that the parent process is terminating rather than looping infinitely?

Code looks basically like this:

void *talker( void *d ) {    int fd;    char buf[] = 'Hello client';     fd = (int)d;     while( 1 ) {       write( fd, buf, strlen( buf )+1 );       sleep(4);    } }   int main( int argc, char *argv[] ) {      pthread_t thread[50];     int sockfd;    struct sockaddr_in client_addr;    int i = 0;    int s1;     /* ... other declarations */    if (initialize_server_socket(portno, &sockfd) < 0) {      fprintf(stderr, 'Could not initialize socket\n');      exit(-1);   }       while( 1 ) {       s1 = accept( sockfd, (struct sockaddr *)&client_addr, &n );      pthread_create( &(thread[i++]), NULL, talker, (void *)s1 );   }       return(0); } 

Also: this is the same project from a question I already asked (linked below)… though after spending too much time trying to use select() and IPC unsuccessfully, I figured I’d give threads a whirl because of the simplicity of shared address space.

Using pipes in C for parent-child IPC makes program block

Also, much of this code was taken from here: http://www.s5h.net/2006/06/27/pthread-socket-server-in-c/

  • 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. 2026-05-11T06:11:24+00:00Added an answer on May 11, 2026 at 6:11 am

    If you debug it in gdb you’ll see that you’re getting a SIGPIPE, which your program doesn’t handle. You can either install a SIGPIPE handler or ignore SIGPIPE.

    The reason is that your thread is writing to a socket (pipe) that has been closed (by the client), which raises a SIGPIPE. You should check the return value of write() after ignoring SIGPIPE:

    signal(SIGPIPE, SIG_IGN); 

    or handle SIGPIPE. It has nothing to do with 2 clients connecting, if you wait for 4 seconds after the client disconnects, you’ll get a SIGPIPE (in your case).

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

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't know about the "best" way (I'd work at… May 11, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer The encoding of space in a URL as %20 is… May 11, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer lcov supports a command line argument --remove to do exactly… May 11, 2026 at 6:14 pm

Related Questions

I have Virtual PC 2007. I am writing a C# program that will run
I need to invoke tesseract OCR (its an open source library in C++ that
I have an IE BHO that's in development in C#. It is supposed to
I am writing a program in Python that will act as a server and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.