#include #include #include #include #include #include #define BUFSIZE 4096 #define error(format, ...) { fprintf (stderr, format, __VA_ARGS__); exit (1); } main(int argc, char **argv){ /* setup connection */ if (argc != 2) { error("usage %s \n", argv[0]); } int sock_fd = socket(PF_UNIX, SOCK_STREAM,0); struct sockaddr_un server; if (-1 == sock_fd) { perror("can't open socket"); exit(1); } server.sun_family = PF_UNIX; strcpy(server.sun_path, argv[1]); if (-1 == connect(sock_fd, (const struct sockaddr *) &server, sizeof(server))){ perror("can't connect to socket"); exit(1); } /* dispatch data */ while (1){ int stdin_bytes = -1; int sock_bytes = -1; fd_set inset; unsigned char buffer[BUFSIZE]; FD_ZERO(&inset); if (stdin_bytes) FD_SET(0, &inset); // watch stdin if (sock_bytes) FD_SET(sock_fd, &inset); // watch socket input if (-1 == select(sock_fd+1,&inset,0,0,0)){ perror("select error"); exit(1); } if (stdin_bytes && FD_ISSET(0, &inset)){ stdin_bytes = read(0, &buffer, BUFSIZE); // read stdin if (stdin_bytes < 0) { perror("can't read stdin"); exit(1); } // write to socket write(sock_fd, &buffer, stdin_bytes); } if (sock_bytes && FD_ISSET(sock_fd, &inset)){ sock_bytes = read(sock_fd, &buffer, BUFSIZE); // read stdin if (sock_bytes < 0) { perror("can't read socket"); exit(1); } // write to socket write(1, &buffer, sock_bytes); } if (!(stdin_bytes || sock_bytes)) exit(0); // return when both EOF } }