Of course. Here is a detailed example using abstract Unix sockets.
What are Abstract Unix Sockets?
Abstract Unix sockets are a Linux-specific feature. Unlike traditional Unix sockets that are tied to a file in the filesystem (e.g., /tmp/my_socket), abstract sockets exist in a special, non-filesystem namespace.
Key Advantages:
- No Filesystem Entry: They don't create a file, which means you don't have to worry about filesystem permissions or cleaning up stale socket files with
unlink().
- Automatic Cleanup: The abstract name is automatically removed when the last reference to the socket is closed.
- No Path Length Limit Issues: They are not constrained by the
PATH_MAX limit of the filesystem.
Important Clarification: You still need to call bind() on the server socket. The "abstract" part refers to where it's bound (an abstract namespace), not the act of binding itself. However, it solves the common problems associated with filesystem-based sockets.
How It Works
To create an abstract socket, you set the first byte of the sun_path member in the struct sockaddr_un to a null byte (\0). The rest of the sun_path buffer is used as the "name" of the socket.
This example will use datagram sockets (SOCK_DGRAM) because they are simpler for a multi-client scenario without requiring fork() or threading. The server receives a message from a client and echoes it back.
1. Server Program (server.c)
The server creates an abstract socket, binds to a unique abstract name, and then enters a loop to receive messages and send back replies.
// server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
// The name for our abstract socket
#define ABSTRACT_SOCKET_NAME "my_cool_server_socket"
#define BUFFER_SIZE 1024
int main() {
int server_fd;
struct sockaddr_un server_addr, client_addr;
socklen_t client_len;
char buffer[BUFFER_SIZE];
// 1. Create a Unix datagram socket
server_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (server_fd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 2. Set up the abstract socket address
memset(&server_addr, 0, sizeof(struct sockaddr_un));
server_addr.sun_family = AF_UNIX;
// The first byte of sun_path must be a null byte for an abstract socket
server_addr.sun_path[0] = '\0';
strncpy(&server_addr.sun_path[1], ABSTRACT_SOCKET_NAME, sizeof(server_addr.sun_path) - 2);
// Calculate the address length
// For abstract sockets, len is sizeof(sun_family) + strlen(name) + 1 (for the leading null byte)
socklen_t addr_len = sizeof(server_addr.sun_family) + strlen(ABSTRACT_SOCKET_NAME) + 1;
// 3. Bind the socket to the abstract name
// No unlink() is needed because there is no file!
if (bind(server_fd, (struct sockaddr *)&server_addr, addr_len) == -1) {
perror("bind");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening on abstract socket '%s'...\n", ABSTRACT_SOCKET_NAME);
// 4. Main loop to receive and reply to messages
while (1) {
client_len = sizeof(struct sockaddr_un);
memset(buffer, 0, BUFFER_SIZE);
// Receive a message from any client
ssize_t bytes_received = recvfrom(server_fd, buffer, BUFFER_SIZE - 1, 0,
(struct sockaddr *)&client_addr, &client_len);
if (bytes_received == -1) {
perror("recvfrom");
continue;
}
// Note: client_addr.sun_path will contain the client's abstract address
printf("Received %ld bytes from a client: %s\n", bytes_received, buffer);
// Echo the message back to the client that sent it
if (sendto(server_fd, buffer, bytes_received, 0,
(struct sockaddr *)&client_addr, client_len) == -1) {
perror("sendto");
}
}
// 5. Cleanup (though this code is unreachable in the loop)
close(server_fd);
return 0;
}
2. Client Program (client.c)
The client also creates its own abstract socket to receive the reply. To ensure multiple clients can run without conflict, we will make each client's abstract socket name unique by appending its process ID.
// client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
// The name of the server's abstract socket
#define ABSTRACT_SOCKET_NAME "my_cool_server_socket"
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int client_fd;
struct sockaddr_un server_addr, client_addr;
char buffer[BUFFER_SIZE];
if (argc < 2) {
fprintf(stderr, "Usage: %s <message>\n", argv[0]);
exit(EXIT_FAILURE);
}
const char *message = argv[1];
// 1. Create a Unix datagram socket
client_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (client_fd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 2. Bind the client socket to its own unique abstract address
// This is necessary so the server knows where to send the reply.
memset(&client_addr, 0, sizeof(struct sockaddr_un));
client_addr.sun_family = AF_UNIX;
client_addr.sun_path[0] = '\0';
// Use process ID to make the client's address unique
snprintf(&client_addr.sun_path[1], sizeof(client_addr.sun_path) - 1, "client.%d", getpid());
socklen_t client_addr_len = sizeof(client_addr.sun_family) + strlen(&client_addr.sun_path[1]) + 1;
if (bind(client_fd, (struct sockaddr *)&client_addr, client_addr_len) == -1) {
perror("bind client");
close(client_fd);
exit(EXIT_FAILURE);
}
// 3. Set up the server's abstract socket address
memset(&server_addr, 0, sizeof(struct sockaddr_un));
server_addr.sun_family = AF_UNIX;
server_addr.sun_path[0] = '\0';
strncpy(&server_addr.sun_path[1], ABSTRACT_SOCKET_NAME, sizeof(server_addr.sun_path) - 2);
socklen_t server_addr_len = sizeof(server_addr.sun_family) + strlen(ABSTRACT_SOCKET_NAME) + 1;
// 4. Send the message to the server
if (sendto(client_fd, message, strlen(message), 0,
(struct sockaddr *)&server_addr, server_addr_len) == -1) {
perror("sendto");
close(client_fd);
exit(EXIT_FAILURE);
}
printf("Sent to server: '%s'\n", message);
// 5. Wait for the reply from the server
memset(buffer, 0, BUFFER_SIZE);
ssize_t bytes_received = recvfrom(client_fd, buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
if (bytes_received == -1) {
perror("recvfrom");
} else {
printf("Received from server: '%s'\n", buffer);
}
// 6. Cleanup
close(client_fd);
return 0;
}
3. Compilation and Execution
-
Compile the programs:
gcc -o server server.c
gcc -o client client.c
-
Run the server in one terminal:
./server
# Output:
# Server listening on abstract socket 'my_cool_server_socket'...
-
Run the first client in a second terminal:
./client "Hello from client 1"
# Output:
# Sent to server: 'Hello from client 1'
# Received from server: 'Hello from client 1'
In the server's terminal, you will see:
Received 20 bytes from a client: Hello from client 1
-
Run the second client in a third terminal:
./client "A different message"
# Output:
# Sent to server: 'A different message'
# Received from server: 'A different message'
In the server's terminal, you will see:
Received 19 bytes from a client: A different message
This setup successfully demonstrates how two (or more) clients can communicate with a single server using abstract Unix sockets, which are clean, efficient, and avoid filesystem clutter.