C 中 client/server 通信期间的奇怪字符

Strange characters during client/server comunication in C

我正在编写一个 client/server 应用程序,其中客户端只需向服务器发送一个字符串,然后服务器重新发送相同的字符串。 我 运行 都喜欢这样: ./服务器本地主机 8000 ./client localhost 8000 StringToSend

Server.c

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv){

    int sd, err, on, len;
    int GO = 1;
    struct addrinfo hints, *res;

    if(argc != 3){
        printf("errore\n");
    }


    memset(&hints, 0, sizeof(hints));

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;



    if ((err = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0){
        fprintf(stderr, "Error bind: %s\n", gai_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
        perror("Error in socket");
        exit(EXIT_FAILURE);
    }

    on = 1;
    if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0){
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }

    if (bind(sd, res->ai_addr, res->ai_addrlen) < 0){ 

        perror("Error in bind");
        exit(EXIT_FAILURE);
    }

    if(listen(sd, SOMAXCONN) < 0){  
      printf("error in listen\n");
    }

    int ns;

    while(GO){
    ns = accept(sd, NULL, NULL);

    }


    char* buff= malloc(100 *sizeof(char));

    read(sd, buff, sizeof(buff));

    write(sd, buff, sizeof(buff));

}

还有 client.c

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv){

    int sd, err, on, len;
    struct addrinfo hints, *res;

    if(argc != 4){
         printf("errore\n");
    }

    memset(&hints, 0, sizeof(hints));

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    if ((err = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0){
        fprintf(stderr, "Error after bind: %s\n", gai_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
        perror("error when creating a socket");
        exit(EXIT_FAILURE);
    }

    if (connect(sd, res->ai_addr, res->ai_addrlen) == 0){
      printf("connected\n");
    }

    char* buff = malloc(100 * sizeof(char));

    buff = argv[3];

    write(sd, buff, sizeof(buff));

    char* risp = malloc(sizeof(char) * 100);

    read(sd, risp, sizeof(risp));

    puts(risp);

}

发生的情况是客户端收到一个随机字符串,可能是因为“\0”存在一些我不知道如何解决的问题,我尝试手动将“\0”与strcat 但没有任何改变。

你要修改记忆allocation/read/write

当前代码假定最大缓冲区大小为 100 个字符。然后发出指针大小(4 或 8 字节)的读取。然后它无条件地向客户端写回 4 或 8 个字节。

char* buff= malloc(100 *sizeof(char));
read(sd, buff, sizeof(buff));
write(sd, buff, sizeof(buff));

您需要以下内容:最多读取 100 个字符,写回相同数量的字符(包括终止 NULL)。

服务器:

    int MSGSIZE = 100 ;
    char* buff= malloc(MSGSIZE *sizeof(char));
    while(GO){
        ns = accept(sd, NULL, NULL);
        int nread = read(ns, buff, MSGSIZE);
        if ( nread > 0 ) write(ns, buff, nread);
    }

服务器:

    int MSGSIZE = 100 ;
    char* buff= malloc(MSGSIZE *sizeof(char));
    while(GO){
        ns = accept(sd, NULL, NULL);
        int nread = read(ns, buff, MSGSIZE);
        if ( nread > 0 ) write(ns, buff, nread);
    }

客户

    int MSGSIZE = 100 ;
    char* buff= malloc(MSGSIZE *sizeof(char));
    strcpy(buff, argv[3]) ;
    write(sd, buff, MSGSIZE) ;
    char* resp= malloc(MSGSIZE *sizeof(char));
    int nread = read(resp, buff, MSGSIZE);
    if ( nread > 0 ) puts(resp) ;

您还想查看其他一些问题 - 关闭服务器套接字、释放内存等。我希望 read/write 的工作能帮助您继续前进!