/[cvs]/eggdrop1.9/testcode/client.c
ViewVC logotype

Contents of /eggdrop1.9/testcode/client.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Mar 13 14:16:58 2002 UTC (18 years ago) by stdarg
Branch: MAIN
Changes since 1.4: +7 -16 lines
File MIME type: text/x-chdr
* Updated chat client

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "my_socket.h"
5 #include "sockbuf.h"
6
7 int server_idx;
8
9 int server_read(int idx, sockbuf_iobuf_t *data, void *client_data)
10 {
11 printf("%s\n", data->data);
12 return(0);
13 }
14
15 int server_eof(int idx, void *client_data)
16 {
17 printf("Server closed connection!\n");
18 exit(0);
19 }
20
21 int server_err(int idx, int err, void *client_data)
22 {
23 perror("server_err");
24 exit(0);
25 }
26
27 int server_connect(int idx, void *client_data)
28 {
29 printf("Connected to server!\n");
30 sslmode_on(idx, 0); /* 0 means client, 1 means server */
31 zipmode_on(idx);
32 linemode_on(idx);
33 return(0);
34 }
35
36 static sockbuf_event_t server_event = {
37 (Function) 5,
38 (Function) "server",
39 server_read,
40 NULL,
41 server_eof,
42 server_err,
43 server_connect
44 };
45
46 int stdin_read(int idx, sockbuf_iobuf_t *data, void *client_data)
47 {
48 data->data[data->len] = '\n';
49 sockbuf_write(server_idx, data->data, data->len+1);
50 return(0);
51 }
52
53 int stdin_eof(int idx, void *client_data)
54 {
55 sockbuf_delete(idx);
56 return(0);
57 }
58
59 static sockbuf_event_t stdin_event = {
60 (Function) 4,
61 (Function) "stdin",
62 stdin_read,
63 NULL,
64 stdin_eof,
65 NULL
66 };
67
68 main (int argc, char *argv[])
69 {
70 int sock, port, stdin_idx;
71 char *host;
72
73 srand(time(0));
74 if (argc < 2) {
75 host = "127.0.0.1";
76 port = 12345;
77 }
78 else if (argc == 2) {
79 host = argv[1];
80 port = 12345;
81 }
82 else {
83 host = argv[1];
84 port = atoi(argv[2]);
85 }
86
87 printf("Connecting to %s %d\n", host, port);
88 sock = socket_create(host, port, SOCKET_CLIENT);
89 if (sock < 0) {
90 perror("socket_create");
91 return(0);
92 }
93 socket_set_nonblock(sock, 1);
94 socket_set_nonblock(0, 1);
95 server_idx = sockbuf_new(sock, SOCKBUF_CLIENT);
96 sockbuf_set_handler(server_idx, server_event, NULL);
97 sslmode_init();
98
99 stdin_idx = sockbuf_new(0, 0);
100 sockbuf_set_handler(stdin_idx, stdin_event, NULL);
101 linemode_on(stdin_idx);
102 while (1) {
103 sockbuf_update_all(-1);
104 }
105 return(0);
106 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23