Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
4.6 kB
1
Indexable
Never
#include "sd1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <unistd.h>

#define PORT 8880
#define SERVER_IP "127.0.0.1"

struct socketInfo {
    int sockfd;
    char *host;
};

void *menu_use(void *arg) {
    struct socketInfo *info = (struct socketInfo *)arg;
    int sockfd = info->sockfd;
    char *host = info->host;

    CLIENT *clnt;
    clnt = clnt_create(host, FUNC_PROG, FUNC_VERS, "tcp");
    if (clnt == NULL) {
        clnt_pcreateerror(host);
        pthread_exit(NULL);
    }

    printf("Connected.\n");

    int clnt_choice, N, t;
    int *temp;
    float a;

    do {
        t = recv(sockfd, &clnt_choice, sizeof(int), 0);
        if (t <= 0) {
            printf("ERROR receiving from socket client\n");
            break;
        }

        if (clnt_choice == 1 || clnt_choice == 2 || clnt_choice == 3) {
            t = recv(sockfd, &N, sizeof(int), 0);
            if (t <= 0) {
                printf("ERROR receiving from socket client\n");
                continue;
            }

            temp = (int *)malloc(N * sizeof(int));
            if (temp == NULL) {
                printf("ERROR allocating memory\n");
                continue;
            }

            t = recv(sockfd, temp, N * sizeof(int), 0);
            if (t <= 0) {
                printf("ERROR receiving from socket client\n");
                free(temp);
                continue;
            }

            if (clnt_choice == 1) {
                N_array averagefunc_1_arg;
                averagefunc_1_arg.N.N_len = N;
                averagefunc_1_arg.N.N_val = temp;
                float *result_1 = mean_value_1_svc(&averagefunc_1_arg, NULL);
                send(sockfd, result_1, sizeof(float), 0);
            } else if (clnt_choice == 2) {
                N_array minmaxfunc_1_arg;
                minmaxfunc_1_arg.N.N_len = N;
                minmaxfunc_1_arg.N.N_val = temp;
                max_min *result_2 = minmax_1_svc(&minmaxfunc_1_arg, NULL);
                int min_max[2] = {result_2->min, result_2->max};
                send(sockfd, min_max, 2 * sizeof(int), 0);
            } else if (clnt_choice == 3) {
                new_array productfunc_1_arg;
                productfunc_1_arg.N.N_len = N;
                productfunc_1_arg.N.N_val = temp;

                t = recv(sockfd, &a, sizeof(float), 0);
                if (t <= 0) {
                    printf("ERROR receiving from socket client\n");
                    free(temp);
                    continue;
                }
                productfunc_1_arg.constant = a;
                new_array_production *result_3 = productarray_1_svc(&productfunc_1_arg, NULL);
                send(sockfd, result_3->product.product_val, N * sizeof(float), 0);
            }
            free(temp);
        } else if (clnt_choice == 0) {
            printf("CLIENT DISCONNECTING....\n");
            break;
        } else {
            printf("CLIENT'S INVALID CHOICE..WAITING FOR NEW CHOICE...\n");
        }
    } while (1);

    clnt_destroy(clnt);
    close(sockfd);
    free(info);
    pthread_exit(NULL);
}

int main() {
    int sockfd, newsockfd, clilen, i = 0;
    struct sockaddr_in serv_addr, cli_addr;
    pthread_t thread[10];

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        printf("ERROR opening socket\n");
        exit(1);
    }

    bzero((char *)&serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    serv_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("ERROR on binding\n");
        close(sockfd);
        exit(1);
    }

    listen(sockfd, 5);

    while (1) {
        clilen = sizeof(cli_addr);
        newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
        if (newsockfd < 0) {
            printf("ERROR on accept\n");
            continue;
        }

        struct socketInfo *info = (struct socketInfo *)malloc(sizeof(struct socketInfo));
        if (info == NULL) {
            printf("ERROR allocating memory\n");
            close(newsockfd);
            continue;
        }
        info->host = SERVER_IP;
        info->sockfd = newsockfd;

        pthread_create(&thread[i++], NULL, menu_use, (void *)info);

        if (i >= 10) {
            for (int j = 0; j < 10; j++) {
                pthread_join(thread[j], NULL);
            }
            i = 0;
        }
    }
    close(sockfd);
    return 0;
}
Leave a Comment