Find the answer to your Linux question:
Results 1 to 3 of 3
I'm trying to make a server program that uses sockets and threads. I can get it to compile, but it keeps giving me a segmentation fault. I've made a number ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    2

    Question simple server & client getting Segmentation fault error

    I'm trying to make a server program that uses sockets and threads. I can get it to compile, but it keeps giving me a segmentation fault. I've made a number of changes to the program, and have no idea what is causing it. If anyone has an idea, please let me know. Here's the code, followed by the output i get. (Compiled using gcc)

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <signal.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void *thread_function(void *arg);
    
    int main ()
    {
    int res; /* for error checking */
    pthread_t client;
    void *thread_result;
    int j = 0; /* counter */
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;
    
    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_port = htons(9734);
    server_len = sizeof(server_address);
    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
    
    listen(server_sockfd, 5);
    
    signal(SIGCHLD, SIG_IGN);
    
    while(1) {
    client_len = sizeof(client_address);
    client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
    printf("client_sockfd after accept call: %d\n", client_sockfd);
    if(client_sockfd < 0) {
    perror("server: accept error");
    exit(1);
    }
    printf("fixin to create pthread\n");
    res = pthread_create(&client, NULL, thread_function, (void *) client_sockfd);
    if(res != 0) {
    perror("Thread creation failed");
    exit(1);
    }
    printf("pthread created\n");
    res = pthread_join(client, &thread_result);
    printf("pthread joined?\n");
    if(res != 0) {
    perror("Thread join failed");
    exit(1);
    }
    printf("pthread joined.\n");
    }
    }
    
    void *thread_function(void *arg)
    {
    char ch;
    int sockfd;
    printf("inside thread_function\n");
    sockfd = *((int *)arg);
    printf("client_sockfd assigned: %d\n", sockfd);
    read(sockfd, &ch, 1);
    printf("char read: %c\n", ch);
    sleep(1);
    ch++;
    write(sockfd, &ch, 1);
    printf("char sent: %c\n", ch);
    close(sockfd);
    pthread_exit(1);
    }
    output:

    client_sockfd after accept call: 4
    fixin to create pthread
    inside thread_function
    pthread created
    char from server = z
    [1]+ Segmentation fault ./server4.exe

    (client code)
    Code:
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<stdio.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<unistd.h>
    
    int main()
    {
    int sockfd;
    int len;
    
    struct sockaddr_in address;
    int result;
    char ch = 'A';
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(9734);
    len = sizeof(address);
    
    result = connect(sockfd, (struct sockaddr *)&address, len);
    
    if(result == -1) {
    perror("client1");
    exit(1);
    }
    
    write(sockfd, &ch, 1);
    ch = 'z';
    read(sockfd, &ch, 1);
    printf("char from server: %c\n", ch);
    close(sockfd);
    exit(0);
    }

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So I believe that the error is coming from this line:
    Code:
    res = pthread_join(client, &thread_result);
    You declare a void *thread_result, but you never allocate it. pthread_join() will then attempt to write something to it, which obviously cannot be achieved, hence the segmentation fault.

    Try allocating it, and see if the problem goes away.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    2
    Thanks for the reply.

    I did allocate it, however I finally discovered my problem. I had an extra asterisk. *facepalm*

    here:
    Code:
    sockfd = *((int *)arg);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...