HQ189
HQ189

Reputation: 35

Multipleclient Server: quitfunction

I have some problems with my Server/Client. If multiple people connect to my server, and one of them quit the connection to the server, all other, which are also connected to the server, lose their connection to.

void *connection_handler(void *);
extern int check_err_buffer(int sock);
extern error_t *error_buffer;
extern invalid_user_input_t invalid_user_input;
regex_t regex;

int main(int argc, char *argv[]) {
    //init KVS
    init();

    int socket_desc, client_sock, c, *new_sock;
    struct sockaddr_in server, client;

    //Create socket
    socket_desc = socket(AF_INET, SOCK_STREAM | O_NONBLOCK, 0);
    int opts;
    opts = fcntl(socket_desc,F_GETFL);
    opts = opts & (~O_NONBLOCK);
    fcntl(socket_desc,F_SETFL,opts);

    if (socket_desc == -1) {
        printf("Could not create socket");
    }
    puts("Socket created");

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

    //Bind
    if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
        //print the error message
        perror("bind failed. Error");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc, 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);//Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
    while (1) {
        puts("waiting for client...");
        client_sock = accept(socket_desc, (struct sockaddr *) &client, (socklen_t * ) & c);
        puts("Connection accepted");
        pthread_t sniffer_thread;
        new_sock = malloc(1);
        *new_sock = client_sock;

        pthread_create(&sniffer_thread, NULL, connection_handler, (void *) new_sock);


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

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

    return 0;
}

In the main function I accept the different connection from the clients. I have more functions, which I didn't put in, because they are not so relevant for this problem. In the second part I will handle some cases. I look if the client type the letter 'q', if it's true, I will close the connection with close(). But in all cases, it close the server, and all connections are lost.

/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc) {
    //Get the socket descriptor
    int sock = *(int *) socket_desc;
    char *message, client_message[3000];
    puts("sending welcome msg to client...");
    write(sock, message, 3000);

    int reti;
    //compile a regexp that checks for valid user input
    regcomp(&regex, "([dg] [0-9]+)|([v] .+)|(p [0-9]+ .+)|q", REG_EXTENDED);
    //Receive a message from client
    char *input[3];
    while(1){
        //wait for some client input
        recv(sock, client_message, 2000, 0);
        //check the input with the regexp
        reti = regexec(&regex, client_message, 0, NULL, REG_EXTENDED);
        if (!reti) {
            //tokenize the client input with \n as a delim char
            char *token = strtok(client_message, " ");
            int i = 0;
            while (token) {
                input[i] = token;
                token = strtok(NULL, " ");
                i++;
            }
                //quit operation
            if (strcmp(input[0], "q") == 0) {
                //if a client quits, the server dies. TODO: fix
                puts("quitting");
                //free(socket_desc);
                close(socket_desc);
                return 0;
            }
            //user input does not match regexp, try again..
        } else if (reti == REG_NOMATCH) {
            error_buffer = invalid_user_input;
            check_err_buffer(sock);
            usleep(100);
            write(sock, " ", 2);
        }
    }
    //Free the socket pointer
    free(socket_desc);

    return 0;
}

On the client side i test if message[0] == 'q' and I close the socket. I would be very happy if i will get some help or hints to solve this problem. Thank you

Upvotes: 0

Views: 35

Answers (1)

alk
alk

Reputation: 70951

As here

      new_sock = malloc(1);

the code allocates 1 byte only, this following line

      *new_sock = client_sock;

writes to invalid memory, as an int is assigned, and sizeof int bytes are written to where new_sock points. By definition sizeof int is at least 2, probably more, With this undefined behaviour is invoked. Anything may happen from then on.

To fix this do

      new_sock = malloc(sizeof *new_sock);

instead.


Inspecting the return value of functions is debugging for free. close(socket_desc); is nonsense and definitely would return -1 and set errno to EBADFD.

Replace

    close(socket_desc);

by

    if (-1 == close(socket_desc))
    {
      perror("close() falied");
    }

to see how the code as it stand fails.

Upvotes: 1

Related Questions