Advertisement
Guest User

Untitled

a guest
May 4th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <signal.h>
  10. #include <ncurses.h>
  11. #include <time.h>
  12. #include <sys/shm.h>
  13.  
  14.  
  15. #define PERM 0660
  16. #define KLUCZ_KOM (key_t)1231
  17. #define KLUCZ_MEM (key_t)0x10
  18.  
  19. //serwer zapisuje w pamięci wspołdzielonej informacje o tym co jest na ekranie, klient informacje o samym sobie
  20. //
  21.  
  22. struct m_komunikat {
  23. long typ;
  24. char z1, kol1, x1, y1, z2, kol2, x2, y2;
  25. };
  26. int kom, kon;
  27. int id_mem, ww, i, k=0;
  28. int *adres;
  29.  
  30. void koniec(){
  31. msgctl(kom, IPC_RMID, 0);
  32. curs_set(1);
  33. clear();
  34. refresh();
  35. endwin();
  36. exit(0);
  37. if(shmctl(id_mem, IPC_RMID, NULL) == -1)
  38. perror("\n S. MEM_RMID error ");//usuwanie pamięci współdzielonej
  39.  
  40. printf("\n Ctrl_C Serwera\n\n");
  41. exit(EXIT_SUCCESS);
  42. }
  43.  
  44. int main(int agrc, char **argv) {
  45.  
  46. struct m_komunikat msg;
  47.  
  48.  
  49. signal(SIGINT, koniec);
  50. initscr();
  51. curs_set(0);
  52. start_color();
  53. clear();
  54. refresh();
  55.  
  56.  
  57. init_pair(1,COLOR_BLACK,COLOR_GREEN);
  58. init_pair(2,COLOR_WHITE,COLOR_CYAN);
  59. init_pair(3,COLOR_RED,COLOR_RED);
  60. init_pair(4,COLOR_GREEN,COLOR_GREEN);
  61. init_pair(5,COLOR_YELLOW, COLOR_YELLOW);
  62. init_pair(6,COLOR_BLUE, COLOR_BLUE);
  63.  
  64. init_pair(10, COLOR_CYAN, COLOR_CYAN);
  65. init_pair(11, COLOR_RED, COLOR_RED);
  66. init_pair(12, COLOR_WHITE, COLOR_WHITE);
  67. init_pair(13, COLOR_BLUE, COLOR_BLUE);
  68. init_pair(14, COLOR_BLACK, COLOR_BLACK);
  69. init_pair(15, COLOR_YELLOW, COLOR_YELLOW);
  70. init_pair(16, COLOR_MAGENTA, COLOR_MAGENTA);
  71. init_pair(17, COLOR_GREEN, COLOR_BLACK);
  72.  
  73.  
  74. attrset(COLOR_PAIR(2) | (A_NORMAL));
  75. move(0,0); printw(" ");
  76. move(1,0); printw(" ");
  77. move(2,0); printw(" ");
  78. move(3,0); printw(" ");
  79. move(4,0); printw(" ");
  80. move(5,0); printw(" ");
  81. move(6,0); printw(" ");
  82. move(7,0); printw(" ");
  83. move(8,0); printw(" ");
  84. move(9,0); printw(" ");
  85. attrset(COLOR_PAIR(2) | (A_NORMAL));
  86. move(1,4); printw("cccc");
  87. move(2,3); printw("cccccc");
  88. move(3,4); printw("cccc");
  89.  
  90. move(2,14); printw("cccc");
  91. move(3,13); printw("ccccccc");
  92. move(4,12); printw("cccccc");
  93. move(5,14); printw("ccccc");
  94.  
  95. move(0,24); printw("cccccc");
  96. move(1,26); printw("cccccc");
  97.  
  98. move(1,35); printw("ccccc");
  99. move(2,33); printw("cccccc");
  100. move(3,34); printw("cccccc");
  101. move(4,37); printw("ccccc");
  102.  
  103. move(2,41); printw("ccccc");
  104. move(3,39); printw("cccccc");
  105. move(4,40); printw("cccccc");
  106.  
  107. move(0,65); printw("cccccccccc");
  108. move(1,64); printw("cccccccccc");
  109. move(2,67); printw("cccccccccc");
  110. move(3,66); printw("cccccccc");
  111.  
  112. attrset(COLOR_PAIR(4) | (A_BOLD));
  113. move(10,0); printw(" ");
  114. move(11,0); printw(" ");
  115. move(12,0); printw(" ");
  116. move(13,0); printw(" ");
  117. move(14,0); printw(" ");
  118. move(15,0); printw(" ");
  119. move(16,0); printw(" ");
  120. move(17,0); printw(" ");
  121. move(18,0); printw(" ");
  122. move(19,0); printw(" ");
  123. move(20,0); printw(" ");
  124. move(21,0); printw(" ");
  125. move(22,0); printw(" ");
  126. move(23,0); printw(" ");
  127. move(24,0); printw(" ");
  128. move(25,0); printw(" ");
  129.  
  130.  
  131. attrset(COLOR_PAIR(1) | (A_BOLD));
  132. move(10,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  133. move(11,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  134. move(12,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  135. move(13,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  136. move(14,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  137. move(15,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  138. move(16,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  139. move(17,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  140. move(18,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  141. move(19,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  142. move(20,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  143. move(21,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  144. move(22,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  145. move(23,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  146. move(24,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  147. move(25,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
  148.  
  149.  
  150. attrset(COLOR_PAIR(3) | (A_BOLD));
  151. move(14,87); printw("======");
  152. move(13,89); printw("||");
  153. move(12,89); printw("||");
  154. move(11,89); printw("||");
  155. move(10,89); printw("||");
  156. move(9,89); printw("||");
  157.  
  158. move(13,96); printw("===");
  159. move(12,97); printw("|");
  160. move(11,97); printw("|");
  161. move(10,97); printw("|");
  162.  
  163.  
  164. move(16,103); printw("========");
  165. move(15,105); printw("||||");
  166. move(14,105); printw("||||");
  167. move(13,105); printw("||||");
  168. move(12,105); printw("||||");
  169. move(11,105); printw("||||");
  170. move(10,105); printw("||||");
  171. move(9,105); printw("||||");
  172.  
  173. attrset(COLOR_PAIR(4) | (A_BOLD));
  174.  
  175. move(8,87); printw("******");
  176. move(7,86); printw("********");
  177. move(6,85); printw("**********");
  178. move(5,86); printw("********");
  179. move(4,87); printw("******");
  180. move(3,88); printw("****");
  181.  
  182. move(9,96); printw("***");
  183. move(8,95); printw("*****");
  184. move(7,96); printw("***");
  185.  
  186. move(8,102); printw("**********");
  187. move(7,101); printw("************");
  188. move(6,100); printw("**************");
  189. move(5,101); printw("************");
  190. move(4,102); printw("**********");
  191. move(3,103); printw("********");
  192. move(2,104); printw("******");
  193. move(1,105); printw("****");
  194.  
  195. attrset(COLOR_PAIR(5) | (A_NORMAL));
  196. move(2,55); printw("ooooo");
  197. move(3,54); printw("ooooooo");
  198. move(4,53); printw("ooooooooo");
  199. move(5,54); printw("ooooooo");
  200. move(6,55); printw("ooooo");
  201. refresh();
  202.  
  203.  
  204.  
  205. if ((kom=msgget(KLUCZ_KOM, PERM | IPC_CREAT))==-1) {
  206. perror("\n S.msgget error\n");
  207. exit(-1);
  208. }
  209.  
  210.  
  211. if ((id_mem=shmget(KLUCZ_MEM, 3250*sizeof(int), PERM | IPC_CREAT | IPC_EXCL))==-1) { //pole*sizeof, ta sama wartosc w kliencie
  212. perror("\n S. MEM_CREAT error ");
  213. exit(EXIT_FAILURE);
  214. }
  215. if ((adres = (int *)shmat(id_mem,0, 0)) == ((int *)-1)) { //shmat powoduje, że uzyskujemy wskaźnik do pamięci współdzielonej
  216. perror("\n S. MEM_SHMAT error ");
  217. if(shmctl(id_mem, IPC_RMID, NULL) == -1)
  218. perror("\n S. MEM_RMID error ");
  219. exit(EXIT_FAILURE);
  220. }
  221.  
  222. attrset(COLOR_PAIR(1) | (A_BOLD));
  223.  
  224. int wys, szer;
  225. for (wys=0;wys<26;wys++)
  226. {
  227. for(szer=0;szer<125;szer++)
  228. {
  229. *(adres+(wys*125+szer))=0;
  230. ww=*(adres+(wys*125+szer));
  231.  
  232. if(wys>=10&&wys<=26&&szer>=7&&szer<=78)
  233. {
  234. *(adres+(wys*125+szer))=1;
  235. ww=*(adres+(wys*125+szer));
  236. }
  237. }
  238. }
  239.  
  240. refresh();
  241.  
  242.  
  243. kon=1;
  244. while(kon){
  245. if ((msgrcv(kom,&msg,sizeof(msg),0 ,0))==-1) {
  246. perror("\n S.msgrcv error ");
  247. kon=0;
  248. } else {
  249. switch(msg.typ) {
  250. case 1:
  251. kon=0;
  252. break;
  253. case 2:
  254. attrset(COLOR_PAIR(msg.kol1) | (A_NORMAL));
  255. move(msg.x1,msg.y1);
  256. printw("%c",msg.z1);
  257.  
  258. attrset(COLOR_PAIR(msg.kol2) | (A_BOLD));
  259. move(msg.x2,msg.y2);
  260. printw("%c",msg.z2);
  261. refresh();
  262. break;
  263.  
  264. default:
  265. break;
  266. }
  267. }
  268. }
  269.  
  270. refresh();
  271.  
  272. usleep(30000000);
  273.  
  274. msgctl(kom, IPC_RMID, 0);
  275. curs_set(1);
  276. clear();
  277. refresh();
  278. endwin();
  279.  
  280. //while (1){}
  281. return (0);
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement