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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:21:32+00:00 2026-06-14T07:21:32+00:00

hi there i’m working on a multithread Server (TCP) in C and i have

  • 0

hi there i’m working on a multithread Server (TCP) in C and i have a little issue about it. Everything works fine, more than one thread can connect to the server but whenever a client writes “exit” (which is a condition for a client when he/she writes “exit” string he/she goes to disconnect from the server) serves shutsdown itself also. So the communication through other threads get lost. However, logically it should be waiting for other clients even some current clients get disconnected. Here is a part of main and server is in a endless loop for waiting clients. hsock is id of socket belongs to server and csock is the id of clients.

main(){
.
.
.


while(1){

    if(counter==0)
        printf("waiting for a connection\n");

    csock = (int*)malloc(sizeof(int));

    if((*csock = accept( hsock, (struct sockaddr*)&sadr, &addr_size))!= -1){
    printf("---------------------\nReceived connection from %s\n",inet_ntoa(sadr.sin_addr));
        pthread_create(&thread_id,0,&SocketHandler, (void*)csock );
        counter++;
    }

    else{
     fprintf(stderr, "Error accepting %d\n", errno);
    }
        }// end while
.
.
.
return 0
}

As you can see whenever a client get disconnected, server should keep waiting for an another threads. On the other hand, this is the last part of SocketHandler function which is a thread function.

void* SocketHandler(void* csock){
.
.
.

        printf("Client disconnected\n");
        free(csock);
        return 0;
    }

After return 0 statement isn’t it necessary to return back to while(1) loop in main. I will be glad if you can help and thanks anyway

  • 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-14T07:21:33+00:00Added an answer on June 14, 2026 at 7:21 am

    Threads run asynchronously once created, which means that the main thread (the one doing the accept) should continue looping and be back to accepting new connections, whatever the child thread does.

    Some advices:

    • if your thread can run autonomously, use pthread_detach after the create to let thread handle its own termination graciously.
    • don’t forget to do a close(csock) before ending the thread.
    • you don’t necessarily need to allocate the int which will contain the socket descriptor, just pass it as the void * directly (but I guess that you will be passing more information than just the socket in a structure in a future version of your code).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a little pdo problem, that I have been working on for a
There are nice SO question and answers about this issue, but these options didn't
There is previous little on the google on this subject other than people asking
There was a similar question posted asking in general about alternatives to Qt. Well,
There was an issue in getting an global data into an BroadCast receiver Class,
There's a few previous questions on StackOverflow questioning how one goes about accessing local
There is this strange situation I'm fighting on. I have 3 pages, les call
There is a shared folder in my D drive as works ( D:\works ).
There are a lot of questions about full-joining in mysql(5.1.36). Of course the solution
There have been multiple questions regarding this topic but I have never really settled

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.