I'm rewriting a PHP application in Node.js. A C program runs on the Linux OS that also hosts PHP.
In short, the C program handles sending data from the PHP web interface to some proprietary hardware. It also handles the creation of the socket file, and various other tasks.
In the legacy code, PHP runs as expected by binding to the socket and transmits/receives data as expected.
I have written both a Node.js Server and Client script to see that I can create and connect to Unix Domain sockets using the native 'net' module. This works as expected.
However, when I attempt to connect the node.js socket client to the socket file created by the C program, I see the following error:
{
Error: connect EPROTOTYPE /var/run/arcontroller-master.socket
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'EPROTOTYPE',
errno: 'EPROTOTYPE',
syscall: 'connect',
address: '/var/run/arcontroller-master.socket'
}
Research has revealed that EPROTOTYPE: Protocol wrong type for socket
.
No information is given in the net module documentation for why the protocol is wrong or how to define the correct protocol.
Can you define a different protocol in node.js? Perhaps this is a misunderstanding on my part but I thought IPC was just a IP connection through a unix domain socket. I am mistaken here?
Further Information:
The C Program uses the sys/socket
lib under the hood, an abstract of the socket creation process is outlined below:
// Open up the socket we're listening on
struct sockaddr_un server_address;
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, socketFile);
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
Further reading shows SOCK_DGRAM is a UDP socket
- what am I missing here?
See man unix(7)
:
EPROTOTYPE
Remote socket does not match the local socket type (SOCK_DGRAM
versusSOCK_STREAM
).
I can create and connect to Unix Domain sockets using the native 'net' module.
net
module provides stream sockets, not datagram. This is the reason you observe the error.
You need to use a datagram socket in your node.js code. See node.js documentation for UDP / Datagram Sockets.