-
Notifications
You must be signed in to change notification settings - Fork 0
/
confserver.c
179 lines (143 loc) · 4.03 KB
/
confserver.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*--------------------------------------------------------------------*/
/* conference server */
#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>
#include <unistd.h>
extern char * recvtext(int sd);
extern int sendtext(int sd, char *msg);
extern int startserver();
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
int fd_isset(int fd, fd_set *fsp) {
return FD_ISSET(fd, fsp);
}
/* main routine */
int main(int argc, char *argv[]) {
int servsock; /* server socket descriptor */
fd_set livesdset; /* set of live client sockets */
int livesdmax; /* largest live client socket descriptor */
/* check usage */
if (argc != 1) {
fprintf(stderr, "usage : %s\n", argv[0]);
exit(1);
}
/* get ready to receive requests */
servsock = startserver();
if (servsock == -1) {
perror("Error on starting server: ");
exit(1);
}
/*
FILL HERE:
init the set of live clients
*/
FD_ZERO(&livesdset);
FD_SET(servsock, &livesdset);
livesdmax = servsock;
fd_set rst;
/* receive requests and process them */
while (1) {
int frsock; /* loop variable */
int mflag = 0 ;
FD_ZERO(&rst);
rst = livesdset;
FD_SET(servsock, &rst);
//rst = livesdset;
if (select(livesdmax + 1, &rst, NULL, NULL, NULL) < 0) {
perror("Error: select error-s");
exit(1);
}
/* look for messages from live clients */
for (frsock = 3; frsock <= livesdmax; frsock++) {
/* skip the listen socket */
/* this case is covered separately */
if (frsock == servsock)
continue;
if ( FD_ISSET(frsock, &rst)!=0 ) {
char * clienthost; /* host name of the client */
ushort clientport; /* port number of the client */
struct hostent *chost;
struct sockaddr_in client;
socklen_t cLen = sizeof(client);
getpeername(frsock, (struct sockaddr *)&client , &cLen);
clienthost = gethostbyaddr(&(client.sin_addr.s_addr), sizeof(client.sin_addr.s_addr), AF_INET)->h_name;
clientport = ntohs(client.sin_port);
char* msg;
/* read the message */
msg = recvtext(frsock);
if (!msg) {
/* disconnect from client */
printf("admin: disconnect from '%s(%hu)'\n", clienthost,
clientport);
FD_CLR(frsock, &rst);
FD_CLR(frsock, &livesdset);
mflag = 1;
if(frsock == livesdmax){
int i;
int temp=0;
for(i=servsock; i<livesdmax; i++){
if(FD_ISSET(i,&livesdset) && i>temp){
temp = i;
}
}
livesdmax=temp;
}
/* close the socket */
close(frsock);
} else {
printf("%s(%hu): %s", clienthost, clientport, msg);
int j;
for(j=servsock+1; j<=livesdmax+1; j++){
if(j!=frsock && (FD_ISSET(j, &livesdset)!=0)){
sendtext(j,msg);
//printf("send to %d, %s\n", j, msg);
mflag = 1;
}
}
/* display the message */
//printf("%s(%hu): %s", clienthost, clientport, msg);
mflag = 1;
/* free the message */
free(msg);
}
}
}
/* look for connect requests */
if (mflag == 0 && FD_ISSET(frsock, &livesdset)==0 ) {
struct sockaddr_in newClient;
socklen_t leng = sizeof(newClient);
int csd;
csd = accept(servsock, (void *)&newClient, &leng);
/* if accept is fine? */
if (csd != -1) {
char * clienthost; /* host name of the client */
ushort clientport; /* port number of the client */
if(csd > livesdmax){
livesdmax = csd;
}
struct hostent *chost;
int cLen;
chost = gethostbyaddr(&(newClient.sin_addr.s_addr), sizeof(newClient.sin_addr), AF_INET);
clienthost = chost->h_name;
clientport = ntohs(newClient.sin_port);
printf("admin: connect from '%s' at '%hu'\n", clienthost,
clientport);
FD_SET(frsock, &livesdset);
} else {
perror("accept");
exit(0);
}
}
}
return 0;
}
/*--------------------------------------------------------------------*/