Skip to content

Instantly share code, notes, and snippets.

@MikuAuahDark
Last active July 10, 2022 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikuAuahDark/750fd063a7ca4093e9b53dfe9dffadcb to your computer and use it in GitHub Desktop.
Save MikuAuahDark/750fd063a7ca4093e9b53dfe9dffadcb to your computer and use it in GitHub Desktop.
UDS Test
/* clang uds_client.c -lws2_32 -Wno-deprecated-declarations */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
struct sockaddr_un
{
ADDRESS_FAMILY sun_family;
char sun_path[108];
};
int main()
{
WSADATA wsaData = {0};
struct sockaddr_un sockIn = {AF_UNIX};
SOCKET sock;
char buf[1024];
int err = 0, recvSize;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
err = WSAGetLastError();
puts("FATAL CANNOT INIT SOCKET!");
goto exit;
}
sock = socket(AF_UNIX, SOCK_STREAM, 0);
memcpy(sockIn.sun_path, "uds.sock", sizeof("uds.sock"));
printf("Path: %s\n", sockIn.sun_path);
if (connect(sock, (struct sockaddr *) &sockIn, sizeof(sockIn.sun_path)) == -1)
{
err = WSAGetLastError();
puts("Connect error");
goto exit;
}
recvSize = recv(sock, buf, 1024, 0);
if (recvSize <= 0)
{
err = WSAGetLastError();
puts("Recv error");
goto exit;
}
printf("Received: %s\n", buf);
exit:
if (err)
printf("WSA Error (%d)\n", err);
WSACleanup();
return err > 0;
}
<?php
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
$path = __DIR__ . DIRECTORY_SEPARATOR . 'uds.sock';
if (socket_connect($sock, $path))
{
$buf = "";
if (socket_recv($sock, $buf, 1024, MSG_WAITALL))
echo "Received: $buf", PHP_EOL;
socket_close($sock);
}
else
echo "Failed to connect", PHP_EOL;
<?php
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
$path = __DIR__ . DIRECTORY_SEPARATOR . 'uds.sock';
if (file_exists($path))
unlink($path);
if (!socket_bind($sock, $path))
{
echo "Failed to bind Unix Socket", PHP_EOL;
exit(1);
}
if (!socket_listen($sock, 5))
{
echo "Failed to listen", PHP_EOL;
exit(1);
}
$response = "Hello from PHP " . PHP_VERSION . " from " . PHP_OS;
while (true)
{
$s = socket_accept($sock);
$data = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nDate: " . gmdate('D, d M Y H:i:s T') . "\r\nContent-Length: " . strlen($response) . "\r\n\r\n$response";
echo "Received connection", PHP_EOL;
socket_send($s, $data, strlen($data), 0);
socket_close($s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment