Advertisement
zaxisss

Untitled

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