-
Notifications
You must be signed in to change notification settings - Fork 0
/
confclient.c
99 lines (73 loc) · 1.75 KB
/
confclient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*--------------------------------------------------------------------*/
/* conference client */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#define MAXMSGLEN 1024
extern char * recvtext(int sd);
extern int sendtext(int sd, char *msg);
extern int hooktoserver(char *servhost, ushort servport);
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
int sock;
fd_set rfds;
int retval;
/* check usage */
if (argc != 3) {
fprintf(stderr, "usage : %s <servhost> <servport>\n", argv[0]);
exit(1);
}
/* get hooked on to the server */
sock = hooktoserver(argv[1], atoi(argv[2]));
if (sock == -1) {
perror("Error: ");
exit(1);
}
FD_ZERO(&rfds);
FD_SET(0, &rfds);
FD_SET(sock, &rfds);
retval = sock;
/* keep talking */
while (1) {
FD_ZERO(&rfds);
FD_SET(0, &rfds);
FD_SET(sock, &rfds);
retval = sock;
if (select(retval + 1, &rfds, NULL, NULL, NULL) < 0 ){
perror("Error: select error");
exit(1);
}
if (FD_ISSET(sock, &rfds)) {
char *msg;
msg = recvtext(sock);
if (!msg) {
/* server killed, exit */
fprintf(stderr, "error: server died\n");
exit(1);
}
/* display the message */
printf(">>> %s", msg);
/* free the message */
free(msg);
}
if (FD_ISSET(0, &rfds)/) {
char msg[MAXMSGLEN];
if (!fgets(msg, MAXMSGLEN, stdin))
exit(0);
sendtext(sock, msg);
}
printf(">");
fflush(stdout);
}
return 0;
}
/*--------------------------------------------------------------------*/