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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:50:09+00:00 2026-05-27T21:50:09+00:00

I am writing a small server which creates a new thread to handle each

  • 0

I am writing a small server which creates a new thread to handle each new connection. I need to pass the socket to the function using the fourth argument of pthread_create. When trying to free the memory used for the socket i get a segfault. The communication works fine. I have tried passing a void* and also a void** (casted to void*, kind of ugly)

This is the latest cludge i’m using while trying to figure this out, later if will be doing actual work in the respond function.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <pthread.h>

void *respond(void *thread_arg)
{
        void *arg = *(void**)thread_arg;
        printf("responding...\n");
        int client_sock;
        client_sock = (int)arg;

        char *message = "Write smthng to echo!\n\t";
        send(client_sock,message,strlen(message),0);
        char *buf = (char*)malloc(100);
        int ptr = 0;
        char last = ' ';
        while (last != '\n') {
                recv(client_sock,&last,1,0);
                buf[ptr++] = last;
        }
        buf[ptr++] = '\n';
        send(client_sock, buf, ptr, 0);
        ptr = 0;
        free(buf);
        close(client_sock);
        //free(arg); // why segfault?
        //free(*(void**)thread_arg); // the same
        pthread_exit(NULL);
}

int main(int argc, char **argv)
{
        int socket_desc, client_sock, addrlen, tmp;
        struct sockaddr_in address;
        pthread_t *responder_thread;
        void *cs;

        socket_desc = socket(AF_INET, SOCK_STREAM, 0);
        if (socket_desc < 0)
                printf("could not create socket");
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons(8080);
        tmp = bind(socket_desc, (struct sockaddr *)&address, sizeof(address));
        if (tmp < 0)
                printf("could nod bind to port!");
        listen(socket_desc, 5);

        addrlen = sizeof(struct sockaddr_in);
        while (true) {
                client_sock = accept(socket_desc, (struct sockaddr *)&address, (socklen_t*)&addrlen);
                if (client_sock < 0) {
                        printf("could not create client socket");
                } else {
                        printf("Accepted connection!\n");
                        cs = malloc(sizeof(int));
                        cs = (void*)client_sock;
                        responder_thread = (pthread_t*)malloc(sizeof(pthread_t*));
                        tmp = pthread_create(responder_thread, NULL, respond, (void*)&cs);
                        //cs = NULL;
                        if (tmp) {
                                printf("pthread_create returned '%d', exiting", tmp);
                                exit(-1);
                        }

                }
        }
        pthread_exit(NULL);
}

Lastly, to clarify; I am very inexperienced when it comes to c. 🙂

  • 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-27T21:50:10+00:00Added an answer on May 27, 2026 at 9:50 pm

    try something more like this.

    int* cs;
    ...
    cs = (int*)malloc(sizeof(int));
    *cs = client_sock;
    ...
    tmp = pthread_create(responder_thread, NULL, respond, (void*)cs);
    

    Then you don’t need this casting.

    void *arg = *(void**)thread_arg;
    

    and you can just free the thread_arg.

    free(thread_arg);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small web server in Python, using BaseHTTPServer and a custom subclass
I'm writing a small web server for testing purposes using python, BasicHTTPServer and SimpleHTTPServer.
I'm writing a small tool in C# which will need to send and receive
I am writing a small Java server, and a matching client in C++, which
I'm writing a small program which will make a GET request to a server
I'm writing myself a small server daemon in C, and the basic parts like
I am doing a file server migration and I'm writing a small C# app
I'm writing a program which creates a large number of large arrays to store
I'm writing a Comet application that has to keep track of each open connection
I am writing a small utility to send JMS messages to a remote server,

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.