Skip to content

Instantly share code, notes, and snippets.

@MikuAuahDark

MikuAuahDark/uds_client.c

Last active Sep 13, 2020
Embed
What would you like to do?
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);
}
while (true)
{
$s = socket_accept($sock);
$data = "Hello from PHP " . PHP_VERSION . " from " . PHP_OS;
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