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

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

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


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

1 stdarg 1.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 stdarg 1.4 int server_read(int idx, sockbuf_iobuf_t *data, void *client_data)
10 stdarg 1.1 {
11 stdarg 1.4 printf("%s\n", data->data);
12 stdarg 1.1 return(0);
13     }
14    
15 stdarg 1.4 int server_eof(int idx, void *client_data)
16 stdarg 1.1 {
17 stdarg 1.4 printf("Server closed connection!\n");
18     exit(0);
19 stdarg 1.1 }
20    
21 stdarg 1.4 int server_err(int idx, int err, void *client_data)
22 stdarg 1.1 {
23 stdarg 1.4 perror("server_err");
24 stdarg 1.1 exit(0);
25     }
26    
27 stdarg 1.4 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 stdarg 1.5 static sockbuf_event_t server_event = {
37     (Function) 5,
38 stdarg 1.4 (Function) "server",
39 stdarg 1.5 server_read,
40 stdarg 1.4 NULL,
41 stdarg 1.5 server_eof,
42     server_err,
43     server_connect
44 stdarg 1.1 };
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 stdarg 1.2 sockbuf_delete(idx);
56     return(0);
57 stdarg 1.1 }
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 stdarg 1.2 char *host;
72 stdarg 1.1
73 stdarg 1.2 srand(time(0));
74 stdarg 1.1 if (argc < 2) {
75 stdarg 1.2 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 stdarg 1.1 }
86    
87 stdarg 1.2 printf("Connecting to %s %d\n", host, port);
88     sock = socket_create(host, port, SOCKET_CLIENT);
89 stdarg 1.1 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 stdarg 1.5 sockbuf_set_handler(server_idx, server_event, NULL);
97 stdarg 1.2 sslmode_init();
98 stdarg 1.1
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