Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <fcntl.h>
  10. struct message
  11. {
  12. long type;
  13. char text[500];
  14. };
  15. int main()
  16. {
  17. struct message msg;
  18. struct message buff;
  19. int msgidUp,msgidDown,len;
  20. //struct msqid_ds buf;
  21. key_t keyUp=152;
  22. key_t keyDown=239;
  23. msgidUp=msgget(keyUp,IPC_CREAT|0666);
  24. msgidDown=msgget(keyDown,IPC_CREAT|0666);
  25. char chatid[100];
  26. printf("Please enter the chat id : ");
  27. scanf("%s",chatid);
  28. strcpy(msg.text,"NEW ");
  29. strcat(msg.text,chatid);
  30. msg.type=1;
  31. len=strlen(msg.text);
  32. msgsnd(msgidUp,&msg,len,0);
  33. /*{
  34. perror("msgop: msgsnd failed");
  35. exit(1);
  36. }*/
  37. printf("%s\n",msg.text );
  38. if(msgrcv(msgidDown,&buff,500,getpid(),0)==-1)
  39. {
  40. perror("msgop: msgrcv failed");
  41. exit(1);
  42. }
  43. char clients[20][100];
  44. char clientid[100];
  45. while(1)
  46. {
  47. char text[400];
  48. int i=0;
  49. char *pch;
  50. pch=(char *)malloc(100);
  51. char h[500];
  52. strcpy(h,buff.text);
  53. pch=strtok(buff.text," ");
  54. while(pch!=NULL)
  55. {
  56. strcpy(clients[i],pch);
  57. pch=strtok(NULL," ");
  58. i++;
  59. }
  60. strcpy(buff.text,h);
  61. printf("The list of available clients are:\n");
  62. int j;
  63. for(j=0;j<i-1;j++)
  64. {
  65. printf("%d) %s\n",j+1,clients[j+1]);
  66. }
  67. printf("Pick a client to send a message : \n");
  68. scanf("%s",clientid);
  69. printf("Enter your message : \n");
  70. scanf("%s",text);
  71. strcpy(msg.text,"MSG ");
  72. strcat(msg.text,text);
  73. strcat(msg.text," ");
  74. strcat(msg.text,clientid);
  75. msg.type=1;
  76. len=strlen(msg.text);
  77. if(msgsnd(msgidUp,&msg,len+1,0)==-1)
  78. {
  79. perror("msgop: msgsnd failed");
  80. exit(1);
  81. }
  82. struct message buffer;
  83. if(msgrcv(msgidDown,&buffer,500,getpid(),0)==-1)
  84. {
  85. perror("msgop: msgrcv failed");
  86. exit(1);
  87. }
  88. if(buffer.text[0]=='M')
  89. {
  90. printf("%s\n", buffer.text);
  91. pch=strtok(buffer.text," ");
  92. pch=strtok(NULL," ");
  93. printf("Message : %s\n",pch);
  94. pch=strtok(NULL," ");
  95. pch=strtok(NULL," ");
  96. printf("Message time : %s\n",pch);
  97. pch=strtok(NULL," ");
  98. printf("Sender : %s\n",pch);
  99. }
  100. else
  101. {
  102. buff=buffer;
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement