Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.h>
- #include <ctype.h>
- #include <fcntl.h>
- struct message
- {
- long type;
- char text[500];
- };
- int main()
- {
- struct message msg;
- struct message buff;
- int msgidUp,msgidDown,len;
- //struct msqid_ds buf;
- key_t keyUp=152;
- key_t keyDown=239;
- msgidUp=msgget(keyUp,IPC_CREAT|0666);
- msgidDown=msgget(keyDown,IPC_CREAT|0666);
- char chatid[100];
- printf("Please enter the chat id : ");
- scanf("%s",chatid);
- strcpy(msg.text,"NEW ");
- strcat(msg.text,chatid);
- msg.type=1;
- len=strlen(msg.text);
- msgsnd(msgidUp,&msg,len,0);
- /*{
- perror("msgop: msgsnd failed");
- exit(1);
- }*/
- printf("%s\n",msg.text );
- if(msgrcv(msgidDown,&buff,500,getpid(),0)==-1)
- {
- perror("msgop: msgrcv failed");
- exit(1);
- }
- char clients[20][100];
- char clientid[100];
- while(1)
- {
- char text[400];
- int i=0;
- char *pch;
- pch=(char *)malloc(100);
- char h[500];
- strcpy(h,buff.text);
- pch=strtok(buff.text," ");
- while(pch!=NULL)
- {
- strcpy(clients[i],pch);
- pch=strtok(NULL," ");
- i++;
- }
- strcpy(buff.text,h);
- printf("The list of available clients are:\n");
- int j;
- for(j=0;j<i-1;j++)
- {
- printf("%d) %s\n",j+1,clients[j+1]);
- }
- printf("Pick a client to send a message : \n");
- scanf("%s",clientid);
- printf("Enter your message : \n");
- scanf("%s",text);
- strcpy(msg.text,"MSG ");
- strcat(msg.text,text);
- strcat(msg.text," ");
- strcat(msg.text,clientid);
- msg.type=1;
- len=strlen(msg.text);
- if(msgsnd(msgidUp,&msg,len+1,0)==-1)
- {
- perror("msgop: msgsnd failed");
- exit(1);
- }
- struct message buffer;
- if(msgrcv(msgidDown,&buffer,500,getpid(),0)==-1)
- {
- perror("msgop: msgrcv failed");
- exit(1);
- }
- if(buffer.text[0]=='M')
- {
- printf("%s\n", buffer.text);
- pch=strtok(buffer.text," ");
- pch=strtok(NULL," ");
- printf("Message : %s\n",pch);
- pch=strtok(NULL," ");
- pch=strtok(NULL," ");
- printf("Message time : %s\n",pch);
- pch=strtok(NULL," ");
- printf("Sender : %s\n",pch);
- }
- else
- {
- buff=buffer;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement