C语言 C:如何从套接字上的 read() 清除缓冲区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16136434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:07:23  来源:igfitidea点击:

C: how to clear out buffer from a read() on a socket

cstringsocketsbuffer

提问by user2158382

I am trying to receive data from a server, and it works fine for the first time, but as read() keeps looping it will also store the old values it previously read. Here is what i have so far.

我正在尝试从服务器接收数据,它第一次工作正常,但随着 read() 不断循环,它也会存储以前读取的旧值。这是我到目前为止所拥有的。

        char receive[50];

        if((he = gethostbyname(servername)) == NULL ) {

            perror(strcat("Cannot find server named:", servername));
            exit(0);
        }

        he = gethostbyname("localhost");
        localIP = inet_ntoa(*(struct in_addr *)*he->h_addr_list);
        client_sock_desc = socket(AF_INET, SOCK_STREAM, 0);
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = inet_addr(localIP);
        server_addr.sin_port = htons(serverport);
        len = sizeof(server_addr);
        if(connect(client_sock_desc, (struct sockaddr *)&server_addr,len) == -1) {
            perror("Client failed to connect");
            exit(0);
        }

        strcpy(buf, "CLIENT/REQUEST\n");
        send(client_sock_desc, buf, strlen(buf), 0);
        //send actual function request

        //put a space before \n char to make it easier for the server
        for(i = 0; i < sizeof(wholeRequest); i++) {
            if(wholeRequest[i] == '\n') {
                wholeRequest[i] = ' ';
                wholeRequest[i+1] = '\n';
                break;
            }
        }

        while(read(client_sock_desc, receive, sizeof(receive)) > 0) {
            strcpy(receive, ""); //attempt to erase all old values
            printf(receive);
            fflush(stdout);
        }
        close(client_sock_desc);

When the server sends data once and closes the socket, it works perfectly. But then I have the client open the socket again, send data to the server, and the server will once again send data to the client and close the socket. The client will again try to read the data the server sent, but this time it fills receive with both the new information and part of the old information

当服务器发送一次数据并关闭套接字时,它完美地工作。但是后来我让客户端再次打开套接字,向服务器发送数据,服务器将再次向客户端发送数据并关闭套接字。客户端将再次尝试读取服务器发送的数据,但这次它用新信息和部分旧信息填充接收

采纳答案by Floris

It seems to me that in your code you delete the received data before printing it - then you pass a string to printfthat is basically empty, and I'm not sure what printfdoes with that (since it is the formatting string that is empty).

在我看来,在您的代码中,您在打印之前删除了接收到的数据 - 然后您将一个字符串传递给它,该字符串printf基本上是空的,我不确定printf它的作用(因为它是空的格式字符串)。

Try this:

尝试这个:

int nread;
while((nread = read(client_sock_desc, receive, sizeof(receive)-1)) > 0) {
        receive[nread]='##代码##';    // explicit null termination: updated based on comments
        printf("%s\n",receive); // print the current receive buffer with a newline
        fflush(stdout);         // make sure everything makes it to the output
        receive[0]='##代码##';        // clear the buffer : I am 99% sure this is not needed now
    }