2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-03-14 11:30:15 quinn
10 * Revision 1.2 1995/03/14 10:28:03 quinn
11 * More work on demo server.
13 * Revision 1.1 1995/03/10 18:22:45 quinn
14 * The rudiments of an asynchronous server.
19 * Simple, static server. I wouldn't advise a static server unless you
20 * really have to, but it's great for debugging memory management. :)
36 * handle incoming connect requests.
38 void listener(IOCHAN h, int event)
40 COMSTACK line = (COMSTACK) iochan_getdata(h);
43 if (event == EVENT_INPUT)
45 if (cs_listen(line, 0, 0) < 0)
47 if (cs_errno(line) == CSNODATA)
49 fprintf(stderr, "cs_listen failed.\n");
52 iochan_setflags(h, EVENT_OUTPUT); /* set us up for accepting */
54 else if (event == EVENT_OUTPUT)
59 if (!(new_line = cs_accept(line)))
61 fprintf(stderr, "Accept failed.\n");
64 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
67 fprintf(stderr, "Failed to create iochan\n");
70 if (!(newas = create_association(new_chan, new_line)))
72 fprintf(stderr, "Failed to create new assoc.\n");
75 iochan_setdata(new_chan, newas);
76 iochan_setflags(h, EVENT_INPUT); /* reset for listening */
80 fprintf(stderr, "Bad event on listener.\n");
86 * Set up a listening endpoint, and give it to the event-handler.
88 void add_listener(char *where)
92 char mode[100], addr[100];
96 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
98 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
102 if (!strcmp(mode, "tcp"))
104 if (!(ap = tcpip_strtoaddr(addr)))
106 fprintf(stderr, "Address resolution failed for TCP.\n");
112 else if (!strcmp(mode, "osi"))
114 if (!(ap = mosi_strtoaddr(addr)))
116 fprintf(stderr, "Address resolution failed for TCP.\n");
124 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
127 if (!(l = cs_create(type, 1)))
129 fprintf(stderr, "Failed to create listener\n");
132 if (cs_bind(l, ap, CS_SERVER) < 0)
134 fprintf(stderr, "Failed to bind.\n");
138 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT)))
140 fprintf(stderr, "Failed to create IOCHAN-type\n");
143 iochan_setdata(lst, l);
146 int main(int argc, char **argv)
148 int ret, listeners = 0;
151 while ((ret = options("l:", argv, argc, &arg)) != -2)
154 case 0: me = arg; break;
155 case 'l': add_listener(arg); listeners++; break;
157 fprintf(stderr, "Usage: %s [-l <listener-addr>]\n", me);
162 fprintf(stderr, "%s: Must specify at least one listener.\n", me);