我正在尝试在 Linux 中使用命名管道创建服务器/客户端程序。每次客户端连接并进行身份验证时,服务器都会将他的数据(pid 和密码)存储在一个链表中。
问题是,在我存储第一个客户端的数据后,每当我尝试存储更多数据(客户端)时,程序都会“给出”分段错误。
typedef struct client client, *pno;
struct client
{
pid_t pid;
char password[TAM_MAX];
pno next;
};
int verify_struct(pno cliente_t)
{
if (cliente_t == NULL) //verifica se a lista está vazia
return 1;
else
return 0;
}
pno AddClient(pno cliente_t, pid_t pid, char password[TAM_MAX])
{
pno new, aux;
new = malloc(sizeof(client)); //aloca espaço
if(new == NULL) //verifica se alocou o espaço com sucesso
{
printf("Memory allocation error!\n");
return cliente_t;
}
new->pid = pid;
strncpy(new->password, password, TAM_MAX-1);
if(verify_struct(cliente_t))
{
printf("Should be at start\n");
cliente_t = new;
printf("Added at start!\n");
}
else
{
//insert at end
printf("Adding in the end!\n");
aux = cliente_t;
while(aux->next != NULL)
aux = aux->next;
aux->next = new;
printf("Added sucssefully!\n");
}
return cliente_t;
}
bool isValidUser(pno cliente, pid_t pid, char password[TAM_MAX])
{
while(cliente != NULL)
{
if(cliente->pid == pid && strncmp(password, cliente->password, 100) == 0)
{
return true;
}
cliente = cliente -> proximo;
}
return false;
}
//progam itself :
int main(void)
{
pno client=NULL;
/* --code-- */
while(1)
{
if(request.loggedIn == 0)
{
client=AddClient(client, request.pid, request.password);
}
else
{
if(!isValidUser(cliente, perg.pid_cliente, perg.password))
abort();
//process commands
}
}
}
输出:
1st Client -> Should be at start!
-> Added at start!
2nd Client -> Adding in the end!
-> segmentation fault (core dumped).