Guest User

wtfff

a guest
May 3rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include <arpa/inet.h>  
  2. #include <netdb.h>
  3. #include <netinet/in.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #include <iostream>
  8. #include <sys/wait.h>
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <pthread.h>
  14. #include <cerrno>
  15. #include <unistd.h>
  16. #include <termios.h>
  17. #include <vector>
  18. #include <sys/stat.h>
  19. #include <iostream>
  20. #include <fstream>
  21. #include <fcntl.h>
  22. #include <dirent.h>
  23. #include <linux/input.h>
  24. #include <sys/select.h>
  25. #include <sys/time.h>
  26.  
  27. #include <signal.h>
  28.  
  29.  
  30. using namespace std;
  31.  
  32. char c;
  33. int my_socket;
  34. struct sockaddr_in local, remote;
  35. socklen_t addrlen = sizeof(remote);
  36.  
  37. struct input_event ev[64];
  38. int fd, rd, value, size = sizeof (struct input_event);
  39. char name[256] = "Unknown";
  40. char *device = NULL;
  41.  
  42. pthread_t thread1;
  43.  
  44. int start (string ip, uint16_t port)
  45. {
  46.     my_socket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  47.     memset((char*)&local, 0, sizeof(local));
  48.     local.sin_family = AF_INET;
  49.     local.sin_addr.s_addr = htonl(INADDR_ANY);
  50.     local.sin_port = htons (port);
  51.     bind (my_socket, (sockaddr*)&local, sizeof(local));
  52.    
  53.  
  54. return 0;
  55.  
  56. }
  57.  
  58. int send (string ip, uint16_t port, char msg)
  59. {
  60.     memset((char*)&remote, 0, sizeof(remote));
  61.     remote.sin_family = AF_INET; //POTRZEBNE?
  62.     remote.sin_addr.s_addr = (inet_addr(ip.c_str()));
  63.     remote.sin_port = htons (port);
  64.     sendto(my_socket, &msg, 1,0,(sockaddr*)&remote,sizeof(remote));
  65.     return 1;
  66. }
  67.  
  68. void* receive (void* useless)
  69. {
  70.     char c;
  71.     while(1){
  72. cout << remote.sin_port << remote.sin_addr.s_addr << endl;
  73.         while(recvfrom(my_socket, &c,1,0,(sockaddr*)&remote,&addrlen) > 0){
  74.             fflush(stdout);
  75.             //putchar(c);
  76.             printf("kod: %d\n", c);
  77.             //fflush(stdout);
  78.  
  79.         }
  80.     }
  81.  
  82. }
  83.  
  84.  
  85.  
  86. int main(int argc, char * argv[])
  87. {
  88.     char d;
  89.     //opening EVENT file   
  90.     device = argv[4];
  91.    
  92.     if ((fd = open (device, O_RDONLY)) == -1)
  93.         printf ("%s is not a vaild device.n", device);
  94.  
  95.  
  96.    
  97.  
  98.     start("127.0.0.1", atoi(argv[3]));
  99.     pthread_create( &thread1, NULL, &receive, NULL);
  100.  
  101.     while(1) {
  102.         rd = read (fd, ev, size * 64);
  103.         value = ev[0].value;
  104.         if ( ev[1].value == 1 && ev[1].type == 1){ // Only read the key press event
  105.             d = ev[1].code;
  106.             send(argv[1], atoi(argv[2]), d);
  107.           }
  108.         //fflush(stdout);
  109.  
  110.    
  111.     }
  112.  
  113.    
  114. return 0;
  115. }
Add Comment
Please, Sign In to add comment