可能比较C中的两个字符串/缓冲区

时间:2016-03-31 17:05:29

标签: c

以下代码无效。我一直在寻找一天的解决方案,但令人惊讶的是没有在哪里。当我发送单词"你好"到tcp服务器我希望它打印文本"它工作"。问题出在第97行。一旦我知道发生了什么,我将编辑帖子的标题。

#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
void *connection_handler(void *);

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

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

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

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("bind failed");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc , 3);

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

        //Reply to the client
        message = "Hello Client , I have received your connection. And now I will assign a handler for you\n";
        write(new_socket , message , strlen(message));

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

        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 (new_socket<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[2000];
        //char *contents;
        //contents = "hello";
        //strcpy(mess,contents);
    //Send some messages to the client
    message = "Greetings! I am your connection handler\n";
    write(sock , message , strlen(message));

    message = "Now type something and i shall repeat what you type \n";
    write(sock , message , strlen(message));

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

问题出在这一行

之下
        char mess[] = "hello\n";
        if(strcmp(client_message, mess) != 0){
            printf("it works");
        }
    }
    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;
}

3 个答案:

答案 0 :(得分:3)

如果字符串相等,

strcmp会返回。你的情况不正确。

您应该if (! strcmp(...)) printf("it works");而不是if (strcmp(...) != 0) ...

答案 1 :(得分:1)

Top hello是“你好”

在第97行附近打招呼是“hello \ n”

或者:

1)编辑其中一个hellos

2)使用 stricmp()

答案 2 :(得分:1)

while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )

recv Linux

  

所有三个例程都会在成功完成时返回消息的长度。

在这种情况下,您不能确保client_message具有字符串结尾字符。许多连接只会发送实际的消息字符,而不是最后的NULL字符。你应该添加(假设它适合)

client_message[read_size] = '\0';

这样strlen(client_message)肯定是正确的。或者你可以使用

write(sock , client_message , read_size);

由于您正在重复该消息,因此大小相同。

请注意,如果您在代码中的其他位置使用client_message字符串,则应确保&#39; \ 0&#39;为了安全起见,添加了字符。