| 1 | /* |
|---|
| 2 | Client program that makes a connection for a byte stream socket in the Internet namespace. |
|---|
| 3 | Once it has connected to the server, it sends a text string to the server and gets the response. |
|---|
| 4 | Returns the response size or a negative error value. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #include <stdio.h> |
|---|
| 8 | #include <errno.h> |
|---|
| 9 | #include <stdlib.h> |
|---|
| 10 | #include <unistd.h> |
|---|
| 11 | #include <sys/types.h> |
|---|
| 12 | #include <sys/socket.h> |
|---|
| 13 | #include <netinet/in.h> |
|---|
| 14 | #include <netdb.h> |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #define PORT 80 |
|---|
| 18 | #define MESSAGE "HEAD / HTTP/1.0\n\n" |
|---|
| 19 | #define SERVERHOST "www.bireme.br" |
|---|
| 20 | |
|---|
| 21 | #define BUFFERSIZE 51200 |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | #if 0 |
|---|
| 26 | "HEAD /'message' HTTP/1.0\n\n" |
|---|
| 27 | "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" |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | int init_sockaddr (struct sockaddr_in *name, const char *hostname, uint16_t port); |
|---|
| 31 | |
|---|
| 32 | int mainclient_read_from_server (int cmd, int filedes, char *buffer, int buffersize); |
|---|
| 33 | |
|---|
| 34 | int mainclient_write_to_server (int cmd, int filedes, char *message, int messagesize); |
|---|
| 35 | |
|---|
| 36 | int mainclient(int cmd, char *message, char *serverhost, uint16_t port, char *buffer, int buffersize); /* char *referrer, char *useragent, char *from */ |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /* |
|---|
| 42 | */ |
|---|
| 43 | int |
|---|
| 44 | init_sockaddr (struct sockaddr_in *name, |
|---|
| 45 | const char *hostname, |
|---|
| 46 | uint16_t port) |
|---|
| 47 | { |
|---|
| 48 | struct hostent *hostinfo; |
|---|
| 49 | |
|---|
| 50 | name->sin_family = AF_INET; |
|---|
| 51 | name->sin_port = htons (port); |
|---|
| 52 | hostinfo = gethostbyname (hostname); |
|---|
| 53 | if (hostinfo == NULL) |
|---|
| 54 | { |
|---|
| 55 | /*perror("unknown host");*/ |
|---|
| 56 | return -1; |
|---|
| 57 | } |
|---|
| 58 | name->sin_addr = *(struct in_addr *) hostinfo->h_addr; |
|---|
| 59 | |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | int |
|---|
| 66 | mainclient_write_to_server (int cmd, int filedes, char *message, int messagesize) |
|---|
| 67 | { |
|---|
| 68 | int nbytes; |
|---|
| 69 | |
|---|
| 70 | if (cmd == 3) fprintf (stderr, "Client: send message(%d): '%s'\n", messagesize, message); |
|---|
| 71 | if (cmd == 2) fprintf (stderr, "Client: send message(%d) \n", messagesize); |
|---|
| 72 | |
|---|
| 73 | nbytes = write (filedes, message, messagesize); |
|---|
| 74 | if (nbytes < messagesize) |
|---|
| 75 | { |
|---|
| 76 | /* Write error */ |
|---|
| 77 | /*perror ("write");*/ |
|---|
| 78 | return -1; |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | return nbytes; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | int |
|---|
| 86 | mainclient_read_from_server (int cmd, int filedes, char *buffer, int buffersize) |
|---|
| 87 | { |
|---|
| 88 | int nbytes; |
|---|
| 89 | int more; |
|---|
| 90 | |
|---|
| 91 | nbytes = read (filedes, buffer, buffersize-1); |
|---|
| 92 | if (nbytes < 0) |
|---|
| 93 | { |
|---|
| 94 | /* Read error */ |
|---|
| 95 | /*perror ("read");*/ |
|---|
| 96 | return -1; |
|---|
| 97 | } |
|---|
| 98 | else |
|---|
| 99 | { |
|---|
| 100 | /* Data read. */ |
|---|
| 101 | buffer[nbytes]='\0'; |
|---|
| 102 | if (cmd == 3) fprintf (stderr, "Client: got message(%d): '%s'\n", nbytes, buffer); |
|---|
| 103 | if (cmd == 2) fprintf (stderr, "Client: got message(%d) \n", nbytes); |
|---|
| 104 | |
|---|
| 105 | while ((more = read (filedes, buffer+nbytes, buffersize-1-nbytes)) > 0) |
|---|
| 106 | { |
|---|
| 107 | buffer[nbytes+more]='\0'; |
|---|
| 108 | if (cmd == 3) fprintf (stderr, "Client: got message(%d): '%s'\n", more, buffer+nbytes); |
|---|
| 109 | if (cmd == 2) fprintf (stderr, "Client: got message(%d) \n", more); |
|---|
| 110 | nbytes+=more; |
|---|
| 111 | } |
|---|
| 112 | return nbytes; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | int |
|---|
| 118 | mainclient(int cmd, |
|---|
| 119 | char *message, /* HEAD|GET /docpath HTTP/1.0\n\n */ |
|---|
| 120 | char *serverhost, /* www.bireme.br */ |
|---|
| 121 | uint16_t port, /* 80 */ |
|---|
| 122 | char *buffer, /* results */ |
|---|
| 123 | int buffersize /* max size */ |
|---|
| 124 | ) |
|---|
| 125 | { |
|---|
| 126 | int sock; |
|---|
| 127 | struct sockaddr_in servername; |
|---|
| 128 | |
|---|
| 129 | int messagelen=strlen(message); |
|---|
| 130 | int nbytes; |
|---|
| 131 | int rc; |
|---|
| 132 | |
|---|
| 133 | /* Create the socket. */ |
|---|
| 134 | sock = socket (PF_INET, SOCK_STREAM, 0); |
|---|
| 135 | if (sock < 0) |
|---|
| 136 | { |
|---|
| 137 | /*perror ("socket (client)");*/ |
|---|
| 138 | return -1; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /* Init (init_sockaddr). */ |
|---|
| 142 | rc = init_sockaddr (&servername, serverhost, port); |
|---|
| 143 | if (rc < 0) |
|---|
| 144 | { |
|---|
| 145 | /*fprintf (stderr, "Unknown host %s.\n", hostname);*/ |
|---|
| 146 | return -2; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /* Connect to the server. */ |
|---|
| 150 | rc = connect (sock, (struct sockaddr *) &servername, sizeof (servername)); |
|---|
| 151 | if (rc < 0) |
|---|
| 152 | { |
|---|
| 153 | /*perror ("connect (client)");*/ |
|---|
| 154 | return -3; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /* Send data to the server. */ |
|---|
| 158 | nbytes = mainclient_write_to_server (cmd, sock, message, messagelen); |
|---|
| 159 | if (nbytes < 0) |
|---|
| 160 | { |
|---|
| 161 | /*perror ("write (client)");*/ |
|---|
| 162 | return -4; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /* Get data from the server */ |
|---|
| 166 | nbytes = mainclient_read_from_server (cmd, sock, buffer, buffersize); |
|---|
| 167 | if (nbytes < 0) |
|---|
| 168 | { |
|---|
| 169 | /*perror ("read (client)");*/ |
|---|
| 170 | return -5; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /* Release connection/socket */ |
|---|
| 174 | close (sock); |
|---|
| 175 | |
|---|
| 176 | return nbytes; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | /* Main |
|---|
| 182 | |
|---|
| 183 | ./client3 0 "HEAD / HTTP/1.0\n\n" |
|---|
| 184 | ./client3 1 "HEAD / HTTP/1.0\n\n" |
|---|
| 185 | ./client3 2 "HEAD / HTTP/1.0\n\n" |
|---|
| 186 | |
|---|
| 187 | ./client3 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 |
|---|
| 188 | ./client3 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 |
|---|
| 189 | |
|---|
| 190 | [aot@serverofi socket]$ ./client3 go "HEAD / HTTP/1.0\n\n" |
|---|
| 191 | Client: send message(17) |
|---|
| 192 | Client: got message(249) |
|---|
| 193 | HTTP/1.1 200 OK |
|---|
| 194 | Date: Sun, 17 Jul 2005 22:45:57 GMT |
|---|
| 195 | Server: Apache/1.3.9 (Unix) |
|---|
| 196 | Last-Modified: Wed, 15 Jun 2005 19:22:27 GMT |
|---|
| 197 | ETag: "5bd2d-3702-42b07ff3" |
|---|
| 198 | Accept-Ranges: bytes |
|---|
| 199 | Content-Length: 14082 |
|---|
| 200 | Connection: close |
|---|
| 201 | Content-Type: text/html |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | [aot@serverofi socket]$ ./client3 go "GET / HTTP/1.0\n\n" >x.htm |
|---|
| 205 | Client: send message(16) |
|---|
| 206 | Client: got message(1460) |
|---|
| 207 | Client: got message(1460) |
|---|
| 208 | Client: got message(1460) |
|---|
| 209 | Client: got message(1460) |
|---|
| 210 | Client: got message(1460) |
|---|
| 211 | Client: got message(1460) |
|---|
| 212 | Client: got message(1460) |
|---|
| 213 | Client: got message(1460) |
|---|
| 214 | Client: got message(1460) |
|---|
| 215 | Client: got message(1191) |
|---|
| 216 | [aot@serverofi socket]$ ll x.htm |
|---|
| 217 | -rw-rw-r-- 1 aot aot 14332 Jul 17 19:48 x.htm |
|---|
| 218 | [aot@serverofi socket]$ head -11 x.htm |
|---|
| 219 | HTTP/1.1 200 OK |
|---|
| 220 | Date: Sun, 17 Jul 2005 22:48:14 GMT |
|---|
| 221 | Server: Apache/1.3.9 (Unix) |
|---|
| 222 | Last-Modified: Wed, 15 Jun 2005 19:22:27 GMT |
|---|
| 223 | ETag: "5bd2d-3702-42b07ff3" |
|---|
| 224 | Accept-Ranges: bytes |
|---|
| 225 | Content-Length: 14082 |
|---|
| 226 | Connection: close |
|---|
| 227 | Content-Type: text/html |
|---|
| 228 | |
|---|
| 229 | <HTML> |
|---|
| 230 | [aot@serverofi socket]$ |
|---|
| 231 | |
|---|
| 232 | [aot@serverofi socket]$ |
|---|
| 233 | [aot@serverofi socket]$ ./client3 |
|---|
| 234 | client3 go|tell|trace "HEAD|GET docpath\n\n" [serverhost [port] ]: Success |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | */ |
|---|
| 238 | int |
|---|
| 239 | main(int argc, char *argv[]) |
|---|
| 240 | { |
|---|
| 241 | /* calling parameters */ |
|---|
| 242 | int cmd; |
|---|
| 243 | char *message=MESSAGE; |
|---|
| 244 | char *serverhost=SERVERHOST; |
|---|
| 245 | uint16_t port=PORT; |
|---|
| 246 | int rc; |
|---|
| 247 | |
|---|
| 248 | char buffer[BUFFERSIZE]; |
|---|
| 249 | char *p,*q; |
|---|
| 250 | |
|---|
| 251 | if (argc == 1) { |
|---|
| 252 | fprintf(stderr, "client3 0=plain|1=xml|2=tell|3=trace \"HEAD|GET docpath\\n\\n\" [serverhost [port] ] \n"); |
|---|
| 253 | exit (EXIT_FAILURE); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | cmd=atoi(argv[1]); |
|---|
| 257 | |
|---|
| 258 | if (argc > 2) message=argv[2]; |
|---|
| 259 | if (argc > 3) serverhost=argv[3]; |
|---|
| 260 | if (argc > 4) port=(uint16_t)atoi(argv[4]); |
|---|
| 261 | |
|---|
| 262 | if (cmd >= 3) fprintf(stderr, "client3: \"%s\" %s %d \n",message,serverhost,port); |
|---|
| 263 | |
|---|
| 264 | /* convert calling \n to LF */ |
|---|
| 265 | for (p=buffer, q=message; *q; p++, q++) |
|---|
| 266 | if (*q=='\\' && *(q+1)=='n') { |
|---|
| 267 | *p = '\n'; q++; |
|---|
| 268 | } |
|---|
| 269 | else *p = *q; |
|---|
| 270 | *p='\0'; |
|---|
| 271 | |
|---|
| 272 | if (cmd >= 3) fprintf(stderr, "client3: \"%s\" \n",buffer); |
|---|
| 273 | if (cmd >= 1) fprintf(stdout, "<message>%s</message><serverhost>%s</serverhost><port>%d</port>\n",buffer,serverhost,port); |
|---|
| 274 | |
|---|
| 275 | rc=mainclient(cmd, buffer, serverhost, port, buffer, BUFFERSIZE); |
|---|
| 276 | |
|---|
| 277 | if (rc < 0) { |
|---|
| 278 | if (cmd >= 3) fprintf(stdout, "client3: exit_failure %d \n", rc); |
|---|
| 279 | exit (EXIT_FAILURE); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /* output results */ |
|---|
| 283 | if (cmd >= 1) fprintf(stdout, "<content>%s</content>\n",buffer); |
|---|
| 284 | else fprintf(stdout, "%s" ,buffer); |
|---|
| 285 | |
|---|
| 286 | /* parse 1st line |
|---|
| 287 | HTTP/1.1 200 OK |
|---|
| 288 | HTTP/1.1 3 Found |
|---|
| 289 | HTTP/1.1 403 Forbidden |
|---|
| 290 | HTTP/1.1 404 Not Found |
|---|
| 291 | HTTP/1.1 501 Method Not Implemented |
|---|
| 292 | */ |
|---|
| 293 | for (p=buffer; *p; p++) if (*p==' ') break; |
|---|
| 294 | *p++='\0'; rc=atoi(p); |
|---|
| 295 | for ( ; *p; p++) if (*p==' ') break; |
|---|
| 296 | *p++='\0'; q=p; |
|---|
| 297 | for ( ; *p; p++) if (*p=='\r' || *p=='\n') break; |
|---|
| 298 | *p='\0'; |
|---|
| 299 | |
|---|
| 300 | if (cmd >= 1) fprintf(stdout, "<protocol>%s</protocol><code>%d</code><msg>%s</msg>\n",buffer,rc,q); |
|---|
| 301 | |
|---|
| 302 | if (cmd >= 3) fprintf(stderr, "client3: exit_success \n"); |
|---|
| 303 | exit (EXIT_SUCCESS); |
|---|
| 304 | } |
|---|