Unix Domain Stream Socket Example

Unix domain sockets are used as IPC mechanisms between processes in a host system. The Unix domain stream socket example is provided below.

Server CodeClient Code

#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

/* do not create sockets in tmp directory
* To show a socket example – it is fine.
* creating sockets in tmp directory is a security
* vulnerability */

const char *path= “/tmp/unix_socket.sock”;

struct sockaddr_un *servaddr, *cliaddr;
int sockserver, sockclient;

/* Interrupt_handler so that CTRL +C can be used
* to exit the program */
void interrupt_handler (int signum) {
        close(sockserver);
        close(sockclient);
        free(servaddr);
        free(cliaddr);
        printf(“socket connection closed\n”);
        exit(0);
}

int main () {

        int length;
        char buffer[50];
        char *string = “Hello from server”;

        /* signal handler to exit out of while 1 loop */
        signal (SIGINT, interrupt_handler);
        signal (SIGPIPE, interrupt_handler);

        /* Part 1 – create the socket */
        sockserver = socket(AF_UNIX, SOCK_STREAM, 0);

        if (sockserver < 0) {
            printf(“creating server socket failed\n”);
            exit(0);
        }
        /* Part 2 – fill the structure and bind to the socket path
        * Remove/unlink any previous socket file creation
        */

        remove(path);

        servaddr = (struct sockaddr_un *)malloc(sizeof(struct sockaddr_un));
        if (servaddr == NULL) {
            printf(“unable to allocate memory\n”);
            goto end;
        }
        servaddr->sun_family = AF_UNIX;

        if ((strlen(path)) > sizeof(servaddr->sun_path)) {
            printf(“Path is too long\n”);
            goto end1;
        }
        snprintf(servaddr->sun_path, (strlen(path)+1), “%s”, path);

        if (0 != (bind(sockserver,(struct sockaddr *)servaddr, sizeof(struct                                   sockaddr_un)))) {
            printf(“Unable to bind to the socket path\n”);
            goto end1;
        }

        /* Part 3 – make the server socket a passive socket and accept                              connections from client stations */

        if(-1 == listen(sockserver, 1)) {
            printf(“Listen Failed\n”);
            goto end1;
        }
        cliaddr = (struct sockaddr_un *)malloc(sizeof(struct sockaddr_un));
        if (cliaddr == NULL) {
            printf(“unable to allocate memory\n”);
            goto end1;
        }
        printf(“waiting for connection\n”);
        sockclient = accept(sockserver, (struct sockaddr *)cliaddr, &length);

        if (sockclient < 0) {
            printf(“unable to accept client connection\n”);
            goto end2;
        }

        /* read and write data from/to socket */
        while (1) {
            memset(buffer, 0, sizeof(buffer));
            read(sockclient, buffer, sizeof(buffer));
            printf(“%s\n”, buffer);

            memset(buffer, 0, sizeof(buffer));
            snprintf(buffer, (strlen(string)+1), “%s”, string);
            write(sockclient, buffer, sizeof(buffer));
        }
end3:
        if (sockclient >= 0)
            close(sockclient);
end2:
        free(cliaddr);
end1:
        free(servaddr);
end:
        close(sockserver); 
        return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

/* do not create sockets in tmp directory
* To show a socket example – it is fine.
* creating sockets in tmp directory is a security
* vulnerability */

const char *path= “/tmp/unix_socket.sock”;

struct sockaddr_un *servaddr = NULL;
int sockserver;

/* Interrupt_handler so that CTRL +C can be used
* to exit the program */
void interrupt_handler (int signum) {
        close(sockserver);
        free(servaddr);
        printf(“socket connection closed\n”);
        exit(0);
}

int main () {
        int length;
        char buffer[50];
        char *string = “Hello from client”;

        /* signal handler to exit out of while 1 loop */
        signal (SIGINT, interrupt_handler);
        signal (SIGINT, interrupt_handler);

        /* Part 1 – create the socket */
        sockserver = socket(AF_UNIX, SOCK_STREAM, 0);

        if (sockserver < 0) {
            printf(“creating client socket failed\n”);
            exit(0);
        }
        /* Part 2 – fill the structure and connect to the server socket path
         */

        servaddr = (struct sockaddr_un *)malloc(sizeof(struct sockaddr_un));
        if (servaddr == NULL) {
            printf(“unable to allocate memory\n”);
            goto end1;
        }
        servaddr->sun_family = AF_UNIX;

        if ((strlen(path)) > sizeof(servaddr->sun_path)) {
            printf(“Path is too long\n”);
            goto end1;
        }
        snprintf(servaddr->sun_path, (strlen(path)+1), “%s”, path);

        if (0 != (connect(sockserver, (struct sockaddr *)servaddr, sizeof(struct                                sockaddr_un)))) {
            printf(“unable to connect to the server\n”);
            goto end1;
        }

        /* read and write data from/to socket */
        while (1) {
            memset(buffer, 0, sizeof(buffer));
            snprintf(buffer, (strlen(string)+1), “%s”, string);
            write(sockserver, buffer, sizeof(buffer));

            memset(buffer, 0, sizeof(buffer));
            read(sockserver, buffer, sizeof(buffer));
            printf(“%s\n”, buffer);
        }
end1:
        free(servaddr);
end:
        close(sockserver);

        return 0;
}

Analyzing The Unix Domain Stream Socket Code

Comments

  1. Pingback: Understanding the Unix Stream Socket Connection | Hitch Hiker's Guide to Learning

  2. Pingback: Analyzing the Unix Domain Stream Socket Code | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *