Unix Domain Socket: Using datagram communication between one server process and several client processes

前端 未结 7 1258
野趣味
野趣味 2020-12-12 17:25

I would like to establish an IPC connection between several processes on Linux. I have never used UNIX sockets before, and thus I don\'t know if this is the correct approach

7条回答
  •  自闭症患者
    2020-12-12 17:42

    You can solve the bind error with the following code:

    int use = yesno;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&use, sizeof(int));
    

    With UDP protocol, you must invoke connect() if you want to use write() or send(), otherwise you should use sendto() instead.

    To achieve your requirements, the following pseudo code may be of help:

    sockfd = socket(AF_INET, SOCK_DGRAM, 0)
    set RESUSEADDR with setsockopt
    bind()
    while (1) {
       recvfrom()
       sendto()
    }
    

提交回复
热议问题