Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "header.h"
- #include <signal.h>
- #include <sys/msg.h>
- #include <sys/ipc.h>
- #include <errno.h>
- int client_queue = -1;
- int server_queue = -1;
- int send_message(Message m) {
- int status = msgsnd(server_queue, &m, SIZE_OF_MESSAGE_STRUCTURE, 0);
- if (status == -1)
- printf("Do server_queue: %d nie udalo wyslac sie wiadomosci. %s\n", server_queue, strerror(errno));
- else
- printf("Wyslano wiadomosc do server_queue: %d\n", server_queue);
- return status;
- }
- int receiveReply() {
- Message m;
- if (msgrcv(client_queue, &m, SIZE_OF_MESSAGE_STRUCTURE, 0, 0) == -1) {
- printf("Some problem with receiving reply!\n");
- exit(1);
- }
- printf("Server reply positive with message: %s\n", m.mes);
- }
- void sigint_handler(int sig_num){
- printf("Kleint %d konczy dzialanie.\n", getpid());
- exit(0);
- }
- void client_closing(){
- if(client_queue != -1)
- if( msgctl(client_queue, IPC_RMID, NULL) == -1)
- printf("Wystapil problem z zamykaniem kolejki.\n");
- else
- printf("Kolejka zamknieta poprawnie. Klient %d zakonczyl prace.\n", getpid());
- else
- printf("Klient %d zakonczyl prace. Kolejka nie istanila.\n", getpid());
- }
- int main(int argc, char** argv) {
- atexit(client_closing);
- signal(SIGINT, sigint_handler);
- if ((server_queue = msgget(ftok(getenv("HOME"), 's'), 0)) == -1) {
- printf("Nie udalo otrzymac sie kolejki serwera.\n");
- exit(2);
- }
- key_t client_key = ftok(getenv("HOME"),'s');
- if ((client_queue = msgget(client_key, IPC_CREAT | 0600)) == -1) {
- printf("Nie udalo storzyc sie kolejki. Moje pid: \n", getpid());
- exit(2);
- } else
- printf("Udalo sie polaczyc, czekam na potwierdzenie\n");
- Message m;
- m.pid = getpid();
- m.mtype = INIT_CLIENT;
- sprintf(m.mes, "%d", client_key);
- if(send_message(m) == -1);
- exit(2);
- printf("Udalo polaczyc sie do serwera.\n");
- while (1) {
- printf("Rozpocznij wiadomosc od cyfry definujacej usluge i ' ':\n");
- printf("ECHO - 1\n");
- printf("UPPERCASE - 2\n");
- printf("TIME - 3\n");
- printf("END - 4\n");
- char cbuf[MESSAGE_SIZE];
- fgets(cbuf, MESSAGE_SIZE, stdin);
- int mtype = atoi(strtok(cbuf, " "));
- char *msg = " ";
- if(mtype == 1 || mtype == 2)
- msg = strtok(NULL, " ");
- if(strlen(msg) > MESSAGE_SIZE){
- printf("You mtype too long message. Try again...\n");
- continue;
- }
- m.mtype = mtype;
- sprintf(m.mes, "%s", msg);
- send_message(m);
- if(mtype == 4)
- break;
- receiveReply();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement