c - 缓冲服务器 C

标签 c sockets server buffer

我一直在用 c 语言尝试这个服务器代码,它第一次正常工作。但是,当客户端第二次调用它时,缓冲区中保留了客户端前一条消息的值,而第二条消息的值将被覆盖。

例如第一条消息是Hello,第二条消息是Hi,缓冲区将是Hello。我该如何解决?

/*
    C socket server example, handles multiple clients using threads
*/

#include<stdio.h>
#include<string.h>    //strlen
#include<stdlib.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>    //write
#include<pthread.h> //for threading , link with lpthread
 int i;
//the thread function

void *connection_handler(void *);

int main(int argc , char *argv[])
{
    int socket_desc , client_sock , c , *new_sock;
    struct sockaddr_in server , client;

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8080 );

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
        //print the error message
        perror("bind failed. Error");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);


    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
    while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
    {
        puts("Connection accepted");

        pthread_t sniffer_thread;
        new_sock = malloc(1);
        *new_sock = client_sock;

        if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
        {
            perror("could not create thread");
            return 1;
        }

        //Now join the thread , so that we dont terminate before the thread
        //pthread_join( sniffer_thread , NULL);
        puts("Handler assigned");
    }

    if (client_sock < 0)
    {
        perror("accept failed");
        return 1;
    }

    return 0;
}

/*
 * This will handle connection for each client
 * */
void *connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size;
    char *message , client_message[];



    //Receive a message from client
    while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
    {
        //Send the message back to client
        write(sock , client_message , strlen(client_message));


    }

    if(read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }


    //Free the socket pointer
    free(socket_desc);

    return 0;
}

最佳答案

不要使用strlen(client_message),使用read_size。那就是你得到了多少字节,所以这就是你发送了多少字节。

问题是消息末尾没有 NUL 终止符,所以 strlen 不起作用。要在消息末尾放置 NUL 终止符,请执行以下操作:

client_message[read_size] = '\0';

然后您可以将client_message 与接受字符串的函数一起使用,例如strlenprintfputs、等

但如果您所做的只是将消息回显给发送者,则无需为 NUL 终止符操心。

关于c - 缓冲服务器 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39632055/

相关文章:

c# - C/C++/C# 设置窗口位置 : Window on top of others

c - 如何使用gcc将homepath导入c程序

c - 向函数传递指针

java 服务器仅从 1 个客户端接收数据,而不是从第 2 个客户端接收数据

java - Java读取protobuf消息时出现异常

php.ini 文件不起作用?

tomcat - tomcat服务器上的请求源

c++ - 如果我将数字存储为整数数据类型,如何检查一个数字是否存在于另一个数字中?

c++ - 手动指定发送数据的网络接口(interface)

asp.net - 更改名称服务器时 '/park' 应用程序中的服务器错误