конечная точка передачи не подключена - PullRequest
0 голосов
/ 27 мая 2018

Я пытаюсь создать связь между двумя процессами после разветвления, то есть между процессом отца и процессом сына.Я пытаюсь использовать сокеты с семействами PF_UNIX и AF_UNIX, но когда я пытаюсь отправить сообщение, возникает ошибка "конечная точка передачи не подключена".Я не очень понимаю, где моя ошибка.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/un.h>

#define PATH "/tmp/Server"
int main() {
    pid_t proc1, proc2;
    proc1 = fork();
    if(proc1 == -1) {
        printf("Errore nella prima fork\n");
        exit(0);
    }

    if(proc1 != 0) {
        //PADRE DOPO IL PRIMO FIGLIO 

        //Lo stesso procedimento si fa per il secondo figlio
        proc2 = fork();
        if(proc2 == -1) {
            printf("Errore nella prima fork\n");
            exit(0);
        }
        wait(NULL); //Attesa che UN figlio termini
        if(proc2 != 0) {
            //PADRE DOPO I DUE FIGLI
            unlink(PATH);
            int father_desc = socket(PF_UNIX, SOCK_STREAM, 0);
            int son1_desc;
            struct sockaddr_un father_add = {AF_UNIX, PATH};
            struct sockaddr_un son1_add;

            if(-1 == bind(father_desc, (struct sockaddr*)&father_add, sizeof(struct sockaddr))) {
                perror("Errore nel bind");
                close(father_desc);
                close(son1_desc);
                wait(NULL);
                exit(0);
            }

            if(-1 == listen(father_desc, 2)) {
                perror("Errore nel listen");
                close(father_desc);
                close(son1_desc);
                wait(NULL);
                exit(0);
            }

            int len = sizeof(son1_add);
            son1_desc = accept(father_desc, (struct sockaddr*)&son1_add, &len);
            if(-1 == son1_desc) {
                perror("Errore nell'accept");
                close(father_desc);
                close(son1_desc);
                wait(NULL);
                exit(0);
            }       

            char message[256] = "Ciao";
            if(-1 == send(father_desc, message, strlen(message) + 1, 0)) {
                perror("Errore nel send"); /* -> ERROR HERE */
                close(father_desc);
                close(son1_desc);
                wait(NULL);
                exit(0);
            }

            close(father_desc);
            close(son1_desc);
            wait(NULL);
        } else {
            //SECONDO FIGLIO

        }
    } else {
    //PRIMO FIGLIO
        sleep(1);
        struct sockaddr_un son1_add = {AF_UNIX, PATH};
        int son1_desc = socket(PF_UNIX, SOCK_STREAM, 0);

        if(-1 == connect(son1_desc, (struct sockaddr*) &son1_add, sizeof(struct sockaddr))) {
            perror("Errore nel connect");
            shutdown(son1_desc, SHUT_RDWR);
            close(son1_desc);
            exit(0);    
        }

        char message[256] = ""; 
        if(-1 == recv(son1_desc, message, strlen(message) + 1, 0)) {
            perror("Errore nel rcv");
            shutdown(son1_desc, SHUT_RDWR);
            close(son1_desc);
            wait(NULL);
            exit(0);
        }

        printf("%s\n", message);

        shutdown(son1_desc, SHUT_RDWR);
        close(son1_desc);
    }
    return 0;
}

Ответы [ 2 ]

0 голосов
/ 19 октября 2018

Передача при сбоях конечной точки 2 (на ICD-3) указывает на целевую плату, которая жалуется на «напряжения».Это не верно.Это ошибка прав доступа к USB-драйверу (хорошо в Linux)

это исправляет

$ cd /dev/bus/usb
$ sudo chmod 777 *
0 голосов
/ 27 мая 2018

Вы хотите вызвать send на принятом сокете, а не на слушающем сокете.

Так что

 if(-1 == send(father_desc, message, strlen(message) + 1, 0)) {

должно быть

 if(-1 == send(son1_desc, message, strlen(message) + 1, 0)) {

Кроме этогодругие проблемы:

  • len должно быть socklen_t, а не int.
  • Клиентская часть не может проверить, успешен ли вызов socket().
  • strlen() в пустой строке возвращает 0.
...