|
| 1 | +#include <stdio.h> |
| 2 | +#include <unistd.h> |
| 3 | +#include <sys/socket.h> |
| 4 | +#include <sys/un.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <sys/select.h> |
| 7 | + |
| 8 | +#define CONTROL_SOCKET_NAME "/tmp/control_socket" |
| 9 | + |
| 10 | +int main (int argc, char **argv) |
| 11 | +{ |
| 12 | + struct sockaddr_un my_addr; |
| 13 | + unsigned char buf [100]; |
| 14 | + int client_socket; |
| 15 | + ssize_t rc; |
| 16 | + struct sockaddr_un to_addr; |
| 17 | + socklen_t to_len; |
| 18 | + struct timeval timeout; |
| 19 | + fd_set read_fds; |
| 20 | + |
| 21 | + client_socket = socket (AF_UNIX, SOCK_DGRAM, 0); |
| 22 | + if (client_socket < 0) { |
| 23 | + perror ("socket (AF_UNIX, SOCK_DGRAM, 0):"); |
| 24 | + return 1; |
| 25 | + } |
| 26 | + |
| 27 | + /* Setup a return address so the server can return a reply. */ |
| 28 | + memset (&my_addr, 0, sizeof (my_addr)); |
| 29 | + my_addr.sun_family = AF_UNIX; |
| 30 | + snprintf (my_addr.sun_path, sizeof (my_addr.sun_path) - 1, "/tmp/%d-return", getpid ()); |
| 31 | + unlink (my_addr.sun_path); |
| 32 | + if (bind (client_socket, (struct sockaddr *)&my_addr, sizeof (my_addr)) < 0) { |
| 33 | + perror ("bind (client_socket,,,):"); |
| 34 | + return 1; |
| 35 | + } |
| 36 | + |
| 37 | + /* Send message to server. */ |
| 38 | + memset (&to_addr, 0, sizeof (to_addr)); |
| 39 | + to_addr.sun_family = AF_UNIX; |
| 40 | + snprintf (to_addr.sun_path, sizeof (to_addr.sun_path) - 1, CONTROL_SOCKET_NAME); |
| 41 | + to_len = sizeof (to_addr); |
| 42 | + |
| 43 | + if (argc >= 2) { |
| 44 | + /* Send the provided numerical value to the server. */ |
| 45 | + buf [0] = 1; /* Set command. */ |
| 46 | + buf [1] = atoi (argv [1]); |
| 47 | + } else { |
| 48 | + /* Poll the server. */ |
| 49 | + buf [0] = 0; /* Poll command. */ |
| 50 | + } |
| 51 | + |
| 52 | + rc = sendto (client_socket, buf, 2, 0, |
| 53 | + (struct sockaddr *)&to_addr, to_len); |
| 54 | + if (rc < 0) { |
| 55 | + perror ("sendto (client_sock,,,):"); |
| 56 | + return 1; |
| 57 | + } |
| 58 | + |
| 59 | + /* Wait for a reply - but only for 2 seconds. */ |
| 60 | + timeout.tv_sec = 2; |
| 61 | + timeout.tv_usec = 0; |
| 62 | + FD_ZERO (&read_fds); |
| 63 | + FD_SET (client_socket, &read_fds); |
| 64 | + rc = select (client_socket + 1, &read_fds, NULL, NULL, &timeout); |
| 65 | + if (rc == 1) { |
| 66 | + /* Got something. */ |
| 67 | + char buf [100]; |
| 68 | + struct sockaddr_un from; |
| 69 | + socklen_t from_len = sizeof (from); |
| 70 | + int x; |
| 71 | + |
| 72 | + rc = recvfrom (client_socket, &buf, sizeof (buf), 0, |
| 73 | + (struct sockaddr *)&from, &from_len); |
| 74 | + |
| 75 | + printf ("Modtaget "); |
| 76 | + for (x = 0; x < rc; x++) { |
| 77 | + printf (" %02X", buf [x]); |
| 78 | + } |
| 79 | + printf ("\n"); |
| 80 | + } else { |
| 81 | + printf ("Timeout waiting for data\n"); |
| 82 | + } |
| 83 | + |
| 84 | + close (client_socket); |
| 85 | + unlink (my_addr.sun_path); |
| 86 | +} |
0 commit comments