0

我有一个小的 TCP 连接,我试图发送一个从哈希表中提取的 char 数组。我可以成功地将一个字符串添加到哈希表中,键是客户端的 IP 地址,但是当我尝试获取键的值时,它将整个表注册为 NULL。

这是文件:

void HandleTCPClient(int clntSocket, char *client_ip, HASHTBL *hashtable)
{
    ...
    ...
    if (strcmp(cmd, "addfile") == 0)
    {
        char arr[1000];
        add = hashtbl_get(hashtable, (char *)client_ip);

        if (add != NULL)
        {
            memcpy(fls, add, strlen(add));
            hashtbl_insert(hashtable, client_ip, fls);
        }
        else
        {
            hashtbl_insert(hashtable, client_ip, fls);
            printf("insert done\n");
        }
        printf("%s\n", hashtbl_get(hashtable, (char *)client_ip));
    }

    else if (strcmp(cmd, "listfiles") == 0)
    {
        char list[1000];
        char *test;

        printf("before getting from hashtable");

        test = hashtbl_get(hashtable, (char *)client_ip);

        printf("%s\n", test);
    }

我调用来获取值的函数hashtbl_get具有标题:

void *hashtbl_get(HASHTBL *hashtbl, const char *key);

所以我(char *)在传递它时将client_ip转换为a。

fls是一个小字符数组

功能在hashtbl_get这里:

void *hashtbl_get(HASHTBL *hashtbl, const char *key)
{
struct hashnode_s *node;
hash_size hash=hashtbl->hashfunc(key)%hashtbl->size;

node=hashtbl->nodes[hash];
while(node) {
    if(!strcmp(node->key, key)) return node->data;
    node=node->next;
}

return NULL;
}

问题是,当我运行我的服务器并调用addfile它添加到哈希表的命令时,但是当我调用listfiles它时,它的行为就好像哈希表现在是空的。知道是什么原因造成的吗?

4

0 回答 0