/[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.4 - (show annotations) (download) (as text)
Sun Mar 3 18:14:14 2002 UTC (18 years, 3 months ago) by stdarg
Branch: MAIN
Changes since 1.3: +28 -26 lines
File MIME type: text/x-chdr
*** empty log message ***

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 static sockbuf_event_t server_event = {
28 (Function) 4,
29 (Function) "server",
30 server_read,
31 NULL,
32 server_eof,
33 server_err
34 };
35
36 int server_connect(int idx, void *client_data)
37 {
38 printf("Connected to server!\n");
39 sockbuf_set_handler(idx, server_event, NULL);
40 sslmode_on(idx, 0); /* 0 means client, 1 means server */
41 zipmode_on(idx);
42 linemode_on(idx);
43 return(0);
44 }
45
46 static sockbuf_event_t server_connecting_event = {
47 (Function) 4,
48 (Function) "server",
49 NULL,
50 server_connect,
51 NULL,
52 server_err
53 };
54
55 int stdin_read(int idx, sockbuf_iobuf_t *data, void *client_data)
56 {
57 data->data[data->len] = '\n';
58 sockbuf_write(server_idx, data->data, data->len+1);
59 return(0);
60 }
61
62 int stdin_eof(int idx, void *client_data)
63 {
64 sockbuf_delete(idx);
65 return(0);
66 }
67
68 static sockbuf_event_t stdin_event = {
69 (Function) 4,
70 (Function) "stdin",
71 stdin_read,
72 NULL,
73 stdin_eof,
74 NULL
75 };
76
77 main (int argc, char *argv[])
78 {
79 int sock, port, stdin_idx;
80 char *host;
81
82 srand(time(0));
83 if (argc < 2) {
84 host = "127.0.0.1";
85 port = 12345;
86 }
87 else if (argc == 2) {
88 host = argv[1];
89 port = 12345;
90 }
91 else {
92 host = argv[1];
93 port = atoi(argv[2]);
94 }
95
96 printf("Connecting to %s %d\n", host, port);
97 sock = socket_create(host, port, SOCKET_CLIENT);
98 if (sock < 0) {
99 perror("socket_create");
100 return(0);
101 }
102 socket_set_nonblock(sock, 1);
103 socket_set_nonblock(0, 1);
104 server_idx = sockbuf_new(sock, SOCKBUF_CLIENT);
105 sockbuf_set_handler(server_idx, server_connecting_event, NULL);
106 sslmode_init();
107
108 stdin_idx = sockbuf_new(0, 0);
109 sockbuf_set_handler(stdin_idx, stdin_event, NULL);
110 linemode_on(stdin_idx);
111 while (1) {
112 sockbuf_update_all(-1);
113 }
114 return(0);
115 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23