--sas

server-client-killer

Nov 16th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. // message-types.h
  2.  
  3. #pragma once
  4.  
  5. typedef struct message_from_client {
  6.     long mtype;
  7.     struct info_from_client {
  8.         float a;
  9.         int b;
  10.         long pid;
  11.     } info;
  12. } message_from_client;
  13.  
  14. typedef struct message_from_server {
  15.     long mtype;
  16.     struct info_from_server {
  17.         float c;
  18.     } info;
  19. } message_from_server;
  20.  
  21.  
  22.  
  23. // server.c
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29.  
  30. #include <sys/ipc.h>
  31. #include <sys/msg.h>
  32. #include <sys/stat.h>
  33. #include <errno.h>
  34.  
  35. #include "message-types.h"
  36.  
  37. int main()
  38. {
  39.     int msqid;
  40.     char pathname[] = "server.c";
  41.     key_t key = ftok(pathname, 0);
  42.  
  43.     (void) umask(0000);
  44.     if ((msqid = msgget(key, 0666 | IPC_CREAT | IPC_EXCL)) < 0) {
  45.         if (errno == EEXIST) { printf("Error1s\n"); exit(-1); }
  46.         else { printf("Error2s\n"); exit(-1); }
  47.     }
  48.  
  49.     message_from_server snd;
  50.     message_from_client rcv;
  51.     int maxlen_c = sizeof(struct info_from_client);
  52.     int maxlen_s = sizeof(struct info_from_server);
  53.  
  54.     while (1) {
  55.         if (msgrcv(msqid, (message_from_client*) &rcv, maxlen_c, 1, 0) < 0) { printf("Error3s\n"); exit(-1); }
  56.         printf("Server received from %ld, contents: a = %f, b = %d\n", rcv.info.pid, rcv.info.a, rcv.info.b);
  57.  
  58.         if (rcv.info.pid < 0)
  59.             break;
  60.  
  61.         snd.mtype = rcv.info.pid;
  62.         snd.info.c = (float) rcv.info.a * rcv.info.b;
  63.  
  64.         if (msgsnd(msqid, (message_from_server*) &snd, maxlen_s, 0) < 0) { printf("Error4s\n"); exit(-1); }
  65.         printf("Server sent to %ld, result: c = %f\n", snd.mtype, snd.info.c);
  66.     }
  67.  
  68.     if (msgctl(msqid, IPC_RMID, (struct msqid_ds*) NULL) < 0) { printf("Error5s\n"); exit(-1); }
  69.  
  70.     return 0;
  71. }
  72.  
  73.  
  74.  
  75. // client.c
  76.  
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <sys/types.h>
  80. #include <unistd.h>
  81.  
  82. #include <sys/ipc.h>
  83. #include <sys/msg.h>
  84. #include <time.h>
  85.  
  86. #include "message-types.h"
  87.  
  88. int main() {
  89.     char pathname[] = "server.c";
  90.     key_t key = ftok(pathname, 0);
  91.  
  92.     int msqid;
  93.     if ((msqid = msgget(key, 0)) < 0) { printf("Error1c\n"); exit(-1); }
  94.  
  95.     message_from_client snd;
  96.  
  97.     snd.mtype = 1;
  98.     srand(time(0));
  99.     snd.info.b = rand() % 512;
  100.     snd.info.a = (float) rand() / RAND_MAX;
  101.     snd.info.pid = (long) getpid();
  102.  
  103.     if (msgsnd(msqid, (message_from_client*) &snd, sizeof (struct info_from_client), 0) < 0) { printf("Error2c\n"); exit(-1); }
  104.     printf ("Client %ld sent numbers a = %f, b = %d to the server\n", snd.info.pid, snd.info.a, snd.info.b);
  105.  
  106.     message_from_server rcv;
  107.     if (msgrcv(msqid, (message_from_server*) &rcv, sizeof (struct info_from_server), (int) getpid(), 0) < 0) { printf("Error3c\n"); exit(-1); }
  108.     printf ("Client %d received from the server, type: %ld, result: c = %f\n", (int) getpid(), rcv.mtype, rcv.info.c);
  109.  
  110.     return 0;
  111. }
  112.  
  113.  
  114.  
  115. // killer.c
  116.  
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <sys/types.h>
  120. #include <unistd.h>
  121.  
  122. #include <sys/ipc.h>
  123. #include <sys/msg.h>
  124.  
  125. #include "message-types.h"
  126.  
  127. int main() {
  128.     char pathname[] = "server.c";
  129.     key_t key = ftok(pathname, 0);
  130.  
  131.     int msqid;
  132.     if ((msqid = msgget(key, 0)) < 0) { printf("Error1k\n"); exit(-1); }
  133.  
  134.     message_from_client snd;
  135.  
  136.     snd.mtype = 1;
  137.     snd.info.pid = -1;
  138.  
  139.     if (msgsnd(msqid, (message_from_client*) &snd, sizeof (struct info_from_client), 0) < 0) { printf("Error2c\n"); exit(-1); }
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment