32 lines
1 KiB
C
32 lines
1 KiB
C
#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
|
|
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
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") ;
|
|
}
|
|
|
|
|
|
unlink (CLIENT_SOCK_FILE);
|
|
return 0;
|
|
}
|