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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:31:43+00:00 2026-05-26T18:31:43+00:00

I am trying to make a simple client-server chat program. On the client side

  • 0

I am trying to make a simple client-server chat program. On the client side I spin off another thread to read any incomming data from the server. The problem is, I want to gracefully terminate that second thread when a person logs out from the main thread. I was trying to use a shared variable ‘running’ to terminate, problem is, the socket read() command is a blocking command, so if I do while(running == 1), the server has to send something before the read returns and the while condition can be checked again. I am looking for a method (with common unix sockets only) to do a non-blocking read, basically some form of peek() would work, for I can continually check the loop to see if I’m done.

The reading thread loop is below, right now it does not have any mutex’s for the shared variables, but I plan to add that later don’t worry! 😉

void *serverlisten(void *vargp)
{
    while(running == 1)
    {
        read(socket, readbuffer, sizeof(readbuffer));
        printf("CLIENT RECIEVED: %s\n", readbuffer);
    }
    pthread_exit(NULL);
}
  • 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-26T18:31:43+00:00Added an answer on May 26, 2026 at 6:31 pm

    You can make socket not blockable, as suggested in another post plus use select to wait input with timeout, like this:

    fd_set         input;
    FD_ZERO(&input);
    FD_SET(sd, &input);
    struct timeval timeout;
    timeout.tv_sec  = sec;
    timeout.tv_usec = msec * 1000;
    int n = select(sd + 1, &input, NULL, NULL, &timeout);
    if (n == -1) {
        //something wrong
    } else if (n == 0)
        continue;//timeout
    if (!FD_ISSET(sd, &input))
       ;//again something wrong
    //here we can call not blockable read
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a simple server/client program pair. On LAN they work
I am trying to make a simple TCP client and host chat program in
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
I am trying to make a simple Client-Server application but when I execute the
I'm trying to make a simple http client server using java. It will show
I'm trying to make a simple UDP client server example in PHP but I
I'm trying to learn UDP, and make a simple file transferring server and client.
I am trying to make a simple twitter client in C. I'm new at
I'm trying to make a simple C# web server that, at this stage, you
I'm trying to make a simple msn client mostly for fun but also for

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.