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
29 #define TESTSERVER_HOST "127.0.0.1"
30 #define TESTSERVER_PORT 64930
32 void sigpipe_handler (int err
) {
33 printf ("DEBUG: recieved a SIGPIPE!!!\n");
36 /* test server: echo's anything it recieves to client & output */
38 int ts_on_read (int sid
, void *conn_data
, char *data
, int len
);
39 int ts_on_eof (int sid
, void *conn_data
, int err
);
40 int ts_on_close (int sid
, void *conn_data
);
41 int ts_on_newclient (int sid
, void *conn_data
, int newsid
, char *host
, int port
);
43 cnet_handler_t testserver_handler
= {
44 NULL
, ts_on_read
, ts_on_eof
, ts_on_close
, ts_on_newclient
47 int ts_on_read (int sid
, void *conn_data
, char *data
, int len
)
49 cnprintf (sid
, "From SERVER: Got your Message !\n");
53 int ts_on_eof (int sid
, void *conn_data
, int err
)
58 int ts_on_close (int sid
, void *conn_data
)
60 printf ("SERVER: on_close sid:%d\n", sid
);
64 int ts_on_newclient (int sid
, void *conn_data
, int newsid
, char *host
, int port
)
66 printf ("SERVER: new_client sid:%d\n", newsid
);
67 cnet_handler (newsid
, &testserver_handler
);
68 cnet_linemode (newsid
, 1);
72 int main (const int argc
, const char **argv
)
76 signal (SIGPIPE
, sigpipe_handler
);
78 ssid
= cnet_listen (TESTSERVER_HOST
, TESTSERVER_PORT
);
80 printf ("SERVER: ERROR failed to connect errno:%d\n", errno
);
81 printf ("SERVER: %s\n", strerror(errno
));
85 cnet_handler (ssid
, &testserver_handler
);
86 printf ("SERVER: OUR PID: %d\n", getpid());
87 printf ("SERVER: started server sid:%d\n", ssid
);
90 ret
= cnet_select (5000);
92 printf ("SERVER: we are broken(%d): %s\n", errno
, strerror(errno
));
102 /* test client: starts x clients and echo's random text to server */
104 int tc_on_connect (int sid
, void *conn_data
)
106 printf ("CLIENT: on_connect sid:%d\n", sid
);
110 int tc_on_read (int sid
, void *conn_data
, char *data
, int len
)
115 int tc_on_eof (int sid
, void *conn_data
, int err
)
120 int tc_on_close (int sid
, void *conn_data
)
122 printf ("CLIENT: on_close sid:%d\n", sid
);
126 cnet_handler_t testclient_handler
= {
127 tc_on_connect
, tc_on_read
, tc_on_eof
, tc_on_close
, NULL
130 int main (const int argc
, const char **argv
)
134 sid
= cnet_connect (TESTSERVER_HOST
, TESTSERVER_PORT
, NULL
, 0);
135 cnet_handler (sid
, &testclient_handler
);
137 printf ("CLIENT: ERROR cnet_connect failed: %d\n", sid
);
138 printf ("perror: %s\n", strerror(errno
));
142 for (i
= 0; i
< 2; i
++) {
143 cnprintf (sid
, "my formatted: %d --> %s\r\nOur second Info....\n", i
, "Format String");