客户端断开连接中的段错误

Segfault in client disconnection

我有一个用 C 语言实现的简单客户端-服务器程序,其中客户端可以将整数发送到服务器,服务器用它们的总和进行回复。但是,每当客户端突然断开连接时,服务器端就会显示一个令人不安的分段错误(核心已转储)。
客户:

#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);
}

服务器:

#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;
                    }
                }
            }
        }
    }
}

段错误如何解决?还可以如何改进代码?

段错误来自 atoi 函数,因为收到 NULL。检查 NULL 似乎可以解决问题...