0

Here is the Node.js code:

       net = require('net');
       fs  = require('fs');
       serverSocketFile = '/tmp/_mediaComm';
       if (fs.existsSync(serverSocketFile))
         fs.unlinkSync(serverSocketFile);
       var server = net.createServer((socket) => {
         console.log("Connection received.");
         socket.on('data', (data) => {
            //console.log(...);
         });
         //...
       });   
       server.on('error', (err) => {
         console.log("Error:" + err);
       });
       server.listen(serverSocketFile, () => {
          fs.chmodSync(serverSocketFile, '0777');
       });

I start it first. The socket file created is:

    ls -l /tmp/_mediaComm
    srwxrwxrwx 1 usr1 usr1 0 May 27 16:28 /tmp/_mediaComm

Here is relevant part of the C client code

   int retStatus;
   struct sockaddr_un clientUnixSocket;
   struct sockaddr_un serverUnixSocket;
   int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_NONBLOCK, 0);
   if (fd < 0) {
     perror("Socket create error");
   };
   clientUnixSocket.sun_family = AF_UNIX;
   strcpy(clientUnixSocket.sun_path, "/tmp/_clientComm");
   unlink("/tmp/_clientComm");
   if (bind(fd, (struct sockaddr *)&clientUnixSocket, sizeof(clientUnixSocket)) < 0) {
     perror("Client bind error");
   }
   serverUnixSocket.sun_family = AF_UNIX;
   strcpy(serverUnixSocket.sun_path, "/tmp/_mediaComm");
   printf("Sending data...\n");
   retStatus = sendto(fd, "Hi", 2, 0, (struct sockaddr *) &serverUnixSocket, sizeof(struct sockaddr_un));
     if (retStatus == -1) {
         perror("SENDTO ERROR");
     }
     else {
         printf("Data sent!\n");
     }

I get the following error from the C program:

 Sending data...
 SENDTO ERROR: Protocol wrong type for socket
  1. What am I missing in the C code? Also
  2. How do we tell the Node server to be in DGRAM and not STREAM mode?
asinix
  • 966
  • 1
  • 9
  • 22

1 Answers1

0

Node.js does not support UNIX domain sockets in DGRAM mode. Only in STREAM mode!

asinix
  • 966
  • 1
  • 9
  • 22