| 1 | |
|---|
| 2 | /* ciupdsocx.c - included from ciupd.c |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | /* |
|---|
| 6 | Client program that makes a connection for a byte stream socket in the Internet namespace. |
|---|
| 7 | Once it has connected to the server, it sends a text string to the server and gets the response. |
|---|
| 8 | Returns the response size or a negative error value. |
|---|
| 9 | */ |
|---|
| 10 | /* |
|---|
| 11 | 16.9.6 Byte Stream Socket Example |
|---|
| 12 | Here is an example client program that makes a connection for a byte stream socket in the Internet namespace. It doesn't do anything particularly interesting once it has connected to the server; it just sends a text string to the server and exits. |
|---|
| 13 | This program uses init_sockaddr to set up the socket address; see Inet Example. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #ifndef CISIS_H |
|---|
| 17 | #define GEN_MAIN 1 |
|---|
| 18 | #endif |
|---|
| 19 | |
|---|
| 20 | #if GEN_MAIN |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include <errno.h> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <unistd.h> |
|---|
| 25 | #include <sys/types.h> |
|---|
| 26 | #endif /* GEN_MAIN */ |
|---|
| 27 | |
|---|
| 28 | #if GEN_MAIN |
|---|
| 29 | #define PORT 80 |
|---|
| 30 | #define MESSAGE "HEAD / HTTP/1.0\n\n" |
|---|
| 31 | #define SERVERHOST "www.bireme.br" |
|---|
| 32 | #endif /* GEN_MAIN */ |
|---|
| 33 | |
|---|
| 34 | #define BUFFERSIZE 51200 |
|---|
| 35 | |
|---|
| 36 | #if 0 |
|---|
| 37 | "HEAD /'message' HTTP/1.0\n\n" |
|---|
| 38 | "GET / HTTP/1.0\nUser-Agent: Mozilla/3.0 (compatible; Opera/3.0; Windows 95/NT4)\nAccept: */*\nHost: birk105.studby.uio.no:81\n\n" |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | #if PGCC |
|---|
| 42 | int init_sockaddr (struct sockaddr_in *name, const char *hostname, uint16_t port); |
|---|
| 43 | #endif |
|---|
| 44 | int mainclient_read_from_server (int cmd, int filedes, char *buffer, int buffersize, int maxrds); |
|---|
| 45 | int mainclient_write_to_server (int cmd, int filedes, char *message, int messagesize); |
|---|
| 46 | //int mainclient (int cmd, char *protocol, char *serverhost, uint16_t port, char *message, char *buffer, int buffersize, int maxrds); /* char *referrer, char *useragent, char *from */ |
|---|
| 47 | |
|---|
| 48 | /* |
|---|
| 49 | */ |
|---|
| 50 | #if PGCC |
|---|
| 51 | int |
|---|
| 52 | init_sockaddr (struct sockaddr_in *name, |
|---|
| 53 | const char *hostname, |
|---|
| 54 | uint16_t port) |
|---|
| 55 | { |
|---|
| 56 | struct hostent *hostinfo; |
|---|
| 57 | |
|---|
| 58 | name->sin_family = AF_INET; |
|---|
| 59 | name->sin_port = htons (port); |
|---|
| 60 | hostinfo = gethostbyname (hostname); |
|---|
| 61 | if (hostinfo == NULL) |
|---|
| 62 | { |
|---|
| 63 | /*perror("unknown host");*/ |
|---|
| 64 | return -1; |
|---|
| 65 | } |
|---|
| 66 | name->sin_addr = *(struct in_addr *) hostinfo->h_addr; |
|---|
| 67 | |
|---|
| 68 | return 0; |
|---|
| 69 | } |
|---|
| 70 | #endif |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | int |
|---|
| 74 | mainclient_write_to_server (int cmd, int filedes, char *message, int messagesize) |
|---|
| 75 | { |
|---|
| 76 | int nbytes; |
|---|
| 77 | |
|---|
| 78 | if (cmd == 3) fprintf (stderr, "Client: send message(%d): '%s'\n", messagesize, message); |
|---|
| 79 | if (cmd == 2) fprintf (stderr, "Client: send message(%d) \n", messagesize); |
|---|
| 80 | |
|---|
| 81 | nbytes = write (filedes, message, messagesize); |
|---|
| 82 | if (nbytes < messagesize) |
|---|
| 83 | { |
|---|
| 84 | /* Write error */ |
|---|
| 85 | /*perror ("write");*/ |
|---|
| 86 | return -1; |
|---|
| 87 | } |
|---|
| 88 | else |
|---|
| 89 | return nbytes; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | int |
|---|
| 94 | mainclient_read_from_server (int cmd, int filedes, char *buffer, int buffersize, int maxrds) |
|---|
| 95 | { |
|---|
| 96 | int nbytes; |
|---|
| 97 | int more; |
|---|
| 98 | |
|---|
| 99 | buffer[0]='\0'; |
|---|
| 100 | nbytes = read (filedes, buffer, buffersize-1); |
|---|
| 101 | if (nbytes < 0) |
|---|
| 102 | { |
|---|
| 103 | /* Read error */ |
|---|
| 104 | /*perror ("read");*/ |
|---|
| 105 | return -1; |
|---|
| 106 | } |
|---|
| 107 | else |
|---|
| 108 | { |
|---|
| 109 | /* Data read. */ |
|---|
| 110 | buffer[nbytes]='\0'; |
|---|
| 111 | if (cmd == 3) fprintf (stderr, "Client: got message(%d): '%s' \n", nbytes, buffer); |
|---|
| 112 | if (cmd == 2) fprintf (stderr, "Client: got message(%d) \n", nbytes); |
|---|
| 113 | |
|---|
| 114 | while (--maxrds) |
|---|
| 115 | if ((more = read (filedes, buffer+nbytes, buffersize-1-nbytes)) > 0) |
|---|
| 116 | { |
|---|
| 117 | buffer[nbytes+more]='\0'; |
|---|
| 118 | if (cmd == 3) fprintf (stderr, "Client: got +message(%d): '%s' \n", more, buffer+nbytes); |
|---|
| 119 | if (cmd == 2) fprintf (stderr, "Client: got +message(%d) \n", more); |
|---|
| 120 | nbytes+=more; |
|---|
| 121 | } |
|---|
| 122 | else break; |
|---|
| 123 | |
|---|
| 124 | return nbytes; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | int |
|---|
| 130 | mainclient (int cmd, |
|---|
| 131 | char *protocol, /* HTTP */ /* X1417 */ |
|---|
| 132 | char *serverhost, /* www.bireme.br */ /* serverofi.bireme.br */ |
|---|
| 133 | uint16_t port, /* 80 */ /* 1417 */ |
|---|
| 134 | char *message, /* HEAD|GET /docpath HTTP/1.0\n\n */ /* wtrig2 c=dff maxrel=3 minsim=0.87 text=diabetes mellitus */ |
|---|
| 135 | char *buffer, /* results */ |
|---|
| 136 | int buffersize, /* max size */ |
|---|
| 137 | int maxrds /* max reads */ /* 1 */ |
|---|
| 138 | ) |
|---|
| 139 | { |
|---|
| 140 | int sock; |
|---|
| 141 | #if PGCC |
|---|
| 142 | struct sockaddr_in servername; |
|---|
| 143 | #endif |
|---|
| 144 | |
|---|
| 145 | int nbytes; |
|---|
| 146 | int rc; |
|---|
| 147 | char *p,*q; |
|---|
| 148 | |
|---|
| 149 | /* Trap null msg */ |
|---|
| 150 | if (!message) /*return 0;*/ message="\\n"; //\n |
|---|
| 151 | if (!*message) /*return 0;*/ message="\\n"; |
|---|
| 152 | |
|---|
| 153 | if (cmd >= 1) fprintf(stdout, "<mainclient>\n"); |
|---|
| 154 | if (cmd >= 1) fprintf(stdout, "<message>%s</message><serverhost>%s</serverhost><port>%d</port>\n",message,serverhost,port); |
|---|
| 155 | |
|---|
| 156 | /* Convert calling \n to LF */ |
|---|
| 157 | for (p=buffer, q=message; *q; p++, q++) |
|---|
| 158 | if (*q=='\\' && *(q+1)=='n') { |
|---|
| 159 | *p = '\n'; q++; |
|---|
| 160 | } |
|---|
| 161 | else *p = *q; |
|---|
| 162 | *p='\0'; |
|---|
| 163 | |
|---|
| 164 | #if !PGCC |
|---|
| 165 | #if CIWTF |
|---|
| 166 | /* Fake output. */ |
|---|
| 167 | if (*buffer) { |
|---|
| 168 | char *msgp=strdup(buffer); |
|---|
| 169 | if (strcmp(protocol,"X1417") == 0) { |
|---|
| 170 | //loadcoll |
|---|
| 171 | rc = loadcoll(cmd, ciawtfp, /*message*/ msgp, buffer); |
|---|
| 172 | if (rc < 0) { |
|---|
| 173 | ///*perror ("read/loadcoll (client)");*/ |
|---|
| 174 | nbytes=(-5); *buffer='\0'; |
|---|
| 175 | } |
|---|
| 176 | else nbytes=rc; |
|---|
| 177 | } |
|---|
| 178 | else |
|---|
| 179 | if (strcmp(protocol,"HTTP") == 0) { |
|---|
| 180 | sprintf(buffer,"%s","HTTP/1.1 200 OK\n...\nContent-Length: 14082\nConnection: close\nContent-Type: text/html\n\n"); |
|---|
| 181 | nbytes=strlen(buffer); |
|---|
| 182 | } |
|---|
| 183 | #if CICPP |
|---|
| 184 | delete[] msgp; |
|---|
| 185 | #else |
|---|
| 186 | FREE(msgp); |
|---|
| 187 | #endif |
|---|
| 188 | } |
|---|
| 189 | #endif //CIWTF |
|---|
| 190 | #else //!PGCC |
|---|
| 191 | |
|---|
| 192 | /* Create the socket. */ |
|---|
| 193 | sock = socket (PF_INET, SOCK_STREAM, 0); |
|---|
| 194 | if (sock < 0) |
|---|
| 195 | { |
|---|
| 196 | /*perror ("socket (client)");*/ |
|---|
| 197 | return -1; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /* Init (init_sockaddr). */ |
|---|
| 201 | rc = init_sockaddr (&servername, serverhost, port); |
|---|
| 202 | if (rc < 0) |
|---|
| 203 | { |
|---|
| 204 | /*fprintf (stderr, "Unknown host %s.\n", hostname);*/ |
|---|
| 205 | close (sock); |
|---|
| 206 | return -2; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /* Connect to the server. */ |
|---|
| 210 | rc = connect (sock, (struct sockaddr *) &servername, sizeof (servername)); |
|---|
| 211 | if (rc < 0) |
|---|
| 212 | { |
|---|
| 213 | /*perror ("connect (client)");*/ |
|---|
| 214 | close (sock); |
|---|
| 215 | return -3; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /* Send data to the server. */ |
|---|
| 219 | nbytes = mainclient_write_to_server (cmd, sock, /*message*/buffer, strlen(/*message*/buffer)); |
|---|
| 220 | if (nbytes < 0) |
|---|
| 221 | { |
|---|
| 222 | /*perror ("write (client)");*/ |
|---|
| 223 | close (sock); |
|---|
| 224 | return -4; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | /* Get data from the server. */ |
|---|
| 228 | nbytes = mainclient_read_from_server (cmd, sock, buffer, buffersize, maxrds); |
|---|
| 229 | if (nbytes < 0) |
|---|
| 230 | { |
|---|
| 231 | /*perror ("read (client)");*/ |
|---|
| 232 | close (sock); |
|---|
| 233 | return -5; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /* Release the socket. */ |
|---|
| 237 | close (sock); |
|---|
| 238 | |
|---|
| 239 | #endif //!PGCC |
|---|
| 240 | |
|---|
| 241 | /* parse 1st line |
|---|
| 242 | HTTP/1.1 200 OK |
|---|
| 243 | HTTP/1.1 3 Found |
|---|
| 244 | HTTP/1.1 403 Forbidden |
|---|
| 245 | HTTP/1.1 404 Not Found |
|---|
| 246 | HTTP/1.1 501 Method Not Implemented |
|---|
| 247 | */ |
|---|
| 248 | |
|---|
| 249 | if (cmd >= 1) { |
|---|
| 250 | fprintf(stdout, "<content>%s</content>\n",buffer); |
|---|
| 251 | if (strncmp(buffer,"HTTP/",5) == 0) { |
|---|
| 252 | for (p=buffer; *p; p++) if (*p==' ') break; *p++='\0'; rc=atoi(p); |
|---|
| 253 | for ( ; *p; p++) if (*p==' ') break; *p++='\0'; q=p; |
|---|
| 254 | for ( ; *p; p++) if (*p=='\r' || *p=='\n') break; *p='\0'; |
|---|
| 255 | fprintf(stdout, "<protocol>%s</protocol><code>%d</code><msg>%s</msg>\n",buffer,rc,q); |
|---|
| 256 | } |
|---|
| 257 | fprintf(stdout, "</mainclient>\n"); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | return nbytes; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | /* Main |
|---|
| 267 | |
|---|
| 268 | ./client4 0 "HEAD / HTTP/1.0\n\n" |
|---|
| 269 | ./client4 1 "HEAD / HTTP/1.0\n\n" |
|---|
| 270 | ./client4 2 "HEAD / HTTP/1.0\n\n" |
|---|
| 271 | |
|---|
| 272 | ./client4 1 "HEAD http://lis.bvs.br/xml2html/xmlListT.php?xml%5B%5D=http://lis.bvs.br/lis-Regional/P/define.xml&xsl=http://lis.bvs.br/lis-Regional/home.xsl HTTP/1.0\n\n" lis.bvs.br 80 |
|---|
| 273 | ./client4 0 "GET http://lis.bvs.br/xml2html/xmlListT.php?xml%5B%5D=http://lis.bvs.br/lis-Regional/P/define.xml&xsl=http://lis.bvs.br/lis-Regional/home.xsl HTTP/1.0\n\n" lis.bvs.br 80 |
|---|
| 274 | |
|---|
| 275 | */ |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | #if GEN_MAIN |
|---|
| 279 | int |
|---|
| 280 | main(int argc, char *argv[]) |
|---|
| 281 | { |
|---|
| 282 | /* calling parameters */ |
|---|
| 283 | int cmd; |
|---|
| 284 | char *message=MESSAGE; |
|---|
| 285 | char *serverhost=SERVERHOST; |
|---|
| 286 | uint16_t port=PORT; |
|---|
| 287 | int maxrds=1; |
|---|
| 288 | int rc; |
|---|
| 289 | |
|---|
| 290 | char buffer[BUFFERSIZE]; |
|---|
| 291 | |
|---|
| 292 | if (argc == 1) { |
|---|
| 293 | fprintf(stderr, "client4 0=plain|1=xml|2=tell|3=trace \"HEAD|GET docpath\\n\\n\" | \"wtrig2 c=col text=x\" [serverhost [port [maxreads] ] ] \n"); |
|---|
| 294 | exit (EXIT_FAILURE); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | cmd=atoi(argv[1]); |
|---|
| 298 | |
|---|
| 299 | if (argc > 2) message=argv[2]; |
|---|
| 300 | if (argc > 3) serverhost=argv[3]; |
|---|
| 301 | if (argc > 4) port=(uint16_t)atoi(argv[4]); |
|---|
| 302 | if (argc > 5) maxrds=atoi(argv[5]); |
|---|
| 303 | |
|---|
| 304 | if (cmd >= 3) fprintf(stderr, "client4: \"%s\" %s %d \n",message,serverhost,port); |
|---|
| 305 | |
|---|
| 306 | rc=mainclient(cmd, message, serverhost, port, buffer, BUFFERSIZE, maxrds); |
|---|
| 307 | |
|---|
| 308 | if (rc < 0) { |
|---|
| 309 | if (cmd >= 3) fprintf(stderr, "client4: exit_failure %d \n", rc); |
|---|
| 310 | exit (EXIT_FAILURE); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /* Output results */ |
|---|
| 314 | fprintf(stdout, "%s",buffer); |
|---|
| 315 | |
|---|
| 316 | if (cmd >= 3) fprintf(stderr, "client4: exit_success \n"); |
|---|
| 317 | exit (EXIT_SUCCESS); |
|---|
| 318 | } |
|---|
| 319 | #endif /* GEN_MAIN */ |
|---|
| 320 | |
|---|
| 321 | #undef GEN_MAIN |
|---|