Advertisement
zaxisss

Untitled

Jun 29th, 2022
592
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None
  1. #include <sys/socket.h>
  2. #include <sys/types.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <ncurses.h>
  7. #define SERV_PORT 45000
  8.  
  9.  
  10.  
  11. int main(int argc, char** argv){
  12.  
  13.     initscr();
  14.     start_color();
  15.     wbkgd(stdscr, COLOR_PAIR(2));
  16.     init_pair(1, COLOR_BLACK, COLOR_WHITE);
  17.     init_pair(2, COLOR_WHITE, COLOR_MAGENTA);
  18.     init_pair(3,COLOR_WHITE,COLOR_BLACK);
  19.     init_pair(4,COLOR_BLACK,COLOR_WHITE);
  20.     refresh();
  21.     endwin();
  22.     int socketfd;
  23.     int connfd;
  24.     struct sockaddr_in serwer_addres;
  25.  
  26.     socketfd = socket(AF_INET, SOCK_DGRAM, 0);
  27.     bzero(&serwer_addres, sizeof(serwer_addres));
  28.     serwer_addres.sin_family = AF_INET;
  29.     serwer_addres.sin_port = htons(SERV_PORT);
  30.     inet_pton(AF_INET, argv[1], &serwer_addres.sin_addr);
  31.  
  32.     StrKli(socketfd, serwer_addres);
  33.  
  34.     printw("\nKLIENT: Program zostal zakonczony\n");
  35.     exit(0);
  36. }
  37.  
  38. void StrKli(int socketfd, struct sockaddr_in serwer_addres)
  39. {
  40.  
  41. initscr();
  42. start_color();
  43.  
  44. init_pair(3,COLOR_BLACK,COLOR_BLUE);
  45. wbkgd(stdscr,COLOR_PAIR(3));
  46.     int n;
  47.     char linia[101];
  48.     char liniarcv[100];
  49.     char helloServer[2];
  50.     int klientId = 0x00;
  51.     helloServer[0] = klientId;
  52.     helloServer[1] = 'x';
  53.     sendto(socketfd,helloServer,10,0,&serwer_addres,sizeof(serwer_addres));
  54.  
  55.     n = recvfrom(socketfd, liniarcv, 100, 0, NULL, NULL);
  56.     printw("\nOtrzymałeś ID: %d\n",liniarcv[0]);
  57.     klientId = liniarcv[0];
  58.  
  59.     linia[0] = klientId;
  60.     while(fgets(&linia[1], 100, stdin) != NULL){
  61.         n = strlen(linia);
  62.         if( n <= 1 ) return;
  63.         if ( sendto(socketfd,linia,n,0,&serwer_addres,sizeof(serwer_addres)) == -1){
  64.             printf("Blad");
  65.         }
  66.         do{
  67.         printw(">");
  68.             n = recvfrom(socketfd, liniarcv, 100, 0, NULL, NULL);
  69.             liniarcv[n] = '\0';
  70.             fputs(liniarcv, stdout);
  71.         }while(liniarcv[strlen(liniarcv)-1] != 0x02);
  72.     }
  73.    
  74. }
Advertisement
RAW Paste Data Copied
Advertisement