InnadrilCastle

Connect

May 1st, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <netdb.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. #define PORT 1111
  9.  
  10. int main(void) {
  11.         int exitStatus, pocitadlo, deteg;
  12.         char buf[2048];
  13.         char *cas, *kurzor;
  14.         char hladam[] = "Nick: ";
  15.         char *poslem = "who";
  16.         struct addrinfo *adresaInfo;
  17.         struct addrinfo adresaNastavenia;
  18.         FILE *filePTR;
  19.         time_t timestamp;
  20.  
  21.         memset(&adresaNastavenia, 0, sizeof adresaNastavenia);  //vynulujeme adresaNastavenia
  22.         adresaNastavenia.ai_family = AF_INET;                   //pojdeme IPV4
  23.         adresaNastavenia.ai_socktype = SOCK_STREAM;             //cez protokol TCP
  24.         adresaNastavenia.ai_flags = AI_PASSIVE;                 //adresa sa doplni automaticky systemom
  25.  
  26.         filePTR = fopen("vystup.txt", "w");    //skusime otvorit na pisanie
  27.  
  28.         if (filePTR == NULL) {                                  //podarilo sa otvorit subor na pisanie?
  29.                 perror("Function \"fopen\" on file \"vystup.txt\" failed");    //nie, tak sa postazujeme menoFunkcie+perror
  30.                 exit(EXIT_FAILURE);                             //a skoncime
  31.         } //if (filePTR == NULL)
  32.  
  33.         int socketFD = socket(AF_INET, SOCK_STREAM, 0);         //FD socketu
  34.         if (socketFD < 0) {                                     //ak sa nepodarilo
  35.                 perror("Function \"socket\" failed");           //menoFunkcie+perror
  36.                 exit(EXIT_FAILURE);                             //skoncime
  37.         } //if (socketFD < 0)
  38.  
  39.         exitStatus = getaddrinfo("nastalker.sk", "1111", &adresaNastavenia, &adresaInfo); //ziskajme z hostname IP
  40.         if (exitStatus != 0) {                                  //prebehlo vsetko OK?
  41.                 if (exitStatus != EAI_SYSTEM)                   //nie jesystem error?
  42.                         fprintf(stderr, "Function \"getaddrinfo\" failed: %s\n", gai_strerror(exitStatus));     //tak pouzi gai_strerror
  43.                 else                                            //system error
  44.                         perror("Function \"getaddrinfo\" failed");      //tak menoFunkcie+perror
  45.                 exit(EXIT_FAILURE);                             //skoncime
  46.         } //if (exitStatus != 0)
  47.  
  48.         exitStatus = connect(socketFD, adresaInfo->ai_addr, adresaInfo->ai_addrlen);    //skusime nadviazat spojenie
  49.         if(exitStatus != 0) {                                   //podarilo sa nadviazat spojenie
  50.                 perror("Function \"connect\" failed");          //menoFunkcie+perror
  51.                 exit(EXIT_FAILURE);                             //skoncime
  52.         } //if(exitStatus != 0)
  53.  
  54.         deteg = 2;                                              //nastavime sa na zbehnutie nasledneho cyklu
  55.         while (deteg) {
  56.                 pocitadlo = recv(socketFD, buf, sizeof buf, 0); //skusime nieco nacitat zo socketu
  57.                 timestamp = time(NULL);                         //zapiseme terajsi cas
  58.                 if (timestamp == -1) {                          //podarilo sa zapisat cas?
  59.                         perror("Function \"time\" failed");     //menoFunkcie+perror
  60.                         exit(EXIT_FAILURE);                     //skoncime
  61.                 } //if (timestamp == -1)
  62.                 fprintf(filePTR, "%s", ctime(&timestamp));      //do suboru zapiseme timestamp
  63.                 fprintf(filePTR, "%s\n", buf);                  //do suboru zapiseme prijate znaky
  64.                 kurzor = strstr(buf, hladam);                   //skusme najst hladany string v prijatom
  65.                 memset(&buf, 0, sizeof buf);                    //vynulujeme buffer
  66.                 if (kurzor != NULL) {                           //nasli sme hladany retazec cez strstr()?
  67.                         if (deteg == 2) {
  68.                                 strcpy (buf, poslem);           //ano, tak do buffera dam string, co treba poslat
  69.                                 exitStatus = send(socketFD, buf, sizeof buf, 0);        //skusim to poslat
  70.                                 if (exitStatus == -1)           //ak sa nepodarilo poslat
  71.                                         perror("Function \"send\" failed");     //menoFunkcie+perror
  72.                         } //if (deteg == 2)
  73.                         deteg-- ;
  74.                 } //if (kurzor != NULL)
  75.         } //while (deteg)
  76.  
  77.         exitStatus = fclose(filePTR);                           //skusime zavriet subor
  78.         if (exitStatus != 0) {                                  //ak sa nepodarilo
  79.                 perror("Function \"fclose\" failed");           //menoFunkcie+perror
  80.                 exit(EXIT_FAILURE);                             //skoncime
  81.         } //if (exitStatus != 0)
  82.  
  83.         exitStatus = close(socketFD);                           //skusime zavriet socket
  84.         if(exitStatus != 0) {                                   //ak sa nepodarilo
  85.                 perror("Function \"close\" failed");            //menoFunkcie+perror
  86.                 exit(EXIT_FAILURE);                             //skoncime
  87.         } //if(exitStatus != 0)
  88.  
  89.         freeaddrinfo(adresaInfo);                               //uvolnime pamat
  90.  
  91.         exit(EXIT_SUCCESS);                                     //uspesne skoncime
  92. }
Add Comment
Please, Sign In to add comment