document timeout option
[yaz-moved-to-github.git] / test / tstcomstack.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <yaz/test.h>
10 #include <yaz/comstack.h>
11 #include <yaz/tcpip.h>
12
13 static int comstack_example(const char *server_address_str)
14 {    
15     COMSTACK stack;
16     char *buf = 0;
17     int size = 0, length_incoming;
18     void *server_address_ip;
19     int status;
20
21     char *protocol_package = "GET / HTTP/1.0\r\n\r\n";
22     int protocol_package_length = strlen(protocol_package);
23
24     stack = cs_create(tcpip_type, 1, PROTO_HTTP);
25     if (!stack) {
26         perror("cs_create");  /* use perror() here since we have no stack yet */
27         return -1;
28     }
29     
30     server_address_ip = cs_straddr(stack, server_address_str);
31     if (!server_address_ip)
32     {
33         fprintf(stderr, "cs_straddr: address could not be resolved\n");
34         return -1;
35     }
36     
37     status = cs_connect(stack, server_address_ip);
38     if (status != 0) {
39         fprintf(stderr, "cs_connect: %s\n", cs_strerror(stack));
40         return -1;
41     }
42     
43     status = cs_put(stack, protocol_package, protocol_package_length);
44     if (status) {
45         fprintf(stderr, "cs_put: %s\n", cs_strerror(stack));
46         return -1;
47     }
48     
49     /* Now get a response */
50     
51     length_incoming = cs_get(stack, &buf, &size);
52     if (!length_incoming) {
53         fprintf(stderr, "Connection closed\n");
54         return -1;
55     } else if (length_incoming < 0) {
56         fprintf(stderr, "cs_get: %s\n", cs_strerror(stack));
57         return -1;
58     }
59     
60     /* Print result */
61     fwrite(buf, length_incoming, 1, stdout);
62     
63     /* clean up */
64     cs_close(stack);
65     if (buf)
66         free(buf);
67     return 0;
68 }
69
70
71 int main(int argc, char **argv)
72 {
73     YAZ_CHECK_INIT(argc, argv);
74     if (argc == 2)
75        comstack_example(argv[1]);
76     YAZ_CHECK_TERM;
77 }
78
79 /*
80  * Local variables:
81  * c-basic-offset: 4
82  * c-file-style: "Stroustrup"
83  * indent-tabs-mode: nil
84  * End:
85  * vim: shiftwidth=4 tabstop=8 expandtab
86  */
87