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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:27:35+00:00 2026-06-16T00:27:35+00:00

I have a server that spawns a thread for each client that connects to

  • 0

I have a server that spawns a thread for each client that connects to it. The thread then deals with receiving/sending data to the client. Here is my server code:

    //////////////
    //  START   //
    /////////////
    while( 1 )
    {
    if( listen(serverSocket, SOMAXCONN) == SOCKET_ERROR ) 
    {
        printf( "Listen failed with error: %ld\n", WSAGetLastError() );
        break;
    }    
        /*
         *  wait for clients...
        */
        clientSocket = accept(serverSocket,
                              (struct sockaddr *)&clientSockAddr,
                              &clientSockAddrLen);


        if( clientSocket == INVALID_SOCKET )
        {
            printf("%d:accept failed\n", WSAGetLastError());
            closesocket(serverSocket);
            WSACleanup();
            return 1;
        }

        printf("Client accepted: IP: %s PORT: %d\n",
            inet_ntoa(clientSockAddr.sin_addr),
            clientSockAddr.sin_port );

        THREADDATA threadData = { clientSocket };
        sprintf_s(threadData.ip, "%s", inet_ntoa(clientSockAddr.sin_addr));

        // spawn a thread for each client
        hthread = CreateThread(
                    NULL,                       // don't inherit handle
                    0,                          // use default size for the executable
                    processClient,
                    (pTHREADDATA)&threadData,   // thread data
                    0,                          // run right away
                    &threadId );
    }

And here is what my thread function looks like:

/*
    called by thread for each client
*/
DWORD WINAPI processClient(LPVOID lpParam)
{
    int numBytesRecvd = 0,
        numBytesSent = 0,
        index = 0,
        nLeft = SIZE,
        TOTAL = SIZE;

    char buff[SIZE];
    int run = 1;

    char msg[MSG_SIZE];

    pTHREADDATA td = (pTHREADDATA)lpParam;

    /* keep processing client */
    while (run)
    {
        memset(buff, 0, SIZE);

        numBytesRecvd = recv(td->clientSocket, buff, nLeft, 0);


        /* CLIENT EXITED */
        if( !strcmp(buff, "exit") )
        {

            printf("Client exited!\n");

            run = 0;
        }

        if( numBytesRecvd > 0 )
        {

            printf("< %s\n", buff);

        }

        if (numBytesRecvd == 0)
        {
            printf("Client closed!\n");
            run = 0;
        }

        if( numBytesRecvd == SOCKET_ERROR )
        {
            printf("%d:Recieve error!\n", WSAGetLastError());
            run = 0;
        }
    }

    closesocket(td->clientSocket);
    ExitThread(0);

    return 0;
}

Issue:

So I start the server and the client, say client1 (from command prompt in win 7), everything is fine. When I type something at the client1 terminal, it is printed at the server terminal.

Now I launch another client, client2, it gets connected to the server and whatever I type gets displayed at the server, but now when I type something at client1 it doesn’t get displayed at the server terminal.

So basically every time I start a new client, only that client can talk to the server, the old clients cannot!!! But I thought the thread will keep handling each client? Or is it that cmd prompt in windows is not thread safe?

  • 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-16T00:27:36+00:00Added an answer on June 16, 2026 at 12:27 am

    I can see two probable issues here : 1) You are passing threadData on the stack to a different thread. You should be allocating it on the heap and then pass it to the thread. 2)I think you are not assigning clientSocket correctly to threadData, shouldnt you be assigning to a member of the structure threadData?

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

Sidebar

Related Questions

I have a game server that spawns a thread for each client with forkIO.
I have a client thread that will ping server for one of its status
I have a server that connects to multiple clients using TCP/IP connections, using C
I have a server that communicates to a lot of devices (>1000). Each connection
I have android app that talks to server and syncs some data int SQLite
So I have two threads going - a Server thread and a Client thread.
I have a simple service that does approximately the following: An HTTP client connects
A web crawler script that spawns at most 500 threads and each thread basically
I have a .NET Windows Service which spawns a thread that basically just acts
I have a server that runs on Solaris 10, whenever I go to the

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.