1 | #include <stdio.h> |
---|
2 | #include <unistd.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string.h> |
---|
5 | #include <signal.h> |
---|
6 | #include <sys/types.h> |
---|
7 | #include <sys/socket.h> |
---|
8 | #include <sys/wait.h> |
---|
9 | #include <sys/select.h> |
---|
10 | #include <sys/time.h> |
---|
11 | #include <netinet/in.h> |
---|
12 | #include <getopt.h> |
---|
13 | |
---|
14 | #include "clientlib.h" |
---|
15 | |
---|
16 | /* |
---|
17 | * min()/max() macros that also do |
---|
18 | * strict type-checking.. See the |
---|
19 | * "unnecessary" pointer comparison. |
---|
20 | */ |
---|
21 | #define min(x,y) ({ \ |
---|
22 | typeof(x) _x = (x); \ |
---|
23 | typeof(y) _y = (y); \ |
---|
24 | (void) (&_x == &_y); \ |
---|
25 | _x < _y ? _x : _y; }) |
---|
26 | |
---|
27 | #define max(x,y) ({ \ |
---|
28 | typeof(x) _x = (x); \ |
---|
29 | typeof(y) _y = (y); \ |
---|
30 | (void) (&_x == &_y); \ |
---|
31 | _x > _y ? _x : _y; }) |
---|
32 | |
---|
33 | void help(const char *argv0) |
---|
34 | { |
---|
35 | fprintf(stderr, |
---|
36 | "Syntax: %s addr:port load\n", |
---|
37 | argv0); |
---|
38 | exit(1); |
---|
39 | } |
---|
40 | |
---|
41 | int connect_fd = -1; |
---|
42 | |
---|
43 | void activity(int signum) |
---|
44 | { |
---|
45 | write(connect_fd, "hello\n\r", 7); |
---|
46 | write(1,".",1); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | int main(int argc, char *argv[]) |
---|
51 | { |
---|
52 | int val; |
---|
53 | int status; |
---|
54 | struct sockaddr_in connect_addr; |
---|
55 | int connect_port = -1; |
---|
56 | |
---|
57 | if (argc != 3) { |
---|
58 | help(argv[0]); |
---|
59 | } |
---|
60 | |
---|
61 | char addr[100]; |
---|
62 | if (sscanf(argv[1], "%100[^:\n]:%d\n", addr, &connect_port) != 2) { |
---|
63 | help(argv[0]); |
---|
64 | } |
---|
65 | int memory = strtoul(argv[2],0,0); |
---|
66 | |
---|
67 | connect_fd = connect_renderer(addr, connect_port, memory); |
---|
68 | |
---|
69 | if (connect_fd == -1) { |
---|
70 | return 1; |
---|
71 | } |
---|
72 | |
---|
73 | printf("Connection accepted.\n"); |
---|
74 | |
---|
75 | #ifdef DEBUGGING |
---|
76 | if (signal(SIGALRM, activity) == SIG_ERR) { |
---|
77 | perror("signal"); |
---|
78 | } |
---|
79 | |
---|
80 | struct itimerval itvalue = { |
---|
81 | {0,500000}, {0,500000} |
---|
82 | }; |
---|
83 | status = setitimer(ITIMER_REAL, &itvalue, NULL); |
---|
84 | #endif |
---|
85 | |
---|
86 | fd_set rfds; |
---|
87 | FD_ZERO(&rfds); |
---|
88 | while(1) { |
---|
89 | char buffer[100]; |
---|
90 | FD_SET(0,&rfds); |
---|
91 | FD_SET(connect_fd,&rfds); |
---|
92 | status = select(connect_fd+1, &rfds, NULL, NULL, NULL); |
---|
93 | if (FD_ISSET(0,&rfds)) { |
---|
94 | status = read(0,buffer,sizeof(buffer)); |
---|
95 | if (status <= 0) { |
---|
96 | fprintf(stderr,"Goodbye\n"); |
---|
97 | exit(0); |
---|
98 | } else { |
---|
99 | status = write(connect_fd, buffer, status); |
---|
100 | if (status < 0) { |
---|
101 | perror("write"); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | else if (FD_ISSET(connect_fd,&rfds)) { |
---|
107 | status = read(connect_fd,buffer,sizeof(buffer)); |
---|
108 | if (status <= 0) { |
---|
109 | fprintf(stderr,"Server closed connection\n"); |
---|
110 | exit(0); |
---|
111 | } else { |
---|
112 | status = write(1, buffer, status); |
---|
113 | if (status < 0) { |
---|
114 | perror("write"); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | else { |
---|
120 | fprintf(stderr,"Strange case for select: %d\n", status); |
---|
121 | exit(1); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|