Select System Call API Code Example

To understand the Select System Call API, kindly look at the article given below.

Select System Call

The current code example provides a reader an example usage of the select system call. The Unix Socket is used as the socket mechanism, however, Internet sockets also will work the same. The code is provided below.

Server Code with Select APIClient 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>
#include <sys/select.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;
/* create a list of client connections */
struct sockaddr_un clientaddr[10] = {0};
int sockserver, sockclient[10] = {0};

/* Interrupt_handler so that CTRL +C can be used
* to exit the program */
void interrupt_handler (int signum) {
        int index =0;
        close(sockserver);
        free(servaddr);
        for(index = 0; index < 10; index++) {
                if(sockclient[index] > 0)
                        close(sockclient[index]);
        }
        printf(“socket connection closed\n”);
        exit(0);
}

int main () {

        int length, max_fd, index=0, sel_ready;
        char buffer[50];
        char *string = “Hello from server”;
        fd_set read_fd;

        /* 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, 10)) {
                printf(“Listen Failed\n”);
                goto end1;
        }
        FD_ZERO(&read_fd);
        /* set max_fd */
        max_fd = sockserver + 1;
        while (1) {
                FD_SET(sockserver, &read_fd);
                /* wait on read_fd and time is set as NULL – so wait infinite time */
                sel_ready = select(max_fd, &read_fd, NULL, NULL, NULL);
                if (FD_ISSET(sockserver, &read_fd)) {
                        sockclient[index] = accept(sockserver, (struct sockaddr *)&clientaddr[index], &length);

                        if (sockclient[index] < 0) {
                                printf(“unable to accept client connection\n”);
                                goto end2;
                        }
                }
                /* read one packet from client and write one packet to client */
               memset(buffer, 0, sizeof(buffer));
               read(sockclient[index], buffer, sizeof(buffer));
               printf(“%s\n”, buffer);

               memset(buffer, 0, sizeof(buffer));
               snprintf(buffer, (strlen(string)+1), “%s”, string);
               write(sockclient[index], buffer, sizeof(buffer));
               index++;
        }
end2:
        for(index = 0; index < 10; index++) {
                if(sockclient[index] > 0)
                        close(sockclient[index]);
        }
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;

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

        /* 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 bind to the socket path
        * Remove/unlink any previous socket file creation
        */

        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 != (connect(sockserver, (struct sockaddr *)servaddr, sizeof(struct sockaddr_un)))) {
                printf(“unable to connect to the server\n”);
                goto end1;
        }

        /* write and read one packet from server */
        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;
}

The Poll System Call

Comments

  1. Pingback: The select system call | Hitch Hiker's Guide to Learning

Leave a Reply

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