0

I'm trying to create a communication between two processes after a fork, thus between a father process and a son process. I'm trying to use sockets with PF_UNIX and AF_UNIX families, but when I try to send a message, the error "transmission endpoint not connected" occurs. I don't really understand where my error is.

#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;
}
alk
  • 69,737
  • 10
  • 105
  • 255
Tom Riddle
  • 33
  • 5

2 Answers2

0

You want to call send on the accepted socket not on the listening socket.

So this

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

should be

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

Aside of this there are other issues:

  • len should be socklen_t not int.
  • The client part fails to check if the call to socket() succeeded.
  • strlen() on an empty string returns 0.
alk
  • 69,737
  • 10
  • 105
  • 255
  • Okay, the first argument of send was definitely my problem. I read about the method and I thought that it would have sent the message to all the socket connected with the accept. I also corrected about the string, and it works perfectly. Thanks! – Tom Riddle May 27 '18 at 17:22
0

Transmission on endpoint 2 failures (on ICD-3) point you at the target board complaining about `voltages'. This is wrong. Its a usb driver permissions error (well on Linux)

this fixes it

$ cd /dev/bus/usb
$ sudo chmod 777 *
user50619
  • 325
  • 5
  • 14