Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <signal.h>
- #include <ncurses.h>
- #include <time.h>
- #include <sys/shm.h>
- #define PERM 0660
- #define KLUCZ_KOM (key_t)1231
- #define KLUCZ_MEM (key_t)0x10
- //serwer zapisuje w pamięci wspołdzielonej informacje o tym co jest na ekranie, klient informacje o samym sobie
- //
- struct m_komunikat {
- long typ;
- char z1, kol1, x1, y1, z2, kol2, x2, y2;
- };
- int kom, kon;
- int id_mem, ww, i, k=0;
- int *adres;
- void koniec(){
- msgctl(kom, IPC_RMID, 0);
- curs_set(1);
- clear();
- refresh();
- endwin();
- exit(0);
- if(shmctl(id_mem, IPC_RMID, NULL) == -1)
- perror("\n S. MEM_RMID error ");//usuwanie pamięci współdzielonej
- printf("\n Ctrl_C Serwera\n\n");
- exit(EXIT_SUCCESS);
- }
- int main(int agrc, char **argv) {
- struct m_komunikat msg;
- signal(SIGINT, koniec);
- initscr();
- curs_set(0);
- start_color();
- clear();
- refresh();
- init_pair(1,COLOR_BLACK,COLOR_GREEN);
- init_pair(2,COLOR_WHITE,COLOR_CYAN);
- init_pair(3,COLOR_RED,COLOR_RED);
- init_pair(4,COLOR_GREEN,COLOR_GREEN);
- init_pair(5,COLOR_YELLOW, COLOR_YELLOW);
- init_pair(6,COLOR_BLUE, COLOR_BLUE);
- init_pair(10, COLOR_CYAN, COLOR_CYAN);
- init_pair(11, COLOR_RED, COLOR_RED);
- init_pair(12, COLOR_WHITE, COLOR_WHITE);
- init_pair(13, COLOR_BLUE, COLOR_BLUE);
- init_pair(14, COLOR_BLACK, COLOR_BLACK);
- init_pair(15, COLOR_YELLOW, COLOR_YELLOW);
- init_pair(16, COLOR_MAGENTA, COLOR_MAGENTA);
- init_pair(17, COLOR_GREEN, COLOR_BLACK);
- attrset(COLOR_PAIR(2) | (A_NORMAL));
- move(0,0); printw(" ");
- move(1,0); printw(" ");
- move(2,0); printw(" ");
- move(3,0); printw(" ");
- move(4,0); printw(" ");
- move(5,0); printw(" ");
- move(6,0); printw(" ");
- move(7,0); printw(" ");
- move(8,0); printw(" ");
- move(9,0); printw(" ");
- attrset(COLOR_PAIR(2) | (A_NORMAL));
- move(1,4); printw("cccc");
- move(2,3); printw("cccccc");
- move(3,4); printw("cccc");
- move(2,14); printw("cccc");
- move(3,13); printw("ccccccc");
- move(4,12); printw("cccccc");
- move(5,14); printw("ccccc");
- move(0,24); printw("cccccc");
- move(1,26); printw("cccccc");
- move(1,35); printw("ccccc");
- move(2,33); printw("cccccc");
- move(3,34); printw("cccccc");
- move(4,37); printw("ccccc");
- move(2,41); printw("ccccc");
- move(3,39); printw("cccccc");
- move(4,40); printw("cccccc");
- move(0,65); printw("cccccccccc");
- move(1,64); printw("cccccccccc");
- move(2,67); printw("cccccccccc");
- move(3,66); printw("cccccccc");
- attrset(COLOR_PAIR(4) | (A_BOLD));
- move(10,0); printw(" ");
- move(11,0); printw(" ");
- move(12,0); printw(" ");
- move(13,0); printw(" ");
- move(14,0); printw(" ");
- move(15,0); printw(" ");
- move(16,0); printw(" ");
- move(17,0); printw(" ");
- move(18,0); printw(" ");
- move(19,0); printw(" ");
- move(20,0); printw(" ");
- move(21,0); printw(" ");
- move(22,0); printw(" ");
- move(23,0); printw(" ");
- move(24,0); printw(" ");
- move(25,0); printw(" ");
- attrset(COLOR_PAIR(1) | (A_BOLD));
- move(10,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(11,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(12,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(13,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(14,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(15,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(16,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(17,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(18,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(19,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(20,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(21,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(22,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(23,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(24,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- move(25,7); printw("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
- attrset(COLOR_PAIR(3) | (A_BOLD));
- move(14,87); printw("======");
- move(13,89); printw("||");
- move(12,89); printw("||");
- move(11,89); printw("||");
- move(10,89); printw("||");
- move(9,89); printw("||");
- move(13,96); printw("===");
- move(12,97); printw("|");
- move(11,97); printw("|");
- move(10,97); printw("|");
- move(16,103); printw("========");
- move(15,105); printw("||||");
- move(14,105); printw("||||");
- move(13,105); printw("||||");
- move(12,105); printw("||||");
- move(11,105); printw("||||");
- move(10,105); printw("||||");
- move(9,105); printw("||||");
- attrset(COLOR_PAIR(4) | (A_BOLD));
- move(8,87); printw("******");
- move(7,86); printw("********");
- move(6,85); printw("**********");
- move(5,86); printw("********");
- move(4,87); printw("******");
- move(3,88); printw("****");
- move(9,96); printw("***");
- move(8,95); printw("*****");
- move(7,96); printw("***");
- move(8,102); printw("**********");
- move(7,101); printw("************");
- move(6,100); printw("**************");
- move(5,101); printw("************");
- move(4,102); printw("**********");
- move(3,103); printw("********");
- move(2,104); printw("******");
- move(1,105); printw("****");
- attrset(COLOR_PAIR(5) | (A_NORMAL));
- move(2,55); printw("ooooo");
- move(3,54); printw("ooooooo");
- move(4,53); printw("ooooooooo");
- move(5,54); printw("ooooooo");
- move(6,55); printw("ooooo");
- refresh();
- if ((kom=msgget(KLUCZ_KOM, PERM | IPC_CREAT))==-1) {
- perror("\n S.msgget error\n");
- exit(-1);
- }
- if ((id_mem=shmget(KLUCZ_MEM, 3250*sizeof(int), PERM | IPC_CREAT | IPC_EXCL))==-1) { //pole*sizeof, ta sama wartosc w kliencie
- perror("\n S. MEM_CREAT error ");
- exit(EXIT_FAILURE);
- }
- if ((adres = (int *)shmat(id_mem,0, 0)) == ((int *)-1)) { //shmat powoduje, że uzyskujemy wskaźnik do pamięci współdzielonej
- perror("\n S. MEM_SHMAT error ");
- if(shmctl(id_mem, IPC_RMID, NULL) == -1)
- perror("\n S. MEM_RMID error ");
- exit(EXIT_FAILURE);
- }
- attrset(COLOR_PAIR(1) | (A_BOLD));
- int wys, szer;
- for (wys=0;wys<26;wys++)
- {
- for(szer=0;szer<125;szer++)
- {
- *(adres+(wys*125+szer))=0;
- ww=*(adres+(wys*125+szer));
- if(wys>=10&&wys<=26&&szer>=7&&szer<=78)
- {
- *(adres+(wys*125+szer))=1;
- ww=*(adres+(wys*125+szer));
- }
- }
- }
- refresh();
- kon=1;
- while(kon){
- if ((msgrcv(kom,&msg,sizeof(msg),0 ,0))==-1) {
- perror("\n S.msgrcv error ");
- kon=0;
- } else {
- switch(msg.typ) {
- case 1:
- kon=0;
- break;
- case 2:
- attrset(COLOR_PAIR(msg.kol1) | (A_NORMAL));
- move(msg.x1,msg.y1);
- printw("%c",msg.z1);
- attrset(COLOR_PAIR(msg.kol2) | (A_BOLD));
- move(msg.x2,msg.y2);
- printw("%c",msg.z2);
- refresh();
- break;
- default:
- break;
- }
- }
- }
- refresh();
- usleep(30000000);
- msgctl(kom, IPC_RMID, 0);
- curs_set(1);
- clear();
- refresh();
- endwin();
- //while (1){}
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement