/[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.8 - (hide annotations) (download) (as text)
Sun Aug 11 20:50:35 2002 UTC (17 years, 9 months ago) by stdarg
Branch: MAIN
Changes since 1.7: +3 -4 lines
File MIME type: text/x-chdr
*** empty log message ***

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.7 int server_read(void *client_data, int idx, char *data, int len)
10 stdarg 1.1 {
11 stdarg 1.7 printf("%s\n", data);
12 stdarg 1.1 return(0);
13     }
14    
15 stdarg 1.7 int server_eof(void *client_data, int idx, int err, const char *errmsg)
16 stdarg 1.1 {
17 stdarg 1.7 if (err && errmsg) printf("Error: %s\n", errmsg);
18 stdarg 1.4 printf("Server closed connection!\n");
19     exit(0);
20 stdarg 1.1 }
21    
22 stdarg 1.7 int server_connect(void *client_data, int idx, const char *peer_ip, int peer_port)
23 stdarg 1.1 {
24 stdarg 1.7 printf("Connected to server (%s %d)!\n", peer_ip, peer_port);
25 stdarg 1.4 sslmode_on(idx, 0); /* 0 means client, 1 means server */
26 stdarg 1.7 zipmode_on(idx);
27     linemode_on(idx);
28 stdarg 1.4 return(0);
29     }
30    
31 stdarg 1.7 static sockbuf_handler_t server_event = {
32     "server",
33     server_connect, server_eof, NULL,
34     server_read, NULL
35 stdarg 1.1 };
36    
37 stdarg 1.7 int stdin_read(void *client_data, int idx, char *data, int len)
38 stdarg 1.1 {
39 stdarg 1.7 data[len] = '\n';
40     sockbuf_write(server_idx, data, len+1);
41 stdarg 1.1 return(0);
42     }
43    
44 stdarg 1.7 int stdin_eof(void *client_data, int idx, int err, const char *errmsg)
45 stdarg 1.1 {
46 stdarg 1.2 sockbuf_delete(idx);
47     return(0);
48 stdarg 1.1 }
49    
50 stdarg 1.7 static sockbuf_handler_t stdin_event = {
51     "stdin",
52     NULL, stdin_eof, NULL,
53     stdin_read, NULL
54 stdarg 1.1 };
55    
56     main (int argc, char *argv[])
57     {
58     int sock, port, stdin_idx;
59 stdarg 1.2 char *host;
60 stdarg 1.1
61 stdarg 1.2 srand(time(0));
62 stdarg 1.1 if (argc < 2) {
63 stdarg 1.2 host = "127.0.0.1";
64     port = 12345;
65     }
66     else if (argc == 2) {
67     host = argv[1];
68     port = 12345;
69     }
70     else {
71     host = argv[1];
72     port = atoi(argv[2]);
73 stdarg 1.1 }
74    
75 stdarg 1.2 printf("Connecting to %s %d\n", host, port);
76 stdarg 1.8 sock = socket_create(host, port, NULL, 0, SOCKET_CLIENT | SOCKET_NONBLOCK);
77 stdarg 1.1 if (sock < 0) {
78     perror("socket_create");
79     return(0);
80     }
81 stdarg 1.7 server_idx = sockbuf_new();
82     sockbuf_set_sock(server_idx, sock, SOCKBUF_CLIENT);
83     sockbuf_set_handler(server_idx, &server_event, NULL);
84 stdarg 1.2 sslmode_init();
85 stdarg 1.1
86 stdarg 1.7 stdin_idx = sockbuf_new();
87 stdarg 1.8 socket_set_nonblock(0, 1);
88 stdarg 1.7 sockbuf_set_sock(stdin_idx, 0, 0);
89     sockbuf_set_handler(stdin_idx, &stdin_event, NULL);
90 stdarg 1.1 linemode_on(stdin_idx);
91     while (1) {
92 stdarg 1.8 sockbuf_update_all(1000);
93 stdarg 1.1 }
94     return(0);
95     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23