Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <time.h>
  2. #include <getopt.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/stat.h>
  9. #include <sys/socket.h>
  10. #include <signal.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <sys/un.h>
  16.  
  17. int endOfDesc;
  18. char myPath[20] = {0};
  19. char socketPath[30] = {0};
  20.  
  21. struct sockaddr_un sockAddrConnection;
  22.  
  23. int socketPrivate;
  24. int myNumber;
  25. typedef struct{
  26.     char c;
  27.     struct timespec stempel;
  28. }MessageToArch;
  29.  
  30. void readOption(int argc, char* argv[]){
  31.     char c;
  32.     char* end;
  33.     while((c=getopt(argc, argv, "z:f:")) != -1){
  34.         switch(c){
  35.             case 'z':  
  36.                 endOfDesc = strtol(optarg, &end, 10);
  37.                 break;
  38.             case 'f':
  39.                 memcpy(myPath, optarg, 20);
  40.             //  printf("%s\n", myPath);
  41.                 break;                     
  42.         }
  43.     }
  44. }
  45.  
  46. void function(int sigNum, siginfo_t* s, void* p){
  47.     MessageToArch secretMessage;
  48. //  memset(secretMessage, 0, sizeof(MessageToArch));
  49.    
  50.     read(endOfDesc, &(secretMessage.c), 1 );
  51.     printf("%c\n", secretMessage.c);
  52.     clock_gettime(CLOCK_REALTIME, &secretMessage.stempel);
  53.  
  54.     int wasSendCorrect = sendto(socketPrivate, &secretMessage, sizeof(MessageToArch), 0,
  55.         (struct sockaddr*)& sockAddrConnection, sizeof(struct sockaddr_un));  
  56.     if(wasSendCorrect == -1){
  57.         perror("/Robotnik/:Message wasn't send becouse:");
  58.     }
  59.  
  60. }
  61.  
  62. void setPrivateSocket(){
  63.     memset(&sockAddrConnection, 0, sizeof(struct sockaddr_un));
  64.     memset(socketPath, 0, sizeof(char)*20);
  65.     sockAddrConnection.sun_family = AF_UNIX;
  66.  
  67.     sprintf(socketPath, "/tmp/private/%s", myPath);
  68.     memcpy(sockAddrConnection.sun_path+1, socketPath, 25);//ile
  69.     printf("sciazkoa: %s\n", sockAddrConnection.sun_path+1);
  70.  
  71.     socketPrivate = socket(AF_UNIX,SOCK_DGRAM ,0);
  72.     sleep(2);
  73.     if( connect(socketPrivate, (struct sockaddr*)& sockAddrConnection, sizeof(sockAddrConnection))){
  74.         perror("/Robotnik/:Something wrong with connect: ");
  75.         close(socketPrivate);
  76.     //  exit(0);   
  77.     }
  78.  
  79.    
  80. }
  81.  
  82. int main(int argc, char* argv[]){
  83.    
  84.     readOption(argc, argv);
  85.     struct sigaction sigAction;
  86.     memset(&sigAction, 0, sizeof(struct sigaction));
  87.  
  88.     sigAction.sa_sigaction = &function;
  89.     sigAction.sa_flags = SA_SIGINFO;
  90.    
  91.     sigaction(SIGALRM, &sigAction, NULL);
  92.  
  93.     setPrivateSocket();
  94.     while(1){};
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement