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 6377321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:51:22+00:00 2026-05-25T01:51:22+00:00

This has been perplexing me for a couple of days, so I decided to

  • 0

This has been perplexing me for a couple of days, so I decided to stop struggling with it and throw it open to a wider audience.

I have a server written in C that creates a worker thread for each client (the number of clients is expected to be very small). Each thread has only two file descriptors associated with it, one of which is the socket, so I decided to use select() for simplicity. The clients never send data to the server, but I set the activity bit for the socket file descriptor in the readfds argument before calling select() as a means of detecting when the client has closed the connection.

The clients are all instances of a Java program that appears to be opening a stream socket connection appropriately (my low-level Java networking experience is not that strong and I didn’t write the client). In my test environment, I run the Java program from within an instance of Eclipse editor, and kill it using the stop button to simulate shutdown.

Server and client are both running under Linux.

Communication behaves according to my expectation except when the client shuts down. In this circumstance, I would expect the call to select() in the server’s worker thread to return with the ready bit associated with the socket file descriptor set in the readfds argument, at which point a call to read() should return 0 bytes, indicating that the peer has closed the connection.

What I see is that the expected behavior occurs randomly, while in other cases, the call to select() does not return and the server eventually gets (errno == EPIPE) after write() returns -1 when it has data that it decides to send to the dead client. In particular, the first connection to the server always behaves correctly, while the second one always fails. This is not really blocking my progress because the server simply logs an error and cleans up the connection when it detects the condition, but this is annoying me and making me wonder if there is some subtle point that I’m forgetting here because it’s been a long time since I’ve programmed at this level.

EDIT: The code is kind of scattered in various small chunks across several translation units, so I’ll try to cook it down to something that is remotely readable (note that lots of error checking code has also been removed):

/* A chunk of code related to the handing of a listen socket */
int clientSockFd = accept(listenerFd, &addr, &addrlen);
int optVal = 1;
setsockopt(clientSockFd, IPPROTO_TCP, TCP_NODELAY, &optVal, sizeof(optVal));
ThreadDataStruct *useful = (ThreadDataStruct *)malloc(sizeof(useful));
/* Add in some useful stuff */
useful->fd = clientSockFd;
pthread_create(&newThreadId, NULL, workerThread, (void *)useful);
/* End of listener handling code */

/* ... */

static void *workerThread(void *arg) {
    int clientSockFd = ((ThreadDataStruct *)arg)->fd;
    /* Unpack some other useful stuff from arg */
    int maxFd = LargestFdUsedByThisThread + 1;
    while (1) {
        fd_set readableReady;
        FD_ZERO(&readableReady);
        FD_SET(clientSockFd, &readableReady);
        int readyFdCount = select(maxFd, &readableReady, NULL, NULL, NULL);
        if (FD_ISSET(clientSockFd, &readableReady)) {
            /* Clean up various data structures associated with the thread */
            return NULL;
        }
        /* Do something else useful but irrelevant to this problem */
    }
}
  • 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-25T01:51:23+00:00Added an answer on May 25, 2026 at 1:51 am

    The three most likely causes of this problem are:

    Something is amiss in the select call. For example, if LargestFdUsedByThisThread is less than clientSockFd, the select call will not unblock.

    The connection never closed. For example, if another process also had a reference to the underlying TCP connection, killing the client process will not close the connection so long as that other process still has a reference to it. This happens commonly if another process forked off the client process after it accepted the connection and didn’t close its handle to the connection.

    You have a threading problem. I noticed that you pass a pointer to the newly-created thread. If you then modify the data in the main thread, the newly-created thread may read the new data rather than the data at the time you constructed it. This can cause the thread to see the wrong value for clientSockFd. A good pattern to avoid this error is:

    1) Accept a connection and get ready to create a thread.
    2) Allocate an object to hold the parameters the thread needs.
    3) Create the thread passing it a pointer to that object.
    4) In the new thread, free the parameter object when done with it.

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

Sidebar

Related Questions

This has been bugging me for a couple days now. A list has content
This has been really killing me for the last couple of days now. I
This has been driving me crazy the past couple of days. I'm trying to
This one has been perplexing me on a couple recent sites I've worked on,
This has been stumping me for a few days now. I have a stored
This has been a rather problematic issue on numerous occasions. We have alot of
This has been driving me crazy for the past few minutes I have a
This has been bugging me for more than two days now, so i thought
This has been stumping me for days. test should check inbox for messages do
this has been giving me a headache for a couple weeks now -- hope

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.