Linux C编程

时间:2017-09-08 03:19:34

标签: c linux struct pthreads named-pipes

这是我第一次在这里提问,所以我希望我不犯任何错误...... 我正在做一个服务器 - 客户端程序,当我运行客户端时,我有这个错误,我有"核心转储错误"。

我想我知道错误在哪里,但我不知道如何解决它。 我有2个结构:

typedef struct {
    int pid;
    int cmd; //1 -login,2-registrar,3-tecla
    char login[100];
    char pw[100];
    int tecla;
} PEDIDO;

typedef struct {
    int cmd;
    int res;
    BONECO *b;
} RESPOSTA;

服务器通过命名管道向客户端发送数据时的代码:

int main(int argc, char *argv[], char *envp[]){
//...
     i = read(fd, &p, sizeof (p));
                    printf(">>> Li %d bytes\n", i);
                    printf("User : %s PID : %d\n", p.login, p.pid);
                    if (p.cmd == 1) { //LOGIN

                        char user_fd[100], pw_fd[100];
                        FILE *f = fopen(argv[1], "r");
                        if (!f) {
                            printf("Erro ao abrir ficheiro\n");
                            exit(0);
                        }
                        while ((fscanf(f, "%s %s ", user_fd, pw_fd))) {
                            if (strcmp(p.login, user_fd) == 0 && strcmp(p.pw,pw_fd) == 0) {
                                printf("\nLOGIN CORRECTO!!!\n");
                                for (i = 0; i < 20; i++) {
                                    if (clientes[i][0] != p.pid && clientes[i][0] == -1) {
                                        clientes[i][0] = p.pid;
                                        break;
                                    }
                                }
                                sprintf(cli_fifo, "ccc%d", p.pid);
                                fd_resp = open(cli_fifo, O_WRONLY);
                                r.cmd = 1;
                                r.res = 1;
                                i = write(fd_resp, &r, sizeof (r));
                                close(fd_resp);
                                fclose(f);
                                break;
                            }

//...

}

从服务器接收数据的客户端代码:

void *recebe_msg(void *dados) {
    int i;
    sprintf(cli_fifo, "ccc%d", getpid());
    mkfifo(cli_fifo, 0600);
    fd_resp = open(cli_fifo, O_RDWR);
    menu();
    do {
        i = read(fd_resp, &r, sizeof (r));
        if (ESTADO == 0) {
            if (r.cmd == 1 && r.res == 1) { // OK
                ESTADO = 1;
                wclear(win);
                mvwprintw(win, 10, 14, "Login efectuado com sucesso!");

                wprintw(win, "BONECO %d, %d , %d , %d ",r.b[0].num,r.b[0].humano,r.b[0].remate,r.b[0].tempo);

                wrefresh(win);
                sleep(3);
                desenha_campo();
            }
            if (r.cmd == 1 && r.res == 0) { // NOK
                wclear(win);
                mvwprintw(win, 10, 14, "Senha e/ou login incorrectos!!!");
                wrefresh(win);
                sleep(3);
                menu();
            }
            if (r.cmd == 2 && r.res == 0) {
                wclear(win);
                mvwprintw(win, 10, 14, "Login já está em uso!");
                wrefresh(win);
                sleep(3);
                menu();
            }
            if (r.cmd == 2 && r.res == 1) {
                wclear(win);
                mvwprintw(win, 10, 14, "Registo efectuado com sucesso!");
                wrefresh(win);
                sleep(3);
                menu();
            }

            if (r.cmd == 9) { // sair
                FIM = 1;
            }
        } else if (ESTADO == 1 || ESTADO == 2) {
            wclear(win3);
            scrollok(win3, TRUE);
            keypad(win3, TRUE);
            noecho();
            if (r.cmd == 3) { // actualizacao de jogadores
                desenha_campo();
                desenha_jogadores();
            } else if (r.cmd == 9) { // sair
                FIM = 1;
            }
        }
    } while (!FIM);

    close(fd_resp);
    unlink(cli_fifo);
    pthread_exit(0);
}

OBS:我认为错误发生在struct RESPOSTA的第3个字段上,但我不知道如何解决它

1 个答案:

答案 0 :(得分:1)

在这里你读了BONECO结构:

    i = read(fd_resp, &r, sizeof (r));

请注意,BONECO被声明为指针,而不是实际数据。

但是你在这里尝试阅读这些数据:

    wprintw(win, "BONECO %d, %d , %d , %d ",r.b[0].num,r.b[0].humano,r.b[0].remate,r.b[0].tempo);

当您尝试访问r.b [0]时,您会出现分段错误,因为您尝试取消引用b指针,指向...未知位置。这会导致错误。

我建议将结构修改为:

typedef struct {
    int cmd;
    int res;
    int count_of_boneco;
    BONECO b[];
} RESPOSTA;

如果您分配了足够大的缓冲区,您可以阅读所有数据,然后立即取消引用。