Finishing a C challange

a. Create 2 files, namely server.c and client.c.

When server.c is first run, it will prompt the user to type in how many clients to serve (range 1-5).

When client.c is first run, it prompts the user to type in a username. This username is then sent to the server, to check whether it already exists or not. If it already exists, it will ask for another prompt to type in the username until it is received by the server.

b. When all the clients needed by the server have been collected, each client will start to be given a prompt to type in the required service, namely Container Name, Image Used, Container Startup Command, and Volume. The order in which clients send service requests to the server is determined by the time they register. Example: If username ragnar registers first, followed by username towel, then ragnar will send the request first, followed by towel, and so on.

c. After all service requests from the client are collected, the server then combines them into a docker compose file. Then run the docker compose file that was created earlier

d. Not until there, after the previous docker compose is finished running, client.c will ask for another prompt from the user. The prompt given is in accordance with point (b). If each client does the same thing at point (b), then the server will generate a new docker compose file. Then the container that has been run before will be destroyed, which will then be replaced by the latest service that has been requested from the client earlier.

so, i have made a code untill b. but i think i didn’t finish the a because my server file isn’t checking a name that already exist. and for the b, it having an error, after i finish filling the first client service the second client is not outputing the service request, just a username. and in the server prompt the output format still not correct. lastly for the b and c i still not work for it either,so can anyone help me please. i’m so appreciating your answers.

SERVER.C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define MAX_CLIENTS 5
#define MAX_REQUEST_LEN 256

// Struktur pesan untuk komunikasi dengan clients
struct message {
    long mtype;
    char mtext[MAX_REQUEST_LEN];
};

// Struktur untuk menyimpan informasi layanan dari setiap client
struct service_request {
    char username[MAX_REQUEST_LEN];
    char container_n// Struktur untuk menyimpan informasi layanan dari setiap client
struct service_request {
    char uame[MAX_REQUEST_LEN];
    char image_used[MAX_REQUEST_LEN];
    char start_command[MAX_REQUEST_LEN];
    char volume[MAX_REQUEST_LEN];
};

// Fungsi untuk mengirim pesan ke client
void send_message(int msqid, struct message *msg) {
    if (msgsnd(msqid, msg, MAX_REQUEST_LEN, 0) == -1) {
        perror("msgsnd");
        exit(1);
    }
}

// Fungsi untuk menerima pesan dari client
void receive_message(int msqid, struct message *msg) {
    if (msgrcv(msqid, msg, MAX_REQUEST_LEN, 1, 0) == -1) {
        perror("msgrcv");
        exit(1);
    }
}

// Fungsi untuk set status server
void set_server_status(int status) {
    FILE *fp = fopen("server_status.txt", "w");
    if (fp == NULL) {
        perror("Failed to open server status file");
        exit(1);
    }
    fprintf(fp, "%d", status);
    fclose(fp);
}

// Fungsi untuk mendapatkan status server
int get_server_status() {
    FILE *fp = fopen("server_status.txt", "r");
    if (fp == NULL) {
        perror("Failed to open server sperror("Failed ttatus file");
        exit(1);
    }
    int status;
    fscanf(fp, "%d", &status);
    fclose(fp);
    return status;
}

int main() {
    key_t key;
    int msqid;
    struct message msg;
    struct service_request requests[MAX_CLIENTS]; // Array untuk menyimpan informasi permintaan layanan dari setiap client
    int num_clients = 0;

    // Set status server menjadi berjalan
    set_server_status(1);

    // Buat key untuk message queue
    if ((key = ftok("server.c", 'B')) == -1) {
        perror("ftok");
        exit(1);
    }

    // Buat message queue
    if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
        perror("msgget");
        exit(1);
    }

    printf("Server is running...n");

    // Menerima jumlah klien yang akan dilayani
    printf("Enter the number of clients to serve (1-5): ");
    scanf("%d", &num_clients);

    // Batasi jumlah klien
    if (num_clients < 1 || num_clients > MAX_CLIENTS) {
        printf("Invalid number of clients. Exiting...n");
        exit(1);
    }

    // Menerima nama pengguna dari setiap klien
    int client_count = 0;
    while (client_count < num_clients) {
        receive_message(msqid, &msg);
        int i;
        for (i = 0; i < client_count; i++) {
            if (strcmp(requests[i].username, msg.mtext) == 0) {
                strcpy(msg.mtext, "exists");
                send_message(msqid, &msg);
                break;
            }
        }
        if (i == client_count) {
            strcpy(requests[client_count].username, msg.mtext);
            client_count++;
            strcpy(msg.mtext, "accepted");
            send_message(msqid, &msg);
        }
    }

    printf("All client usernames accepted.n");

    // Menerima permintaan layanan dari klien yang telah mendaftar
    for (int i = 0; i < num_clients; i++) {
        receive_message(msqid, &msg);
        printf("Received service request from client %s:n", requests[i].username);
        printf("Container Name: %sn", msg.mtext);
        strcpy(requests[i].container_name, msg.mtext);

        receive_message(msqid, &msg);
        printf("Image Used: %sn", msg.mtext);
        strcpy(requests[i].image_used, msg.mtext);

        receive_message(msqid, &msg);
        printf("Start Command: %sn", msg.mtext);
        strcpy(requests[i].start_command, msg.mtext);

        receive_message(msqid, &msg);
        printf("Volume: %sn", msg.mtext);
        strcpy(requests[i].volume, msg.mtext);
    }

    // Set status server menjadi tidak berjalan
    set_server_status(0);

    // Tampilkan layanan yang diminta berdasarkan username
    printf("Service requests:n");
    for (int i = 0; i < num_clients; i++) {
        printf("Username: %sn", requests[i].username);
        printf("Container Name: %sn", requests[i].container_name);
        printf("Image Used: %sn", requests[i].image_used);
        printf("Start Command: %sn", requests[i].start_command);
        printf("Volume: %sn", requests[i].volume);
        printf("----------------------------------------n");
    }

    return 0;
}
CLIENT.C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define MAX_REQUEST_LEN 256

// Struktur pesan untuk komunikasi dengan server
struct message {
    long mtype;
    char mtext[MAX_REQUEST_LEN];
};

// Struktur untuk menyimpan informasi permintaan layanan
struct service_request {
    char container_name[MAX_REQUEST_LEN];
    char image_used[MAX_REQUEST_LEN];
    char start_command[MAX_REQUEST_LEN];
    char volume[MAX_REQUEST_LEN];
};

// Fungsi untuk mengirim pesan ke server
void send_message(int msqid, char *message) {
    struct message msg;
    msg.mtype = 1;
    strcpy(msg.mtext, message);
    if (msgsnd(msqid, &msg, MAX_REQUEST_LEN, 0) == -1) {
        perror("msgsnd");
        exit(1);
    }
}

// Fungsi untuk menerima pesan dari server
void receive_message(int msqid, struct message *msg) {
    if (msgrcv(msqid, msg, MAX_REQUEST_LEN, 1, 0) == -1) {
        perror("msgrcv");
        exit(1);
    }
}

// Fungsi untuk mencetak pesan koneksi terputus
void print_disconnected() {
    printf("Connection terminated.n");
}

// Fungsi untuk memeriksa apakah server berjalan
int is_server_running() {
    key_t key;
    int msqid;

    if ((key = ftok("server.c", 'B')) == -1) {
        return 0; // Gagal mendapatkan key, server mungkin tidak berjalan
    }

    // Coba mengakses message queue
    if ((msqid = msgget(key, 0644)) == -1) {
        return 0; // Gagal mengakses message queue, server mungkin tidak berjalan
    }

    return 1; // Server berjalan
}

int main() {
    key_t key;
    int msqid;
    struct message msg;
    struct service_request request; // Deklarasi struct service_request

    // Buat key untuk message queue
    if ((key = ftok("server.c", 'B')) == -1) {
        perror("ftok");
        exit(1);
    }

    // Dapatkan message queue ID
    if ((msqid = msgget(key, 0644 | IPC_NOWAIT)) == -1) {
        // Jika tidak dapat terhubung ke server
        print_disconnected();
        exit(1);
    }

    // Dapatkan status server
    int server_running = is_server_running();

    // Jika server tidak berjalan, keluarkan pesan dan hentikan program
    if (!server_running) {
        print_disconnected();
        exit(1);
    }

    // Input dan kirim username ke server
    do {
        printf("Enter your username: ");
        scanf("%s", msg.mtext);

        send_message(msqid, msg.mtext);
        receive_message(msqid, &msg);

        if (strcmp(msg.mtext, "exists") == 0) {
            printf("Username already exists. Please enter a different username.n");
        }
    } while (strcmp(msg.mtext, "exists") == 0);

    printf("Username accepted.n");

    // Meminta input permintaan layanan
    printf("Enter container name: ");
    scanf("%s", request.container_name);

    printf("Enter image used: ");
    scanf("%s", request.image_used);

    printf("Enter start command: ");
    scanf("%s", request.start_command);

    printf("Enter volume: ");
    scanf("%s", request.volume);

    // Kirim permintaan layanan ke server
    send_message(msqid, request.container_name);
    send_message(msqid, request.image_used);
    send_message(msqid, request.start_command);
    send_message(msqid, request.volume);

    printf("Service request sent.n");

    return 0;
}

New contributor

Stevanza Gian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật