Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <iostream>
- #include <sys/wait.h>
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <cerrno>
- #include <unistd.h>
- #include <termios.h>
- #include <vector>
- #include <sys/stat.h>
- #include <iostream>
- #include <fstream>
- #include <fcntl.h>
- #include <dirent.h>
- #include <linux/input.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <signal.h>
- using namespace std;
- char c;
- int my_socket;
- struct sockaddr_in local, remote;
- socklen_t addrlen = sizeof(remote);
- struct input_event ev[64];
- int fd, rd, value, size = sizeof (struct input_event);
- char name[256] = "Unknown";
- char *device = NULL;
- pthread_t thread1;
- int start (string ip, uint16_t port)
- {
- my_socket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- memset((char*)&local, 0, sizeof(local));
- local.sin_family = AF_INET;
- local.sin_addr.s_addr = htonl(INADDR_ANY);
- local.sin_port = htons (port);
- bind (my_socket, (sockaddr*)&local, sizeof(local));
- return 0;
- }
- int send (string ip, uint16_t port, char msg)
- {
- memset((char*)&remote, 0, sizeof(remote));
- remote.sin_family = AF_INET; //POTRZEBNE?
- remote.sin_addr.s_addr = (inet_addr(ip.c_str()));
- remote.sin_port = htons (port);
- sendto(my_socket, &msg, 1,0,(sockaddr*)&remote,sizeof(remote));
- return 1;
- }
- void* receive (void* useless)
- {
- char c;
- while(1){
- cout << remote.sin_port << remote.sin_addr.s_addr << endl;
- while(recvfrom(my_socket, &c,1,0,(sockaddr*)&remote,&addrlen) > 0){
- fflush(stdout);
- //putchar(c);
- printf("kod: %d\n", c);
- //fflush(stdout);
- }
- }
- }
- int main(int argc, char * argv[])
- {
- char d;
- //opening EVENT file
- device = argv[4];
- if ((fd = open (device, O_RDONLY)) == -1)
- printf ("%s is not a vaild device.n", device);
- start("127.0.0.1", atoi(argv[3]));
- pthread_create( &thread1, NULL, &receive, NULL);
- while(1) {
- rd = read (fd, ev, size * 64);
- value = ev[0].value;
- if ( ev[1].value == 1 && ev[1].type == 1){ // Only read the key press event
- d = ev[1].code;
- send(argv[1], atoi(argv[2]), d);
- }
- //fflush(stdout);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment