fix big bug with losing fd's, and flag usage
[cnet.git] / test.c
blob06c7a31a699e614e5ff9eef0fbfef28187818492
1 /* Copyright (c) 2007 Zachery Hostens <zacheryph@gmail.com>
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include "cnet.h"
28 #define TESTSERVER_HOST "127.0.0.1"
29 #define TESTSERVER_PORT 64930
31 /* test server: echo's anything it recieves to client & output */
32 #ifdef TESTSERVER
33 int ts_on_read (int sid, void *conn_data, char *data, int len);
34 int ts_on_eof (int sid, void *conn_data, int err);
35 int ts_on_close (int sid, void *conn_data);
36 int ts_on_newclient (int sid, void *conn_data, int newsid, char *host, int port);
38 cnet_handler_t testserver_handler = {
39 NULL, ts_on_read, ts_on_eof, ts_on_close, ts_on_newclient
42 int ts_on_read (int sid, void *conn_data, char *data, int len)
44 cnprintf (sid, "From SERVER: Got your Message !\n");
45 return 0;
48 int ts_on_eof (int sid, void *conn_data, int err)
50 return 0;
53 int ts_on_close (int sid, void *conn_data)
55 printf ("SERVER: on_close sid:%d\n", sid);
56 return 0;
59 int ts_on_newclient (int sid, void *conn_data, int newsid, char *host, int port)
61 printf ("SERVER: new_client sid:%d\n", newsid);
62 cnet_handler (newsid, &testserver_handler);
63 cnet_linemode (newsid, 1);
64 return 0;
67 int main (const int argc, const char **argv)
69 int ssid, ret;
70 ssid = cnet_listen (TESTSERVER_HOST, TESTSERVER_PORT);
71 if (-1 == ssid) {
72 printf ("SERVER: ERROR failed to connect errno:%d\n", errno);
73 printf ("SERVER: %s\n", strerror(errno));
74 return errno;
77 cnet_handler (ssid, &testserver_handler);
78 printf ("SERVER: OUR PID: %d\n", getpid());
79 printf ("SERVER: started server sid:%d\n", ssid);
81 for (;;) {
82 ret = cnet_select (5000);
83 if (-1 == ret) {
84 printf ("SERVER: we are broken(%d): %s\n", errno, strerror(errno));
85 break;
89 return 0;
91 #endif
94 /* test client: starts x clients and echo's random text to server */
95 #ifdef TESTCLIENT
96 int tc_on_connect (int sid, void *conn_data)
98 printf ("CLIENT: on_connect sid:%d\n", sid);
99 return 0;
102 int tc_on_read (int sid, void *conn_data, char *data, int len)
104 return 0;
107 int tc_on_eof (int sid, void *conn_data, int err)
109 return 0;
112 int tc_on_close (int sid, void *conn_data)
114 printf ("CLIENT: on_close sid:%d\n", sid);
115 exit(0);
118 cnet_handler_t testclient_handler = {
119 tc_on_connect, tc_on_read, tc_on_eof, tc_on_close, NULL
122 int main (const int argc, const char **argv)
124 int i, sid;
126 sid = cnet_connect (TESTSERVER_HOST, TESTSERVER_PORT, NULL, 0);
127 cnet_handler (sid, &testclient_handler);
128 if (0 > sid) {
129 printf ("CLIENT: ERROR cnet_connect failed: %d\n", sid);
130 printf ("perror: %s\n", strerror(errno));
131 return 0;
134 for (i = 0; i < 4; i++) {
135 cnprintf (sid, "my formatted: %d --> %s\r\nOur second Info....\n", i, "Format String");
136 cnet_select(2000);
138 cnet_close (sid);
140 return 0;
142 #endif