call nonblock with sock instead of sid
[cnet.git] / test.c
blob724faaeb373a4d4ff7cfb797ac50e52ee5642560
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 printf ("SERVER: on_read sid:%d len:%d --> %s\n", sid, len, data);
45 cnprintf (sid, "From SERVER: Got your Message !\n");
46 return 0;
49 int ts_on_eof (int sid, void *conn_data, int err)
51 printf ("SERVER: on_eof sid:%d\n", sid);
52 return 0;
55 int ts_on_close (int sid, void *conn_data)
57 printf ("SERVER: on_close sid:%d\n", sid);
58 return 0;
61 int ts_on_newclient (int sid, void *conn_data, int newsid, char *host, int port)
63 printf ("SERVER: new_client sid:%d host:%s port:%d\n", newsid, host, port);
64 cnet_handler (newsid, &testserver_handler);
65 return 0;
68 int main (const int argc, const char **argv)
70 int ssid;
71 ssid = cnet_listen (TESTSERVER_HOST, TESTSERVER_PORT);
72 if (-1 == ssid) {
73 printf ("SERVER: ERROR failed to connect errno:%d\n", errno);
74 printf ("SERVER: %s\n", strerror(errno));
75 return errno;
78 cnet_handler (ssid, &testserver_handler);
79 printf ("SERVER: OUR PID: %d\n", getpid());
80 printf ("SERVER: started server sid:%d\n", ssid);
82 for (;;) {
83 printf ("SERVER: calling cnet_select()\n");
84 cnet_select (5000);
87 return 0;
89 #endif
92 /* test client: starts x clients and echo's random text to server */
93 #ifdef TESTCLIENT
94 int tc_on_connect (int sid, void *conn_data)
96 printf ("CLIENT: on_connect sid:%d\n", sid);
97 return 0;
100 int tc_on_read (int sid, void *conn_data, char *data, int len)
102 printf ("CLIENT: on_read sid:%d len:%d --> %s\n", sid, len, data);
103 return 0;
106 int tc_on_eof (int sid, void *conn_data, int err)
108 printf ("CLIENT: on_eof sid:%d\n", sid);
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\n", i, "Format String");
136 cnet_select(2000);
137 sleep (2);
139 cnet_close (sid);
141 return 0;
143 #endif