Sunday, September 13, 2020

PHP Unix Socket under Windows

It's been a while that Windows 10 have support of Unix Domain Socket/UDS (although with limitation but that doesn't matter for this blog). While PHP language has some questionable design decisions, let me tell you something.

PHP already prepared for AF_UNIX support in Windows older than you think.

Checking the PHP source code, the earliest presence of sockaddr_un struct in the code existed since PHP 4.1.0. First let's test whetever UDS is supported in PHP 7.4. The server code

In my machine, that code runs (no error message)


So, the client code:

Running the client code prints the following message.

That means UDS is working. "Wait, that's probably PHP completely emulating UDS in Windows", fine, let's write the client code with C instead!

Compiling that code and running it shows this output.


That also means PHP didn't emulate UDS and uses WinSock directly.

So, UDS works in PHP 7.4.1 under Windows. What about earlier version? It should be, right? Since reference to UDS in Windows existed since PHP 4.1.0. So let's try with PHP 5.3.19.

Note: The reason I choose PHP 5.3.19 because I didn't look at older commits that reference sockaddr_un in Windows, and I just see that it's there since 4.1.0 while writing this blog, after doing all testing.



The reverse is also possible, i.e. PHP 7.4.1 connecting to UDS which was created for PHP 5.3.19 and vice versa, but that will clutter this blog post with images.

This is surprising discovery in my opinion. What if PHP team already predicted this blog post after all? Well ...

That's story for another blog post.