1

我正在编写一个多路复用的客户端/服务器和我的服务器,在收到来自客户端的 LOGIN 命令后,等待帐户数据,但收到一个空结构,我不知道为什么。这是我的代码: 服务器:

奥替尼卡功能:

客户端登录功能:

我尝试使用函数 sleep(),但它不起作用。有什么建议么?

4

3 回答 3

1

我还没有看到所有的代码,因为这里是晚上!!!:)

但我看到函数 autentica (autentica.c) 的代码似乎以错误的方式使用 strcmp!我认为这可能是一个问题!

这是:

 while (!feof(file)) {//legge finchè non finisce il file
        fread(&acc_to_cmp, sizeof(acc_to_cmp), 1, file);//recupera un account dal file
        if (strcmp(account.matricola, acc_to_cmp.matricola)) {//se l'utente esiste
            if (strcmp(account.password, acc_to_cmp.password)) {//controlla la password
                write(fd, (comm_t *)OK, sizeof(comm_t));//se coincidono concedi l'accesso
                fclose(file);
                return 1;
            }
            else{//se non coincidono
                write(fd, (comm_t *)WRONG_PASS, sizeof(comm_t));//nega l'accesso
                fclose(file);
                return 0;
            }
        }
    }//se l'utente non esiste

我认为应该是:

 while (!feof(file)) {//legge finchè non finisce il file
        fread(&acc_to_cmp, sizeof(acc_to_cmp), 1, file);//recupera un account dal file
        if (!strcmp(account.matricola, acc_to_cmp.matricola)) {//se l'utente esiste
            if (!strcmp(account.password, acc_to_cmp.password)) {//controlla la password
                write(fd, (comm_t *)OK, sizeof(comm_t));//se coincidono concedi l'accesso
                fclose(file);
                return 1;
            }
            else{//se non coincidono
                write(fd, (comm_t *)WRONG_PASS, sizeof(comm_t));//nega l'accesso
                fclose(file);
                return 0;
            }
        }
    }//se l'utente non esiste

当两个字符串相等时,strcmp 函数返回 0!:)

于 2015-04-14T00:22:05.243 回答
0

可能在

    if( (fd = accept(......)) <0)

我认为这个赋值也会导致问题,有时你不知道它们如何在应用程序中运行。你可以在判断之前进行评估。当然,这是我的想法。因为我也有同样的问题. 我希望这对你有帮助。或者帮助你找到解决它的方法。告诉我,谢谢!

于 2015-04-14T00:22:25.707 回答
0

解决!问题是我在第一个 write() 中传递了一个常量:write(socket, (comm_t *)LOGIN, sizeof(comm_t)); 而我必须先将“LOGIN”保存在一个变量中,然后以这种方式传递它:write(socket, &command, sizeof(comm_t));

于 2015-04-15T22:29:08.340 回答