40 lines
1.3 KiB
C
40 lines
1.3 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
|
|
|
|
struct sockaddr_un client_addr = {};
|
|
client_addr.sun_family = AF_UNIX;
|
|
strcpy(client_addr.sun_path, CLIENT_SOCK_FILE);
|
|
|
|
// get socket
|
|
int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
// bind client to client_filename
|
|
if (bind(sockfd, (struct sockaddr *) &client_addr, sizeof(client_addr)) > 0) {
|
|
if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) > 0) {
|
|
unsigned char* messageBuffer = malloc(500);
|
|
memset(messageBuffer, 0, 500);
|
|
const char* message = "Hello from C";
|
|
size_t messageLengthInBytes = strlen(message);
|
|
messageBuffer[0] = messageLengthInBytes;
|
|
strcpy((char *)messageBuffer+1, message);
|
|
send(sockfd, messageBuffer, messageLengthInBytes+1, 0);
|
|
free(messageBuffer);
|
|
}
|
|
}
|
|
|
|
// connect client to server_filename
|
|
|
|
|
|
unlink (CLIENT_SOCK_FILE);
|
|
return 0;
|
|
}
|