root/tags/5.52/ciupdsocx.c

Revision 4, 9.7 kB (checked in by heitor.barbieri, 2 years ago)

Versão 5.52 do cisis (28/04/2010)

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