有问题的套接字服务器

时间:2018-06-10 14:14:39

标签: c sockets server client

我尝试创建服务器 - 客户端套接字方案,其中多个客户端通过套接字与服务器通信。服务器读取数据并发回一些确认,并行保存一些数据。

我的server.c代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

void serve(int);

struct data {
    int checked;
    int all;
}Data;

int main(int argc, char *argv[]) {
    int sockfd, newsockfd, portno;
    struct sockaddr_in serv_addr, cli_addr;
    int pid;
    socklen_t clilen;

    if (argc < 2) {
        fprintf(stderr,"ERROR: NO PORT PROVIDED!\n");
        exit(1);
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        fprintf(stderr,"ERROR: CANNOT CREATE SOCKET!\n");
        exit(1);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        fprintf(stderr,"ERROR: CANNOT BIND CONNECTION!\n");
        exit(1);
    }

    listen(sockfd,5);
    clilen = sizeof(cli_addr);

    while (1) {
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0) {
            fprintf(stderr,"ERROR: CANNOT ACCEPT NEW CONNECTION!\n");
            exit(1);
        }

        pid = fork();
        if (pid < 0) {
            fprintf(stderr,"ERROR: CANNOT CREATE NEW PROCESS!\n");
            exit(1);
        }

        if (pid == 0)  {
            close(sockfd);
            serve(newsockfd);
            exit(0);
        }
        else
            close(newsockfd);
    }

    return 0;
}

void serve(int new) {
    int n;
    int buffer[256];
    int sum = 0;
    int m = 0;
    int i = 0;
    int size = 0;

    bzero(buffer, 256);

    n = read(new, buffer, 256);
    if (n == NULL) {
        fprintf(stderr, "ERROR: CANNOT READ FROM CLIENT!\n");
        exit(1);
    }

    do {
        size++;
        i++;
    }while (buffer[i] != 0);

    for (i = 0; i < size; i++)
        sum += buffer[i];
    m = sum / size;

    sum = 0;

    if (m > 10) {
        Data.checked++;
        Data.all++;

        n = write(new, "Sequence OK", 11);
        if (n == NULL) {
            fprintf(stderr, "ERROR: CANNOT WRITE TO CLIENT!\n");
            exit(1);
        }
    } else {
        Data.all++;
        n = write(new, "Check failed", 12);
    }

    printf("Data so far:\n1)Number of sequences received: %d\n2)Number of sequences with average above 10: %d\n", Data.all, Data.checked);
} 

我的client.c代码是:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <memory.h>

int main(int argc, char *argv[]) {
    int sockfd, portno, n;

    struct sockaddr_in serv_addr;
    struct hostent *server;

    int buffer[256];
    char buffer2[13];
    char answer;
    int i;
    int size = 0;

    if (argc < 3) {
        fprintf(stderr,"ERROR: CANNOT USE %s PORT!\n", argv[0]);
        exit(0);
    }

    portno = atoi(argv[2]);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0){
        fprintf(stderr, "ERROR: CANNOT CREATE SOCKET!\n");
        exit(1);
    }

    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR: NO HOST FOUND!\n");
        exit(1);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy(server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);

    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
        fprintf(stderr, "ERROR: CANNOT CONNECT TO SERVER!\n");
        exit(1);
    }

    do {
        bzero(buffer, 256);

        printf("Give the numbers now and enter 0 to stop\n");
        for (i = 0; i < 256; i++) {
            scanf("%d", &buffer[i]);
            if (buffer[i] == 0)
                break;
        }

        do {
            size++;
            i++;
        }while (buffer[i] != 0);

        n = write(sockfd, buffer, size);
        if (n < 0) {
            fprintf(stderr, "ERROR: CANNOT WRITE TO SERVER!\n");
            exit(1);
        }

        bzero(buffer2, 13);
        n = read(sockfd, buffer2, 13);
        if (n < 0) {
            fprintf(stderr, "ERROR: CANNOT READ FROM CLIENT!\n");
            exit(1);
        }
        printf("%s\n", buffer2);

        printf("Do you want to enter another sequence?\ty (yes) || n (no)\n");
        scanf(" %c", &answer);

        system("clear");
    }while (answer == 'y');

    return 0;
}

所以问题是服务器使用不同的客户端(多个终端),但第一次使用后不能使用同一个客户端。我是整个服务器客户端的新手,所以我有点挣扎。有没有人有想法?

提前致谢。

0 个答案:

没有答案