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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:10:18+00:00 2026-06-05T06:10:18+00:00

I’m currently programming a Basic Client Server messaging program in C. The problem I

  • 0

I’m currently programming a Basic Client Server messaging program in C.
The problem I have is with the Server side code.
The main part of the code is a while loop that checks for new incoming socket connections from clients.
If a client connects, a new thread is spawned that handles incoming messages. this function I use tcp_wait_for_connection( server ); is blocking.

so my question is if it’s possible to break the while loop from one of these threads without having to use Exit() so that I can close the sockets

(I’m new to Stack overflow so I did not know if I should post my complete code on here or not, I placed the most relevant part below, if you need more I’ll edit the post.

Thanks

The code goes like this:

while(1) {

    client = (Socket *) malloc( sizeof(Socket) ); //allocate memory for the new client socket
    if ( client == NULL ){
        perror("Allocation of memory for the new client has failed");
        return 1;
    }
    //wait for a client to connect
    *client = tcp_wait_for_connection( server );
    printf("Incoming client connection\n");
    //get the socket descriptor for the new client socket
    sd = get_socket_descriptor(client);
    //insert the new client socket into the client list with the sd as ID
    InsertElement(&cl, (Element)client, sd);

    p_thread = (pthread_t *) malloc( sizeof(pthread_t) ); //allocate memory for the new thread handler
    if ( p_thread == NULL ){
        perror("Allocation of memory for the new thread has failed");
        return 1;
    }
    //insert the new thread handler into the thread handler list
    //with the sd of the corressponding client socket as ID
    InsertElement(&tl, (Element)p_thread, sd);
    //create the new thread
    pthread_create(p_thread, NULL, HandleClient, (void*)p_thread);

}
tcp_close( *client );
tcp_close( server );
  • 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-06-05T06:10:19+00:00Added an answer on June 5, 2026 at 6:10 am

    No, you can’t do that. I’d suggest that you just call the function that closes the sockets from the child threads. Use a pthread_mutex to make sure you’re only doing it from one thread at a time. Also, in main, handle errors in tcp_wait_for_connection that happen because the socket was closed properly.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

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.