【问题标题】:transmission endpoint not connected传输端点未连接
【发布时间】:2018-11-06 02:57:24
【问题描述】:

我正在尝试在分叉之后创建两个进程之间的通信,从而在父进程和子进程之间创建通信。我正在尝试将套接字与PF_UNIXAF_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;
}

【问题讨论】:

    标签: c sockets fork send unix-socket


    【解决方案1】:

    端点 2 故障(在 ICD-3 上)的传输将您指向目标板,抱怨“电压”。这是错误的。它是一个 USB 驱动程序权限错误(在 Linux 上很好)

    这解决了它

    $ cd /dev/bus/usb
    $ sudo chmod 777 *
    

    【讨论】:

      【解决方案2】:

      你想在接受的套接字上调用发送,而不是在监听的套接字上。

      所以这个

       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

      【讨论】:

      • 好吧,send的第一个参数肯定是我的问题。我阅读了该方法,我认为它会将消息发送到与接受连接的所有套接字。我还纠正了字符串,它工作得很好。谢谢!
      猜你喜欢
      • 2012-12-30
      • 2019-03-15
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多