Started on C-client
This commit is contained in:
parent
3a44c2b3ae
commit
965e3d18c8
14 changed files with 1685 additions and 1 deletions
40
CUDSClient/main.c
Normal file
40
CUDSClient/main.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue