checked/CUDSClient/main.c

33 lines
1 KiB
C
Raw Normal View History

2024-11-28 13:27:02 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define CLIENT_SOCK_FILE "/tmp/cclient.sock"
#define SERVER_SOCK_FILE "/tmp/udsserver.sock"
int main(void) {
struct sockaddr_un server_addr = {};
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, SERVER_SOCK_FILE); // XXX: should be limited to about 104 characters, system dependent
// get socket
2024-11-28 14:22:48 +01:00
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
2024-11-28 13:27:02 +01:00
2024-11-28 14:22:48 +01:00
if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) > -1) {
unsigned char messageBuffer[500] = {};
const char* message = "Hello from C";
size_t messageLengthInBytes = strlen(message);
messageBuffer[0] = messageLengthInBytes;
// Lucky for us the message just happens to be compatible with UTF-8 encoding
strcpy((char *)messageBuffer+1, message);
send(sockfd, messageBuffer, messageLengthInBytes+1, 0);
} else {
perror("Unable to connect") ;
2024-11-28 13:27:02 +01:00
}
unlink (CLIENT_SOCK_FILE);
return 0;
}