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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:46:05+00:00 2026-06-17T16:46:05+00:00

im just started coding in c++ and i am trying to build a multithreaded

  • 0

im just started coding in c++ and i am trying to build a multithreaded server but i got some errors. first, here is the code i got:

while(true){
        printf("waiting for a connection\n");
        csock = (int*)malloc(sizeof(int));

        if((*csock = accept( hsock, (sockaddr*)&sadr, &addr_size))!= -1)
        {
            printf("---------------------\nReceived connection from   %s\n",inet_ntoa(sadr.sin_addr));
            //std::thread th(&Network::SocketHandler, NULL);

            std::thread th(Network::SocketHandler, (void*)csock);
            th.detach();
        }
        else
        {
            fprintf(stderr, "Error accepting %d\n", errno);
        }
    }

    }


    void Network::SocketHandler(void* lp)
    {
        int *csock = (int*)lp;

       char buffer[1024];
       int buffer_len = 1024;
       int bytecount;

       memset(buffer, 0, buffer_len);
       if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
          fprintf(stderr, "Error receiving data %d\n", errno);

       }
       printf("Received bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
       strcat(buffer, " SERVER ECHO");

       if((bytecount = send(*csock, buffer, strlen(buffer), 0))== -1){
          fprintf(stderr, "Error sending data %d\n", errno);

       }

       printf("Sent bytes %d\n", bytecount);

    }

i am getting a error when compiling at this line:

std::thread th(Network::SocketHandler, (void*)csock);

saying:
std::thread::thread(_Callable&&, _Args&& …) [with _Callable = void (Network::)(int); _Args = {void*}]
no known conversion for argument 1 from ‘’ to ‘void (Network::&&)(int)’

how can i fix this? or is there a better way to create a multithreaded server any examples maybe of other posts?

  • 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-17T16:46:06+00:00Added an answer on June 17, 2026 at 4:46 pm

    Why are you passing in a void * instead of an int * when it’s clear that what you really want is an int *?

    Just change the function signature to:

    void Network::SocketHandler(int* csock)
    

    and remove the cast in the code that does the calling:

    std::thread th(Network::SocketHandler, csock);
    

    Now, you will still get an error, and it will be for a different reason. Network::SocketHandler is a member function. It needs a this pointer. Normally you would call such functions with syntax like object.SocketHandler(csock) or objptr->SocketHandler(csock). When you call it that way with ::std::thread you aren’t giving it an object to be called on. It has no this pointer.

    What you should do is change the function signature again to:

    static void Network::SocketHandler(int* csock)
    

    and then your code will work just fine. It doesn’t look like the function makes use of any member variables, so it doesn’t need a this pointer.

    On a different note, it looks like you’re trying to adapt something originally written for pthreads. If I were doing this for the C++11 thread library I would do it in a rather different manner.

    I can’t see your entire program, so I don’t really have the luxury of re-designing it. But, from what I can see, I would make these tweaks:

    while(true){
            printf("waiting for a connection\n");
            int csock = -1;
    
            if((csock = accept( hsock, (sockaddr*)&sadr, &addr_size))!= -1)
            {
                printf("---------------------\nReceived connection from   %s\n",inet_ntoa(sadr.sin_addr));
                //std::thread th(&Network::SocketHandler, NULL);
    
                std::thread th(Network::SocketHandler, csock);
                th.detach();
            }
            else
            {
                fprintf(stderr, "Error accepting %d\n", errno);
            }
        }
    
        }
    
    
        void Network::SocketHandler(int csock)
        {
           char buffer[1024];
           int buffer_len = 1024;
           int bytecount;
    
           memset(buffer, 0, buffer_len);
           if((bytecount = recv(csock, buffer, buffer_len, 0))== -1){
              fprintf(stderr, "Error receiving data %d\n", errno);
    
           }
           printf("Received bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
           strcat(buffer, " SERVER ECHO");
    
           if((bytecount = send(csock, buffer, strlen(buffer), 0))== -1){
              fprintf(stderr, "Error sending data %d\n", errno);
    
           }
    
           printf("Sent bytes %d\n", bytecount);
    
        }
    

    The changes are fairly subtle. The C++11 thread library lets you call functions and provide all their arguments, and it handles this in a thread-safe way. There is no need to pass void * anymore, nor is there a need to use malloc or new to create storage space for those arguments you can just pass the arguments your thread needs directly to the thread constructor.

    Your program, in fact, has a memory leak. It never reclaims the space it mallocs for csock to point to. If it ran for a long time, it would eventually run out of memory as the space for all those filehandles was never reclaimed.

    Your program may also have a filehandle leak. You do not appear to close the socket in Network::SocketHandler. But since I don’t have visibility on your entire program, I can’t be sure about that.

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

Sidebar

Related Questions

I have just started to do some coding with Access and trying to create
this is my first question and I just started coding in C# some days
Just started my first MVC 2.0 .net application. And I set up some default
I have just started out coding in c++ but have quite a bit of
Just started coding in AS3 with FlashDevelop and coming from a C# background, I
I have just started coding in AS3 and it would be really great to
I just started learning C++ (coming from Java ) and am having some serious
Please bear with me as I've just started using NetBeans for the first time!
Just started learning Rails (3). I am tearing my hair out trying to find
I just started learning on WCF and is trying to create a WCF service

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.