root/trunk/client4.c

Revision 389, 8.2 kB (checked in by heitor.barbieri, 3 weeks ago)

essage first commit

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