From 14ba9ab89104319f56b94de68cb177f78f69a739 Mon Sep 17 00:00:00 2001
From: Johan Maasing <johan@zoom.nu>
Date: Thu, 28 Nov 2024 14:22:48 +0100
Subject: [PATCH] Java does not suppport DGRAM sockets

---
 CUDSClient/main.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)

diff --git a/CUDSClient/main.c b/CUDSClient/main.c
index eefd90a..4028ded 100644
--- a/CUDSClient/main.c
+++ b/CUDSClient/main.c
@@ -11,29 +11,21 @@ int main(void) {
     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);
+    int sockfd = socket(AF_UNIX, SOCK_STREAM, 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);
-        }
+    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") ;
     }
 
-    // connect client to server_filename
-
 
     unlink (CLIENT_SOCK_FILE);
     return 0;