Server(handle_protocol 수정)

void handle_protocol(int x, fd_set * pset, int state[]) {
    int y;
    char buf[50];
    y = read(x, buf, 50); // 데이터를 읽음
    buf[y] = '\\0'; // 문자열로 만듦

    if (state[x] == 1) {
        // 클라이언트가 "hello"를 보낸 경우
        if (strcmp(buf, "hello") == 0) {
            printf("cli at socket %d => serv: %s\\n", x, buf);
            write(x, "download or upload?", 20);
            printf("serv => cli at socket %d : download or upload?\\n", x);
            state[x] = 2;
        } else {
            printf("received wrong msg from socket %d, disconnecting\\n", x);
            write(x, "protocol error", 14);
            close(x);
            FD_CLR(x, pset);
        }
    } else if (state[x] == 2) {
        // 클라이언트가 "download" 또는 "upload"를 선택한 경우
        printf("cli at socket %d => serv: %s\\n", x, buf);

        if (strcmp(buf, "download") == 0) {
            write(x, "file name to download?", 22);
            printf("serv => cli at socket %d : file name to download?\\n", x);
            state[x] = 3;
        } else if (strcmp(buf, "upload") == 0) {
            write(x, "file name to upload?", 20);
            printf("serv => cli at socket %d : file name to upload?\\n", x);
            state[x] = 4;
        } else {
            printf("received wrong msg from socket %d, disconnecting\\n", x);
            write(x, "protocol error", 14);
            close(x);
            FD_CLR(x, pset);
        }
    } else if (state[x] == 3) {
        // 클라이언트가 다운로드할 파일 이름을 보낸 경우
        printf("cli at socket %d => serv: %s\\n", x, buf);

        // 파일을 열고 파일 내용을 클라이언트에게 전송
        FILE *file = fopen(buf, "r");
        if (file == NULL) {
            printf("file not found: %s\\n", buf);
            write(x, "file not found", 14);
        } else {
            fseek(file, 0, SEEK_END);
            long file_size = ftell(file);
            fseek(file, 0, SEEK_SET);

            char file_content[51];
            fread(file_content, sizeof(char), 50, file);
            fclose(file);

            file_content[file_size] = '\\0';
            write(x, file_content, strlen(file_content) + 1);
            printf("serv => cli at socket %d : file content sent\\n", x);
        }

        state[x] = 1;
    } else if (state[x] == 4) {
        // 클라이언트가 업로드할 파일 이름을 보낸 경우
        printf("cli at socket %d => serv: %s\\n", x, buf);

        // 파일을 열고 클라이언트로부터 파일 내용을 받음
        FILE *file = fopen(buf, "w");
        if (file == NULL) {
            printf("failed to create file: %s\\n", buf);
            write(x, "file creation failed", 20);
        } else {
            write(x, "send file content", 18);
            printf("serv => cli at socket %d : send file content\\n", x);
            state[x] = 5;
        }
    } else if (state[x] == 5) {
        // 클라이언트가 파일 내용을 보낸 경우
        printf("cli at socket %d => serv: %s\\n", x, buf);

        // 파일에 내용을 씀
        FILE *file = fopen(cli[x].name, "w");
        fwrite(buf, sizeof(char), strlen(buf), file);
        fclose(file);

        printf("serv => cli at socket %d : file content saved\\n", x);
        state[x] = 1;
    }
}

Client (main 함수 수정)

void main() {
    int x, y;
    struct sockaddr_in serv_addr;
    char buf[50];
    printf("Hi, I am the client\\n");

    bzero((char *)&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = PF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(SERV_ADDR);
    serv_addr.sin_port = htons(SERV_TCP_PORT);

    // open a tcp socket
    if ((x = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        printf("socket creation error\\n");
        exit(1);
    }
    printf("socket opened successfully. socket num is %d\\n", x);

    // connect to the server
    if (connect(x, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("can't connect to the server\\n");
        exit(1);
    }
    // now start ftp client protocol
    int i;
    for (i = 0; i < 4; i++) {
        if (i == 0) {
            printf("enter hello\\n");
        }
        gets(buf);
        write(x, buf, strlen(buf));
        y = read(x, buf, 50);
        write(1, buf, y);
        printf("\\n");
    }

    int z = fork();
    if (z == 0) {
        // child process for upload
        for (i = 0; i < 10; i++) {
            gets(buf);
            write(x, buf, strlen(buf));
            if (strcmp(buf, "f1") == 0) {
                y = read(x, buf, 50);
                write(1, buf, y);
                printf("\\n");
                gets(buf);
                write(x, buf, strlen(buf));
                break;
            }
        }
    } else {
        // parent process for download
        for (i = 0; i < 10; i++) {
            y = read(x, buf, 50);
            write(1, buf, y);
            printf("\\n");
            if (strcmp(buf, "file name to download?") == 0) {
                gets(buf);
                write(x, buf, strlen(buf));
                y = read(x, buf, 50);
                write(1, buf, y);
                printf("\\n");
                break;
            }
        }
    }

    close(x); // disconnect the communication
}