Unix Domain Socket C Program
Neelkanth_221$ cat unix_dom_client.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
//#define CLIENT_SOCK_FILE "client.sock"
//#define SERVER_SOCK_FILE "server.sock"
#define CLIENT_SOCK_FILE "/var/run/process_restart_stats/client.sock"
#define SERVER_SOCK_FILE "/var/run/process_restart_stats/server.sock"
//#define STAT_FILE_PATH "."
#define STAT_FILE_PATH "/data/system"
char client_text[8192] = {'\0'};
#if 0
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);
/* here, secs is time since epoch */
printf("client: current Time: %ld.%ld\n", tv.tv_sec, tv.tv_usec);
return;
}
#endif
void get_current_time(void)
{
/* get current system time*/
struct timeval tv;
gettimeofday(&tv, NULL);
/* here, secs is time since epoch */
printf("client: current Time: %ld.%ld\n", tv.tv_sec, tv.tv_usec);
return;
}
/************************************************************************************
Arguments for main function:
1st arg : string
2nd arg : 'bl' or 'nb'
3rd arg : 'yes' or 'no' ['sleep or not' after send mode]
***********************************************************************************/
int main(int argc, char *argv[]) {
int fd;
struct sockaddr_un addr;
int ret;
char buff[8192];
struct sockaddr_un from;
int ok = 1;
int len;
/* for non-blocking, if sleep needs to be introduced after every send,
* set this flag to 1
*/
int sleep_mode_flag = 0;
/*********************** TIME CODE *****************************************/
double time_1, time_50000;
struct timeval tv;
char save_time[20] = {'\0'};
/****************************************************************************/
if ((argc != 4) || (argc < 4) || (argc > 4))
{
printf("\n Enter a string and enter blocking/non-blocking mode ('bl' or 'nb') "
"and Enter usleep mode ('yes' or 'no') \n");
return 0;
}
if (! ((!strcmp(argv[2], "bl")) || (!strcmp(argv[2], "nb"))) )
{
printf("Invalid mode.. enter 'bl' or 'nb'\n");
return 0;
}
if (! ((!strcmp(argv[3], "yes")) || (!strcmp(argv[3], "no"))) )
{
printf("Invalid mode.. enter sleep mode: 'yes' or 'no'\n");
return 0;
}
if (strcmp(argv[3], "yes") == 0) {
sleep_mode_flag = 1;
}
char file_name[100] = {0};
snprintf(file_name, sizeof(file_name)-1, "%s/udp_client_%s_%s_%ld",
STAT_FILE_PATH, argv[2], argv[3],strlen(argv[1])*2);
/*********************** TIME CODE *****************************************/
/* Open file saving timestamps in each iteration */
FILE *fp = NULL;
fp = fopen(file_name, "a+");
/****************************************************************************/
strncpy(client_text, argv[1], sizeof(client_text)-1);
strncat(client_text, argv[1], sizeof(client_text)-1);
if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
perror("socket");
ok = 0;
}
/* Set the client to non blocking based on input argument */
if (strcmp(argv[2], "nb") == 0) {
fcntl(fd, F_SETFL, O_NONBLOCK);
}
if (ok) {
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, CLIENT_SOCK_FILE);
unlink(CLIENT_SOCK_FILE);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
ok = 0;
}
}
if (ok) {
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SERVER_SOCK_FILE);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("connect");
ok = 0;
}
}
int i;
printf("*******************************************************************\n");
printf("length of the packet: [%ld]\n", strlen(client_text));
printf ("client: Time stamp 'before' the 1st packet is sent from client\n");
// get_current_time();
/*********************** TIME CODE *****************************************/
/* get current time */
memset(&tv, '\0', sizeof(struct timeval));
memset(save_time, '\0', sizeof(save_time));
gettimeofday(&tv, NULL);
time_1 = (double)tv.tv_sec + ((double)tv.tv_usec/1000000);
/* here, secs is time since epoch */
snprintf(save_time, sizeof(save_time) -1, "%lf ", time_1);
/* save this timestamp in file */
fprintf(fp, "%s", save_time);
printf("client: current Time: %s\n", save_time);
/****************************************************************************/
printf("*******************************************************************\n");
int actual_drops = 0;
if (ok) {
for (i = 0; i < 50000; i++)
{
if (send(fd, client_text, strlen(client_text)+1, 0) == -1) {
//perror("send");
actual_drops ++;
ok = 0;
}
/* only when sleep mode is 'yes', do usleep after every msgsnd */
if (sleep_mode_flag == 1) {
usleep(1);
}
}
printf("*******************************************************************\n");
printf ("client: Time stamp when the 50000th packet is sent from client\n");
//get_current_time();
/*********************** TIME CODE *****************************************/
/* get current time */
memset(&tv, '\0', sizeof(struct timeval));
memset(save_time, '\0', sizeof(save_time));
gettimeofday(&tv, NULL);
time_50000 = (double)tv.tv_sec + ((double)tv.tv_usec/1000000);
/* here, secs is time since epoch */
snprintf(save_time, sizeof(save_time) -1, "%lf ", time_50000);
/* save this timestamp in file */
fprintf(fp, "%s", save_time);
printf("client: current Time: %s\n", save_time);
/****************************************************************************/
printf("*******************************************************************\n");
printf("Packet Count : %d\n", i + 1);
}
/*********************** actual drops CODE *************************************/
memset(save_time, '\0', sizeof(save_time));
/* save actual drops in file */
snprintf(save_time, sizeof(save_time) -1, "%d", actual_drops);
fprintf(fp, "%s ", save_time);
printf("No. of actual drops : %d\n", actual_drops);
// < packet1_timestamp , packet50000_timestamp, actual_drops >
/******************** Save time difference in file (time_50000 - time_1)**************/
double time_diff;
time_diff = time_50000 - time_1;
memset(save_time, '\0', sizeof(save_time));
/* save actual drops in file */
snprintf(save_time, sizeof(save_time) -1, "%lf", time_diff);
fprintf(fp, "%s\n", save_time);
printf("Time difference time_50000 - time_1 : %lf\n", time_diff);
/*********************************************************************************/
printf ("Client: bye\n");
printf ("\n\n\n");
if (fd >= 0) {
close(fd);
}
fclose(fp);
unlink (CLIENT_SOCK_FILE);
return 0;
}
Neelkanth_221$ cat unix_dom_server.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
//#define CLIENT_SOCK_FILE "client.sock"
//#define SERVER_SOCK_FILE "server.sock"
#define CLIENT_SOCK_FILE "/var/run/process_restart_stats/client.sock"
#define SERVER_SOCK_FILE "/var/run/process_restart_stats/server.sock"
#if 0
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;
}
#endif
void get_current_time(void)
{
/* get current system time*/
struct timeval tv;
gettimeofday(&tv, NULL);
/* here, secs is time since epoch */
printf("client: current Time: %ld.%ld\n", tv.tv_sec, tv.tv_usec);
return;
}
int main() {
int fd;
struct sockaddr_un addr;
int ret;
char buff[8192];
struct sockaddr_un from;
int ok = 1;
int len;
socklen_t fromlen = sizeof(from);
if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
perror("socket");
ok = 0;
}
if (ok) {
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SERVER_SOCK_FILE);
unlink(SERVER_SOCK_FILE);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
ok = 0;
}
}
int count = 0;
while ((len = recvfrom(fd, buff, 8192, 0, (struct sockaddr *)&from, &fromlen)) > 0)
{
#if 0
if ((count == 0) || (count == 49999)) {
printf("*******************************************************************************\n");
printf("packet count :%d\n", count + 1);
printf ("server: recvfrom msg from client: [%s] \nlength "
"of string received: [%ld]\n", buff, strlen(buff));
get_current_time();
printf("*******************************************************************************\n");
printf("\n\n");
}
#endif
count ++;
if (count == 50000)
{count = 0;}
}
if (fd >= 0) {
close(fd);
}
return 0;
}
Compilation command:
gcc -o mq_server mq_server.c -lm
gcc -o udp_server unix_dom_server.c -lm
gcc -o mq_client mq_client.c -lm
gcc -o udp_client unix_dom_client.c -lm
System V Message Queue C program
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;
};
#if 0
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;
}
#endif
void get_current_time(void)
{
/* get current system time*/
struct timeval tv;
gettimeofday(&tv, NULL);
/* here, secs is time since epoch */
printf("client: current Time: %ld.%ld\n", tv.tv_sec, 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("message queue id : %d\n", qid);
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 0
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");
}
#endif
}
}
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>
//#define STAT_FILE_PATH "."
#define STAT_FILE_PATH "/data/system"
char client_text[8192] = {'\0'};
struct message_text {
int qid;
char buf [8100];
};
struct message {
long message_type;
struct message_text message_text;
};
#if 0
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;
}
#endif
void get_current_time(void)
{
/* get current system time*/
struct timeval tv;
gettimeofday(&tv, NULL);
/* here, secs is time since epoch */
printf("client: current Time: %ld.%ld\n", tv.tv_sec, tv.tv_usec);
return;
}
/**************************************************************************
./mqclient <string> <bl/nb> <yes/no>
*************************************************************************/
int main (int argc, char **argv)
{
key_t server_queue_key;
int server_qid, myqid;
struct message my_message, return_message;
int actual_packet_drop = 0;
/* for non-blocking, if sleep needs to be introduced after every send,
* set this flag to 1
*/
int sleep_mode_flag = 0;
/* if mode is 'nb', set this flag to 1 */
int non_blocking_mode_flag = 0;
/*********************** TIME CODE *****************************************/
struct timeval tv;
char save_time[20] = {'\0'};
/****************************************************************************/
/*****************************************************************************
VALIDATION CHECKS.. COMMON FOR BOTH MESSAGE QUEUES AND UNIX DOMAIN SOCKETS
*****************************************************************************/
if ((argc != 4) || (argc < 4) || (argc > 4))
{
printf("\n Enter a string , Enter blocking/non-blocking mode ('bl' or 'nb')"
" and Enter usleep mode ('yes' or 'no') \n");
return 0;
}
if (! ((!strcmp(argv[2], "bl")) || (!strcmp(argv[2], "nb"))) )
{
printf("Invalid mode.. enter blocking/non-blocking mode: 'bl' or 'nb'\n");
return 0;
}
if (! ((!strcmp(argv[3], "yes")) || (!strcmp(argv[3], "no"))) )
{
printf("Invalid mode.. enter sleep mode: 'yes' or 'no'\n");
return 0;
}
if (strcmp(argv[2], "nb") == 0) {
non_blocking_mode_flag = 1;
}
if (strcmp(argv[3], "yes") == 0) {
sleep_mode_flag = 1;
}
/****************************************************************************/
/************************ File to store statistics **************************/
char file_name[100] = {0};
snprintf(file_name, sizeof(file_name)-1, "%s/mq_client_%s_%s_%ld", STAT_FILE_PATH,
argv[2], argv[3], strlen(argv[1])*2);
/****************************************************************************/
/*********************** TIME CODE *****************************************/
double time_1, time_50000;
/* Open file saving timestamps in each iteration */
FILE *fp = NULL;
fp = fopen(file_name, "a+");
/****************************************************************************/
strncpy(client_text, argv[1], sizeof(client_text));
strncat(client_text, argv[1], sizeof(client_text)-1);
// 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();
/*********************** TIME CODE *****************************************/
/* get current time */
memset(&tv, '\0', sizeof(struct timeval));
memset(save_time, '\0', sizeof(save_time));
gettimeofday(&tv, NULL);
time_1 = (double)tv.tv_sec + ((double)tv.tv_usec/1000000);
/* here, secs is time since epoch */
snprintf(save_time, sizeof(save_time) -1, "%lf ", time_1);
/* save this timestamp in file */
fprintf(fp, "%s", save_time);
printf("client: current Time: %s\n", save_time);
/****************************************************************************/
printf("########################################################################\n");
int i;
if (non_blocking_mode_flag == 1)
{
for (i = 0; i < 50000; i++)
{
/* non-blocking */
if (msgsnd (server_qid, &my_message, sizeof (struct message_text), IPC_NOWAIT) == -1) {
//perror ("client: msgsnd");
actual_packet_drop ++;
}
/* only when sleep mode is 'yes', do usleep after every msgsnd */
if (sleep_mode_flag == 1) {
usleep(1);
}
}
} else {
for (i = 0; i < 50000; i++)
{
/* blocking */
if (msgsnd (server_qid, &my_message, sizeof (struct message_text), 0) == -1) {
actual_packet_drop ++;
}
}
}
printf("########################################################################\n");
printf("client: time stamp just 'after' sending 50000th message to server \n");
printf("packet count : %d\n", i+1);
//get_current_time();
/*********************** TIME CODE *****************************************/
/* get current time */
memset(&tv, '\0', sizeof(struct timeval));
memset(save_time, '\0', sizeof(save_time));
gettimeofday(&tv, NULL);
time_50000 = (double)tv.tv_sec + ((double)tv.tv_usec/1000000);
/* here, secs is time since epoch */
snprintf(save_time, sizeof(save_time) -1, "%lf ", time_50000);
/* save this timestamp in file */
fprintf(fp, "%s", save_time);
printf("client: current Time: %s\n", save_time);
/****************************************************************************/
printf("########################################################################\n");
// remove message queue
if (msgctl (myqid, IPC_RMID, NULL) == -1) {
perror ("client: msgctl");
exit (1);
}
/*********************** actual drops CODE *************************************/
memset(save_time, '\0', sizeof(save_time));
/* save actual drops in file */
snprintf(save_time, sizeof(save_time) -1, "%d ", actual_packet_drop);
fprintf(fp, "%s", save_time);
printf("actual packet drop : %d\n", actual_packet_drop);
// < packet1_timestamp , packet50000_timestamp, actual_drops >
/****************************************************************************/
/******************** Save time difference in file (time_50000 - time_1)**************/
double time_diff;
time_diff = time_50000 - time_1;
memset(save_time, '\0', sizeof(save_time));
/* save actual drops in file */
snprintf(save_time, sizeof(save_time) -1, "%lf", time_diff);
fprintf(fp, "%s\n", save_time);
printf("Time difference time_50000 - time_1 : %lf\n", time_diff);
/****************************************************************************/
printf ("Client: bye\n");
printf ("\n\n\n");
fclose(fp);
exit (0);
}