Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

Detail an exemple using Abstract unix socket

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    Detail an exemple using Abstract unix socket

  2. Assistant

    Certainly! Abstract Unix domain sockets are a special kind of Unix socket available on Linux that do not create a socket file in the filesystem. Instead, their address starts with a null byte (\0) and lives only in the kernel's socket namespace.

    This means:

    • You must bind the server socket to an abstract address (starting with \0).
    • No socket file is created on disk.
    • Clients connect to the same abstract address.

    Example: Server and Clients Using Abstract Unix Domain Sockets (C)


    1. Server Code

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <errno.h>
    
    #define ABSTRACT_SOCK_NAME "\0my_abstract_socket"
    #define BUFFER_SIZE 256
    
    int main() {
        int server_fd, client_fd;
        struct sockaddr_un addr;
        char buffer[BUFFER_SIZE];
    
        // Create socket
        server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (server_fd == -1) {
            perror("socket");
            exit(EXIT_FAILURE);
        }
    
        // Clear and set address
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
    
        // Abstract socket: first byte of sun_path is '\0'
        strncpy(addr.sun_path, ABSTRACT_SOCK_NAME, sizeof(addr.sun_path) - 1);
    
        // Bind socket to abstract address
        // Note: length = offsetof + length of sun_path (including leading \0)
        int addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(ABSTRACT_SOCK_NAME);
        if (bind(server_fd, (struct sockaddr *)&addr, addr_len) == -1) {
            perror("bind");
            close(server_fd);
            exit(EXIT_FAILURE);
        }
    
        // Listen for connections
        if (listen(server_fd, 5) == -1) {
            perror("listen");
            close(server_fd);
            exit(EXIT_FAILURE);
        }
    
        printf("Server listening on abstract socket '%s'\n", ABSTRACT_SOCK_NAME + 1);
    
        while (1) {
            client_fd = accept(server_fd, NULL, NULL);
            if (client_fd == -1) {
                perror("accept");
                continue;
            }
    
            ssize_t n = read(client_fd, buffer, BUFFER_SIZE - 1);
            if (n > 0) {
                buffer[n] = '\0';
                printf("Received: %s\n", buffer);
    
                const char *response = "Message received\n";
                write(client_fd, response, strlen(response));
            }
            close(client_fd);
        }
    
        close(server_fd);
        return 0;
    }
    

    2. Client Code

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <errno.h>
    
    #define ABSTRACT_SOCK_NAME "\0my_abstract_socket"
    #define BUFFER_SIZE 256
    
    int main() {
        int sock_fd;
        struct sockaddr_un addr;
        char buffer[BUFFER_SIZE];
    
        sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (sock_fd == -1) {
            perror("socket");
            exit(EXIT_FAILURE);
        }
    
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, ABSTRACT_SOCK_NAME, sizeof(addr.sun_path) - 1);
    
        int addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(ABSTRACT_SOCK_NAME);
        if (connect(sock_fd, (struct sockaddr *)&addr, addr_len) == -1) {
            perror("connect");
            close(sock_fd);
            exit(EXIT_FAILURE);
        }
    
        const char *msg = "Hello from client!";
        write(sock_fd, msg, strlen(msg));
    
        ssize_t n = read(sock_fd, buffer, BUFFER_SIZE - 1);
        if (n > 0) {
            buffer[n] = '\0';
            printf("Server response: %s\n", buffer);
        }
    
        close(sock_fd);
        return 0;
    }
    

    3. How to Compile and Run

    gcc -o server server.c
    gcc -o client client.c
    
    # Run server in one terminal
    ./server
    
    # Run client in another terminal
    ./client
    

    4. Important Notes

    • The abstract socket name starts with a null byte (\0), which is not printable. When printing, skip the first byte.
    • The socket does not create a file in the filesystem, so no cleanup is needed.
    • The length passed to bind() and connect() must include the null byte and the rest of the name.
    • Abstract sockets are Linux-specific and won't work on other Unix-like OSes.

    If you want, I can also provide examples in Python or other languages!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.