How to use unix sockets for two programs to communicate with a third one that acts as a…
- User
How to use unix sockets for two programs to communicate with a third one that acts as a server ?
Assistant
Using Unix sockets for inter-process communication (IPC) allows two programs (clients) to communicate with a third program (server) on the same machine. Unix sockets are efficient and lightweight, making them ideal for local communication. Below is a step-by-step guide on how to implement this:
1. Server Program
The server creates a Unix socket, binds it to a file path, listens for incoming connections, and handles client requests.
#include <unistd.h> #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define SOCKET_PATH "/tmp/my_unix_socket" #define BUFFER_SIZE 1024 int main() { int server_fd, client_fd; struct sockaddr_un server_addr, client_addr; socklen_t client_len = sizeof(client_addr); char buffer[BUFFER_SIZE]; // Create a Unix socket server_fd = socket(AF_UNIX, SOCK_STREAM, 0); if (server_fd == -1) { perror("socket"); exit(EXIT_FAILURE); } // Set up the server address memset(&server_addr, 0, sizeof(server_addr)); server_addr.sun_family = AF_UNIX; strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1); unlink(SOCKET_PATH); // Remove existing socket file // Bind the socket to the address if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { perror("bind"); close(server_fd); exit(EXIT_FAILURE); } // Listen for incoming connections if (listen(server_fd, 5) == -1) { perror("listen"); close(server_fd); exit(EXIT_FAILURE); } printf("Server listening on %s...\n", SOCKET_PATH); // Accept a client connection client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len); if (client_fd == -1) { perror("accept"); close(server_fd); exit(EXIT_FAILURE); } // Communicate with the client while (1) { memset(buffer, 0, BUFFER_SIZE); ssize_t bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0); if (bytes_received <= 0) { break; // Client disconnected or error } printf("Received: %s", buffer); // Echo back the message to the client send(client_fd, buffer, bytes_received, 0); } // Clean up close(client_fd); close(server_fd); unlink(SOCKET_PATH); return 0; }
2. Client Program
The client connects to the server's Unix socket and sends/receives data.
#include <unistd.h> #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define SOCKET_PATH "/tmp/my_unix_socket" #define BUFFER_SIZE 1024 int main() { int client_fd; struct sockaddr_un server_addr; char buffer[BUFFER_SIZE]; // Create a Unix socket client_fd = socket(AF_UNIX, SOCK_STREAM, 0); if (client_fd == -1) { perror("socket"); exit(EXIT_FAILURE); } // Set up the server address memset(&server_addr, 0, sizeof(server_addr)); server_addr.sun_family = AF_UNIX; strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1); // Connect to the server if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { perror("connect"); close(client_fd); exit(EXIT_FAILURE); } // Send a message to the server const char *message = "Hello, Server!"; send(client_fd, message, strlen(message), 0); printf("Sent: %s\n", message); // Receive a response from the server memset(buffer, 0, BUFFER_SIZE); ssize_t bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0); if (bytes_received > 0) { printf("Received: %s\n", buffer); } // Clean up close(client_fd); return 0; }
3. Compilation and Execution
-
Compile the server and client programs:
gcc -o server server.c gcc -o client client.c -
Run the server:
./server -
Run one or more clients in separate terminals:
./client
Key Points
- Socket File Path: The server binds to a file path (e.g.,
/tmp/my_unix_socket), which clients use to connect. - Communication: Data is sent and received using
send()andrecv(). - Cleanup: The server should remove the socket file (
unlink()) when it terminates. - Multiple Clients: The server can handle multiple clients by using
fork()or threading afteraccept().
This setup enables efficient local communication between processes using Unix sockets.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.