Pthread_join has no effect on the main thread

I found this Sockets tutorial http://www.binarytides.com/socket-programming-c-linux-tutorial/ and I am having trouble with the last example. It is a streaming server using sockets and pthreads.

The code compiles fine, but it doesn't work as expected. I suspect the challenge pthread_join

is the culprit, but I'm not sure.

I've tried compiling and running the server and client on both Cygwin (32-bit) and Alpine Linux (32-bit) with GCC 4.8.3 and 4.8.2. The problem is the same in both environments.

I have read the pthreads documentation pthread_join

at http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html but I cannot find anything wrong with the call pthread_join

to the server code.

Updated server, client and output.

Server code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>

void *connection_handler(void *);

int main(int argc, char *argv[])
{
    int socket_desc, new_socket, c;
    struct sockaddr_in server, client;
    char *message;

    // Create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    // Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(2000);

    // Bind
    if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0)
    {
        puts("bind failed");
        return 1;
    }
    puts("bind done");

    // Listen
    listen(socket_desc, 3);

    // Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    while ((new_socket = accept(socket_desc, (struct sockaddr *) &client, (socklen_t*)&c)) )
    {
        puts("Connection accepted");

        // Reply to the client
        message = "Hello Client, I have received your connection. And now I will assign a handler for you\n";
        write(new_socket, message, strlen(message));

        pthread_t sniffer_thread;

        if (pthread_create(&sniffer_thread, NULL, connection_handler, (void*) new_socket) < 0)
        {
            perror("could not create thread");
            return 1;
        }

        // Now join the thread , so that we don't terminate before the thread
        // pthread_join(sniffer_thread, NULL);
        puts("Handler assigned");
    }

    if (new_socket < 0)
    {
        perror("accept failed");
        return 1;
    }
    return 0;
}

/*
 * This will handle connection for each client
 * */
void *connection_handler(void *socket_desc)
{
    // Get socket descriptor
    int sock = (int)socket_desc;
    int read_size;
    char *message, client_message[2000]; 

    // Send some messages to the client
    message = "Greetings! I am your connection handler\n";
    write(sock, message, strlen(message) + 1);

    message = "Now type something and i shall repeat what you type\n";
    write(sock, message, strlen(message) + 1);

    // Receive a message from client
    read_size = recv(sock, client_message, 1999, 0); 

    if (read_size > 0) 
    {
        client_message[read_size] = 0; 
    }
    else
    {
        puts("recv failed");
    }

    //while(read_size > 0 )
    //{
        // Send the message back to client
        write(sock, client_message, strlen(client_message) + 1);
    //}

    if (read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if (read_size == -1)
    {
        perror("recv failed");
    }

    close(sock);

    return 0;
}

      

Client code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int arc, char *argv[])
{
    int socket_desc;
    struct sockaddr_in server;
    char *message, server_reply[2000];

    // Create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(2000);

    // Connect to remote server
    if (connect(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected\n");

    // Send some data
    message = "Hello server!";
    if (send(socket_desc, message, strlen(message), 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
    puts("Data Send\n");

    int length = 0;
    do {
        // Receive a reply from the server
        length = recv(socket_desc, server_reply, 1999, 0); 

        if (length > 0)
        {
            server_reply[length] = 0; 
        }
        else
        {
            puts("Nothing to recieve");
            close(socket_desc);
            return 1;
        }

        puts("Reply received\n");
        puts(server_reply);

    } while (length > 0);

    return 0;
}

      

Logging out from the server:

bind done
Waiting for incoming connections...
Connection accepted
Test 1
Segmentation fault (core dumped)

      

Client conclusion:

Connected
Data Send
Reply received
Hello Client , I have received your connection. And now I will assign a handler for you
recv failed

      

It looks like the main thread returns / ends before sniffer_thread.

Any help is assigned.

+3


source to share


1 answer


You are experiencing undefined behavior because you are calling strlen

on a line that does not end with '\ 0'. recv

does not place a null terminator at the end of the line.

char *message , client_message[2000];
//...
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
                                     ^^^^^^^^^^^^^^
                                     not null terminated

      



You must use the return value recv

to check the size of the data read.

+2


source







All Articles