我正在编写一个多路复用的客户机/服务器和我的服务器,在收到来自客户机的登录命令后,等待帐户数据,但收到一个空结构,我不知道为什么。这是我的代码:Server:
autentica function:
client login function:
我尝试使用函数sleep (),但它不起作用。有什么建议吗?
发布于 2015-04-14 08:22:05
我还没有看到所有的代码,因为这里是晚上!:)
但是我发现函数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-14 08:22:26
可能在
if( (fd = accept(......)) <0)我认为这个任务也会导致问题,有些时候你不知道如何在application.You中运行,在judgment.Of课程之前可以做评估,这是我对that.Because的想法,我也遇到过同样的问题。我希望这对you.Or有帮助,帮助你找到解决它的方法。告诉我,谢谢!
发布于 2015-04-16 06:29:08
解决了!问题是我在第一个write()中传递了一个常量:write(socket, (comm_t *)LOGIN, sizeof(comm_t));,而我必须首先将"LOGIN“保存在一个变量中,然后以这种方式传递它:write(socket, &command, sizeof(comm_t));
https://stackoverflow.com/questions/29617062
复制相似问题