C program to implement message queue in linux

C program to implement message queue in linux


Neelkanth_221$ cat mq_server.c

/*
 * server.c: Server program
 *           to demonstrate interprocess commnuication
 *           with System V message queues
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include <sys/time.h>
#include <time.h>
#include <math.h>


#define SERVER_KEY_PATHNAME "mqueue_server_key"
#define PROJECT_ID 'M'
#define QUEUE_PERMISSIONS 0660

struct message_text {
    int qid;
    char buf [8100];
};

struct message {
    long message_type;
    struct message_text message_text;
};

void get_current_time(void)
{
    /* get current system time*/
    char buffer[26];
    struct tm* tm_info;
    struct timeval tv;

    gettimeofday(&tv, NULL);
    tm_info = localtime(&tv.tv_sec);

    strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);
    printf("server: current Time: %s.%ld\n", buffer, tv.tv_usec);

    return;
}


int main (int argc, char **argv)
{
    key_t msg_queue_key;
    int qid;
    struct message message;


    if ((msg_queue_key = ftok (SERVER_KEY_PATHNAME, PROJECT_ID)) == -1) {
        perror ("ftok");
        exit (1);
    }

    if ((qid = msgget (msg_queue_key, IPC_CREAT | QUEUE_PERMISSIONS)) == -1) {
        perror ("msgget");
        exit (1);
    }

    printf ("Server: Hello, World!\n");

    int count = 0;
    while (1) {
        // read an incoming message
        if (msgrcv (qid, &message, sizeof (struct message_text), 0, 0) == -1) {
            perror ("msgrcv");
            exit (1);
        }
        count ++;

        if(count == 1 || count == 50000)
        {
            printf("#####################################################################\n");
            printf("packet count  : %d\n", count);
            printf ("Server: message received from client:\n [%s]  message length: %ld\n",
                    message.message_text.buf, strlen(message.message_text.buf));
            get_current_time();
            printf("#####################################################################\n");
            if (count == 50000) {
                count = 0;
            }

            printf("\n\n\n");
        }


    }
}

Neelkanth_221$ cat mq_client.c

/*
 * client.c: Client program
 *           to demonstrate interprocess commnuication
 *           with System V message queues
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define SERVER_KEY_PATHNAME "mqueue_server_key"
#define PROJECT_ID 'M'

#include <sys/time.h>
#include <time.h>
#include <math.h>


char client_text[8192] = {'\0'};


struct message_text {
    int qid;
    char buf [8100];
};

struct message {
    long message_type;
    struct message_text message_text;
};


void get_current_time(void)
{
    /* get current system time*/
    char buffer[26];
    struct tm* tm_info;
    struct timeval tv;

    gettimeofday(&tv, NULL);
    tm_info = localtime(&tv.tv_sec);

    strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);
    printf("client: current Time: %s.%ld\n", buffer, tv.tv_usec);

    return;
}

int main (int argc, char **argv)
{
    key_t server_queue_key;
    int server_qid, myqid;
    struct message my_message, return_message;

    if (argc != 2)
    {
        printf("\n Enter a string \n");
        return 0;
    }

    strncpy(client_text, argv[1], sizeof(client_text));

    // create my client queue for receiving messages from server
    if ((myqid = msgget (IPC_PRIVATE, 0660)) == -1) {
        perror ("msgget: myqid");
        exit (1);
    }

    if ((server_queue_key = ftok (SERVER_KEY_PATHNAME, PROJECT_ID)) == -1) {
        perror ("ftok");
        exit (1);
    }

    if ((server_qid = msgget (server_queue_key, 0)) == -1) {
        perror ("msgget: server_qid");
        exit (1);
    }

    my_message.message_type = 1;
    my_message.message_text.qid = myqid;

    printf("########################################################################\n");
    printf("client: send  message to server : [%s] message length: [%ld] \n",client_text,
                              strlen(client_text));
    printf("########################################################################\n");


    strcpy(my_message.message_text.buf, client_text);

    // remove newline from string
    int length = strlen (my_message.message_text.buf);
    if (my_message.message_text.buf [length - 1] == '\n')
        my_message.message_text.buf [length - 1] = '\0';

    printf("########################################################################\n");
    printf("client: time stamp just 'Before' sending the message to server \n");
    get_current_time();
    printf("########################################################################\n");

    int i;
    for (i = 0; i < 50000; i++)
    {
        // send message to server
        if (msgsnd (server_qid, &my_message, sizeof (struct message_text), 0) == -1) {
            perror ("client: msgsnd");
            exit (1);
        }
        if ((i == 0) || (i == 49999))
        {
            printf("########################################################################\n");
            printf("client: time stamp just 'after' sending the message to server \n");
            printf("packet count : %d\n", i+1);
            get_current_time();
            printf("########################################################################\n");
        }
    }

    // remove message queue
    if (msgctl (myqid, IPC_RMID, NULL) == -1) {
        perror ("client: msgctl");
        exit (1);
    }
    printf ("Client: bye\n");
    printf ("\n\n\n");

    exit (0);
}

Output:
Neelkanth_221$ gcc -o server mq_server.c -lm
Neelkanth_221$ gcc -o client mq_client.c -lm

Neelkanth_221$ ./server &
[1] 32263
Neelkanth_221$ Server: Hello, World!

Neelkanth_221$ ./client asldfhalsdjkfhaslfhasldfhl
########################################################################
client: send  message to server : [asldfhalsdjkfhaslfhasldfhl] message length: [26]
########################################################################
########################################################################
client: time stamp just 'Before' sending the message to server
client: current Time: 2018:02:26 19:43:27.596650
########################################################################
#####################################################################
packet count  : 1
Server: message received from client:
 [asldfhalsdjkfhaslfhasldfhl]  message length: 26
server: current Time: 2018:02:26 19:43:27.596763
#####################################################################

########################################################################
client: time stamp just 'after' sending the message to server
packet count : 1
client: current Time: 2018:02:26 19:43:27.597185
########################################################################
########################################################################
client: time stamp just 'after' sending the message to server
packet count : 50000
client: current Time: 2018:02:26 19:43:27.817492
########################################################################
Client: bye

#####################################################################
packet count  : 50000
Server: message received from client:
 [asldfhalsdjkfhaslfhasldfhl]  message length: 26
server: current Time: 2018:02:26 19:43:27.818828
#####################################################################