-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
243 lines (221 loc) · 5.97 KB
/
server.c
File metadata and controls
243 lines (221 loc) · 5.97 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
char buffer[256];
int sdout,sdin;
struct cliente{
char *username;
int fd_toserver;
int fd_toclient;
char userlen[16];
};
struct cliente clientes[100];
pthread_mutex_t critical;
struct missatge{
int len;
char *txt;
};
//Handler que s'executa al rebre SIGUSR2
void int_handler(){
char buffer[256];
int i=0;
sprintf(buffer,"SIGUSR2 recieved Closing sockets...\n");
write(1,buffer,strlen(buffer));
//Recorre tot vector de clientes xapant els sockets
while(clientes[i].fd_toserver!=0){
shutdown(clientes[i].fd_toserver,0);
shutdown(clientes[i].fd_toclient,1);
close(clientes[i].fd_toserver);
close(clientes[i].fd_toclient);
}
sprintf(buffer,"All clients closed\n");
write(1,buffer,strlen(buffer));
//Xapa sockets del server i finalitza
shutdown(sdin,0);
shutdown(sdout,1);
close(sdin);
close(sdout);
sprintf(buffer,"Server Sockets closed\n");
write(1,buffer,strlen(buffer));
exit(0);
}
//CREACCIÓ DE SOCKETS INICIALS
int createsocket(int port, struct sockaddr_in address){
int x;
x=socket(AF_INET,SOCK_STREAM,0);
if (x==-1) {
sprintf(buffer,"ERROR socket creation port: %d \n",port);
write(1,buffer,strlen(buffer));
return 0;
}else{
sprintf(buffer,"OK SCreation port: %d \n",port);
write(1,buffer,strlen(buffer));
}
if (bind(x,(struct sockaddr*) &address, sizeof(address)) == -1) {
sprintf(buffer,"ERROR Bind port: %d \n",port);
write(1,buffer,strlen(buffer));
return 0;
}else{
sprintf(buffer,"OK Bind port: %d \n",port);
write(1,buffer,strlen(buffer));
}
if(listen(x,2) == -1) {
sprintf(buffer,"ERROR listen port: %d \n",port);
write(1,buffer,strlen(buffer));
return 0;
}else{
sprintf(buffer,"OK Listen port: %d \n",port);
write(1,buffer,strlen(buffer));
}
sprintf(buffer,"------------------------\n");
write(1,buffer,strlen(buffer));
return x;
}
void *read_write (void *asdf){
struct cliente *cliente;
cliente=(struct cliente*)asdf;
int i=0;
char *txt;
char len[32];
char buffer2[270];
sprintf(buffer2,"Thread username: %s Born\n",cliente->username);
write(1,buffer2,strlen(buffer2));
while(1){
//Rep longitud missatge
read(cliente->fd_toserver,len,32);
sprintf(buffer2,"missatge rebut!!\n");
write(1,buffer2,strlen(buffer2));
//COMPROVACIONS
sprintf(buffer2,"len=%s\n",len);
write(1,buffer2,strlen(buffer2));
sprintf(buffer2,"%s said:",cliente->username);
write(1,buffer2,strlen(buffer2));
//Posem mida missatge
txt=(char*)malloc(atoi(len));
read(cliente->fd_toserver,txt,atoi(len));
write(1,txt,atoi(len));
pthread_mutex_lock(&critical);
while(clientes[i].fd_toserver!=0){
if(clientes[i].fd_toclient != cliente->fd_toclient){
usleep(25);
write(clientes[i].fd_toclient,cliente->userlen,strlen(cliente->userlen));
usleep(25);
write(clientes[i].fd_toclient,cliente->username,atoi(cliente->userlen));
usleep(25);
write(clientes[i].fd_toclient,len,32);
usleep(25);
write(clientes[i].fd_toclient,txt,atoi(len));
}
i++;
}
pthread_mutex_unlock(&critical);
free(txt);
i=0;
}
}
int main(int argc, char**argv){
//Llegir els 2 ports per teclat
if (argc!=1){
sprintf(buffer,"ERROR input params\n");
write(1,buffer,strlen(buffer));
return 0;
}
/*
int portin=atoi(argv[1]);
sprintf(buffer,"primer port %d \n",atoi(argv[1]));
write(1,buffer,strlen(buffer));
int portout=atoi(argv[2]);;
sprintf(buffer,"segon port %d \n",atoi(argv[2]));
write(1,buffer,strlen(buffer));
*/
int portin=11005;
int portout=12005;
//Creacio cosetes de signals
struct sigaction hand;
sigset_t mask;
sigemptyset (&mask);
hand.sa_mask=mask;
hand.sa_flags=0;
hand.sa_handler=int_handler;
sigaction(SIGUSR2,&hand,NULL);
//Creacio de les adreces pels 2 sockets: addresin i addresout
int sAddrLen;
int j=0,in,out;
pthread_t id[100];
struct sockaddr_in addressin, addressout, retSin;
memset(&addressin,0,sizeof(addressin));
addressin.sin_family = AF_INET;
addressin.sin_addr.s_addr = INADDR_ANY;
addressin.sin_port = htons(portin);
memset(&addressout,0,sizeof(addressout));
addressout.sin_family = AF_INET;
addressout.sin_addr.s_addr = INADDR_ANY;
addressout.sin_port = htons(portout);
//Creació dels 2 sockets, retorna el sd corresponent a cada canal.
sdin=createsocket(portin,addressin);
if (sdin==0)
{
return 0;
}
sdout=createsocket(portout,addressout);
if (sdout==0)
{
return 0;
}
pthread_mutex_init(&critical,NULL);
sprintf(buffer,"Entrant bucle accepts\n");
write(1,buffer,strlen(buffer));
while(1){
sprintf(buffer,"------------------------\n");
write(1,buffer,strlen(buffer));
sprintf(buffer,"Waiting IN...\n");
write(1,buffer,strlen(buffer));
in=accept(sdin, (struct sockaddr *)&retSin, &sAddrLen);
if(in==-1){
sprintf(buffer,"IN accept error\n");
write(1,buffer,strlen(buffer));
}else{
sprintf(buffer,"Accepted IN\n");
write(1,buffer,strlen(buffer));
sprintf(buffer,"Waiting OUT...\n");
write(1,buffer,strlen(buffer));
out=accept(sdout, (struct sockaddr *)&retSin, &sAddrLen);
if(out==-1){
sprintf(buffer,"OUT accept error\n");
write(1,buffer,strlen(buffer));
}else{
sprintf(buffer,"Accepted OUT:\n");
write(1,buffer,strlen(buffer));
pthread_mutex_lock(&critical);
//Rep mida UserName
read(in,clientes[j].userlen,16);
//Assigna mida a username
clientes[j].username=(char*)malloc(atoi(clientes[j].userlen));
//Rep Username
read(in,clientes[j].username,atoi(clientes[j].userlen));
//Assignem fd
clientes[j].fd_toclient=out;
clientes[j].fd_toserver=in;
sprintf(buffer,"Creating thread...\n");
write(1,buffer,strlen(buffer));
//Crea thread read-write
pthread_create(&id[j],NULL,read_write,(void *)&clientes[j]);
pthread_mutex_unlock(&critical);
j++;
}
}
}
return 0;
}