问题描述
我必须开发一个 rpc 程序来显示所有连接到服务器的用户,显示哪些是系统用户,哪些是普通用户。 我查了很多帖子,但我仍然不知道如何正确使用 fgetpwent(),我留下了下面的代码。
可能还有更多我还没发现的错误,但我的问题是关于fgetpwent()的使用
服务器代码:
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include "entrega3.h"
salidaop1 *
op1_1_svc(void *argp,struct svc_req *rqstp)
{
struct passwd {
char *pw_name; /* nombre de usuario */
char *pw_passwd; /* contraseña cifrada */
uid_t pw_uid; /* id. del usuario */
gid_t pw_gid; /* id. del grupo primario */
char *pw_gecos; /* nombre real */
char *pw_dir; /* directorio de inicio */
char *pw_shell; /* programa caparazón */
};
static salidaop1 result ;
FILE *file ;
int numSystemUsers,numnormalUsers,index1,index2 ;
struct passwd *users ;
xdr_free((xdrproc_t) xdr_salidaop1,(char *) &result) ;
file = fopen("/etc/passwd","r") ;
while(users = fgetpwent(file)) {
if (users -> pw_uid >= 1000) numnormalUsers++ ;
else numSystemUsers++ ;
}
fclose(file) ;
result.system.v1_len = numSystemUsers ;
result.normal.v2_len = numnormalUsers ;
result.system.v1_val = malloc(result.system.v1_len * sizeof(char)) ;
result.normal.v2_val = malloc(result.normal.v2_len * sizeof(char)) ;
file = fopen("/etc/passwd","r") ;
index1 = 0;
index2 = 0;
while(users = fgetpwent(file)) {
if (users -> pw_uid >= 1000) {
result.normal.v2_val[index2] = *users -> pw_name ;
index2++;
} else {
result.system.v1_val[index1] = *users -> pw_name ;
index1++;
}
}
fclose(file) ;
return &result;
}
客户代码:
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "entrega3.h"
void
nprog_1(char *host,int opcion)
{
CLIENT *clnt;
salidaop1 *result_1;
char *op1_1_arg;
#ifndef DEBUG
clnt = clnt_create (host,NPROG,NVERS,"udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
switch (opcion) {
case 1:
result_1 = op1_1((void*)&op1_1_arg,clnt);
if (result_1 == (salidaop1 *) NULL) {
clnt_perror (clnt,"call Failed");
} else {
printf("Los usuarios del sistema son: ");
for (int i = 0; i < result_1 -> system.v1_len ; i++)
printf("%d,",result_1 -> system.v1_val[i]) ;
printf("\nLos usuarios normales son: ");
for (int i = 0; i < result_1 -> normal.v2_len ; i++)
printf("%d,result_1 -> normal.v2_val[i]) ;
}
break;
case 2:
break;
}
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
int
main (int argc,char *argv[])
{
char *host;
int opcion;
if (argc < 2) {
printf ("usage: %s server_host\n",argv[0]);
exit (1);
}
host = argv[1];
setbuf(stdin,NULL) ;
printf("\n+------------------------+\n");
printf("| Bienvenido |\n");
printf("+------------------------+\n");
printf("| 1. Usuarios Conectados |\n");
printf("| 2. Usuarios Conectados |\n");
printf("| 3. Salir |\n");
printf("+------------------------+\n");
printf("\nSeleccione una opción (1 - 3): ") ;
scanf("%d",&opcion) ;
if (opcion == 3) exit(0);
if (opcion < 1 || opcion > 3) {
printf("Error al selecionar la opción.\n") ;
exit(1);
}
nprog_1 (host,opcion);
exit (0);
}
当我同时运行服务器和客户端时,我得到一个无限循环: Image
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)