Home > front end >  Segfault in client disconnection
Segfault in client disconnection

Time:12-10

I have a simple client-server program implemented in C where a client can send integers to a server and the server replies with their sums. However, there is a troubling Segmentation fault (core dumped) shown on the server side whenever the client disconnects suddenly.
The Client:

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

#define PORT 5010

int main(int argc, char **argv) {
    char buf[BUFSIZ], buf2[BUFSIZ], message[BUFSIZ], serverReply[BUFSIZ];
    int SOCKET;
    struct sockaddr_in server;
    SOCKET = socket(AF_INET, SOCK_STREAM, 0);
    if (SOCKET < 0) {
        perror("Could not create socket");
        return -1;
    }
    printf("Socket created\n");
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    if (connect(SOCKET, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) {
        perror("Could not connect");
        return -1;
    }
    memset(&serverReply, 0, sizeof(serverReply));
    printf("Connected to server.\nEnter first number: ");
    scanf("%s", buf);
    fflush(stdin);
    printf("Enter second number: ");
    scanf("%s", buf2);
    strcat(buf, " ");
    strcat(buf, buf2);
    strcpy(message, buf);
    if (send(SOCKET, message, strlen(message), 0) < 0) {
        perror("Failed to send message");
        return -1;
    }
    if (recv(SOCKET, serverReply, sizeof(serverReply), 0) < 0) {
        perror("Could not receive message");
        return -1;
    }
    printf("Server: %s", serverReply);
    close(SOCKET);
}

The Server:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

#define PORT 5010

int main(int argc, char *argv[]) {
    char msg[BUFSIZ], reply[BUFSIZ];

    struct sockaddr_in server, client;
    int SOCKET, ACCEPT, READ, sockSize, num1, num2, option = 1, maxClients = 30,
            h, clientSocket[maxClients], maxsd, sd, SELECT;
    fd_set readfds;
    for (h = 0; h < maxClients; h  ) {
        clientSocket[h] = 0;
    }
    SOCKET = socket(AF_INET, SOCK_STREAM, 0);
    if (SOCKET == -1) {
        perror("Could not create socket");
        return -1;
    }
    if (setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) == -1) {
        perror("Could not set OPTNAME");
        return -1;
    }
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(PORT);
    printf("Created socket.\n");
    if (bind(SOCKET, (struct sockaddr *) &server, sizeof(server)) < 0) {
        perror("Could not bind");
        return -1;
    }
    if (listen(SOCKET, 1) < 0) {
        perror("Listen failed");
        return -1;
    }
    printf("Server is listening.\n");
    sockSize = sizeof(struct sockaddr_in);

    while (1) {
        FD_ZERO(&readfds);
        FD_SET(SOCKET, &readfds);
        maxsd = SOCKET;
        for (h = 0; h < maxClients; h  ) {
            sd = clientSocket[h];
            if (sd > 0) { FD_SET(sd, &readfds); }
            if (sd > maxsd) { maxsd = sd; }
        }
        SELECT = select(maxsd   1, &readfds, NULL, NULL, NULL);
        if ((SELECT < 0) && (errno != EINTR)) {
            perror("select error");
        }
        if (FD_ISSET(SOCKET, &readfds)) {

            ACCEPT = accept(SOCKET, (struct sockaddr *) &server, (socklen_t *) &sockSize);
            if (ACCEPT < 0) {
                perror("Could not accept client");
                return -1;
            }
            for (h = 0; h < maxClients; h  ) {
                if (clientSocket[h] == 0) {
                    clientSocket[h] = ACCEPT;
                    break;
                }
            }
            printf("Client has joined the server.\n");
        }
        for (h = 0; h < maxClients; h  ) {
            sd = clientSocket[h];
            if (FD_ISSET(sd, &readfds)) {
                READ = read(sd, msg, sizeof(msg));
                if (READ == -1) {
                    perror("Could not receive message");
                    return -1;
                }
                if (READ == 0) {
                    printf("Client disconnected\n");
                    fflush(stdout);
                    clientSocket[h]=0;
                }
                int e = 0;
                char *p = strtok(msg, " ");
                char *arr[2];
                while (p != NULL) {
                    arr[e  ] = p;
                    p = strtok(NULL, " ");
                }
                num1 = atoi(arr[0]);
                num2 = atoi(arr[1]);

                if ((strcmp(arr[0], "0") != 0 && num1 != 0) && (strcmp(arr[1], "0") != 0 && num2 != 0)) {
                    printf("Client: %d, %d\n", num1, num2);
                    sprintf(reply, "%d\n", num1   num2);
                    if (write(sd, reply, strlen(reply)) < 0) {
                        perror("Could not send message");
                        return -1;
                    }
                    memset(&reply, 0, sizeof(reply));

                } else {
                    printf("Conversion error");
                    strcpy(reply, "Conversion error.");
                    if (write(sd, reply, strlen(reply)) < 0) {
                        perror("Could not send message");
                        return -1;
                    }
                }
            }
        }
    }
}

How can the segfault be solved? How else can the codes be improved?

CodePudding user response:

The segfault comes from the atoi function since NULL is received. Checking for NULL seems to do the trick...

  • Related