Debian Codegraph @

COSMIC/EXAMPLES/talkclient.c

raw
1 /* talkclient.c: this program implements a client
2 * Author: Dr Charles E Campbell, Jr
3 *
4 * Protocol
5 *
6 * server->client
7 * m#...message message from #>0: client-id #==0: server
8 * q tell client to quit
9 *
10 * client->server
11 * m...message message from client
12 * q client is quitting
13 * Q client tells server to quit
14 */
15 #include <stdio.h>
16 #include "xtdio.h"
17 #include "sockets.h"
18
19 /* ------------------------------------------------------------------------
20 * Definitions:
21 */
22 #define BUFSIZE 256
23 #define SERVER "TalkServer"
24 #define PROMPT "Client: "
25
26 /* ------------------------------------------------------------------------
27 * Typedefs:
28 */
29
30 /* ------------------------------------------------------------------------
31 * Local Data Structures:
32 */
33
34 /* ------------------------------------------------------------------------
35 * Global Data:
36 */
37
38 /* ------------------------------------------------------------------------
39 * Explanation:
40 */
41
42 /* ------------------------------------------------------------------------
43 * Prototypes:
44 */
45
46 /* ------------------------------------------------------------------------
47 * Source Code:
48 */
49
50 /* main: */
51 int main(
52 int argc,
53 char **argv)
54 {
55 Socket *client = NULL;
56 char buf[BUFSIZE];
57 int id;
58 int result;
59
60 rdcolor();
61
62 Smaskfdset(fileno(stdin)); /* put stdin into mask */
63 fputs(PROMPT,stdout); /* print prompt */
64 fflush(stdout); /* insure prompt is displayed */
65
66 /* attempt to open client at 1-second intervals */
67 while(1) {
68
69 if(!client) {
70 /* note: if server drops out, this client will attempt to
71 * reconnect at one second intervals
72 */
73 while(!client) {
74 client= Sopen(SERVER,"c");
75 if(Smasktest() > 0) {
76 fgets(buf,BUFSIZE,stdin);
77 srmtrblk(buf);
78 if(!strcmp(buf,"q")) exit(0);
79 }
80 if(!client) sleep(1);
81 }
82 Smaskset(client);
83 }
84
85 Smaskwait();
86
87 result= Stest(client);
88 if(result > 0) {
89 if(!Sgets(buf,BUFSIZE,client)) {
90 error(XTDIO_WARNING,"error on Sgets\n");
91 Smaskunset(client);
92 Sclose(client);
93 client= NULL;
94 }
95
96 switch(buf[0]) {
97
98 case 'm': /* m# message... */
99 sscanf(buf+1,"%d",&id);
100 if(id == 0) printf("\nserver%s\n",buf+2); /* print message from server */
101 else printf("\nclient%s\n",buf+1); /* print message from another client */
102 fputs(PROMPT,stdout); /* print prompt */
103 fflush(stdout); /* insure prompt is displayed */
104 break;
105
106 case 'q': /* server told this client to quit */
107 goto enditnow;
108
109 default:
110 error(XTDIO_WARNING,"protocol violation, I don't know what to do with <%s%s%s>\n",
111 YELLOW,
112 sprt(buf),
113 GREEN);
114 break;
115 }
116 }
117
118 else if(result < 0) {
119 error(XTDIO_WARNING,"error on Stest\n");
120 Smaskunset(client);
121 Sclose(client);
122 client= NULL;
123 }
124
125 else {
126 fgets(buf,BUFSIZE,stdin);
127 srmtrblk(buf); /* remove trailing white space */
128
129 fputs(PROMPT,stdout); /* print prompt */
130 fflush(stdout); /* insure prompt is displayed */
131
132 if(!strcmp(buf,"q")) {
133 Sputs("q",client);
134 }
135 else if(!strcmp(buf,"Q")) {
136 Sputs("Q",client);
137 goto enditnow;
138 }
139 else {
140 Sprintf(client,"m%s",buf);
141 }
142 }
143 }
144
145 /* that'suh that'sall folks! */
146 enditnow:
147 if(client) {
148 Smaskunset(client);
149 Sclose(client);
150 client= NULL;
151 }
152
153 return 0;
154 }
155
156 /* --------------------------------------------------------------------- */