1
+ /**
2
+ * Name: Book streaming (SERVER)
3
+ * @Author: Patryk Nizio
4
+ * Date: 2018.02
5
+ * https://github.com/Dyzio18/Linux_stream_server
6
+ */
7
+
1
8
#include <stdio.h>
2
9
#include <unistd.h>
3
10
#include <sys/time.h>
@@ -29,6 +36,7 @@ struct threadData
29
36
char fragmentation ;
30
37
int interval ;
31
38
char * path ;
39
+ pthread_t client_thread ;
32
40
};
33
41
int nsleep (int time );
34
42
@@ -61,6 +69,11 @@ void *clientThread_handler(void *thread_arg)
61
69
char * source = NULL ;
62
70
FILE * fp = fopen (sourcePath , "r" );
63
71
72
+ if (fp == NULL ){
73
+ snprintf (sourcePath , sizeof (sourcePath ), "%s/%d.txt" , catalog , myData -> book );
74
+ fp = fopen (sourcePath , "r" );
75
+ }
76
+
64
77
long bufsize ;
65
78
if (fp != NULL )
66
79
{
@@ -98,6 +111,7 @@ void *clientThread_handler(void *thread_arg)
98
111
else
99
112
{
100
113
printf ("\nResources not found...\n " );
114
+ pthread_cancel (myData -> client_thread );
101
115
pthread_exit (NULL );
102
116
}
103
117
@@ -109,7 +123,7 @@ void *clientThread_handler(void *thread_arg)
109
123
int socket_fd ;
110
124
struct sockaddr_un server_address , client_address ;
111
125
int bytes_received = 0 ;
112
- int bytes_sent = 0 ;
126
+ // int bytes_sent = 0;
113
127
socklen_t address_length = sizeof (struct sockaddr_un );
114
128
115
129
if ((socket_fd = socket (AF_UNIX , SOCK_DGRAM , 0 )) < 0 )
@@ -157,7 +171,7 @@ void *clientThread_handler(void *thread_arg)
157
171
// CHECK FIRST MSG WITH CLIENT PID
158
172
if (INITIALIZATION_FLAG == 0 )
159
173
{
160
- bytes_received = recvfrom (socket_fd , & INITIALIZATION_FLAG , sizeof (INITIALIZATION_FLAG ), 0 , (struct sockaddr * )& client_address , & address_length );
174
+ recvfrom (socket_fd , & INITIALIZATION_FLAG , sizeof (INITIALIZATION_FLAG ), 0 , (struct sockaddr * )& client_address , & address_length );
161
175
if (INITIALIZATION_FLAG == myData -> clientPID )
162
176
{
163
177
printf ("\n\nCLIENT with PID %d authorized\n" , INITIALIZATION_FLAG );
@@ -192,7 +206,7 @@ void *clientThread_handler(void *thread_arg)
192
206
lineLen ++ ;
193
207
}
194
208
lineBuff [lineLen + 1 ] = '\0' ;
195
- bytes_sent = sendto (socket_fd , & lineBuff , sizeof (char ) * LINE , 0 , (struct sockaddr * )& client_address , address_length );
209
+ sendto (socket_fd , & lineBuff , sizeof (char ) * LINE , 0 , (struct sockaddr * )& client_address , address_length );
196
210
bytes_received = recvfrom (socket_fd , & lineBuffCheck , sizeof (char ) * LINE , 0 , (struct sockaddr * )& client_address , & address_length );
197
211
//write(STDOUT_FILENO, lineBuffCheck, sizeof(char) * LINE);
198
212
if (!rot13_arr_check (lineBuff , lineBuffCheck , WORD ))
@@ -218,7 +232,7 @@ void *clientThread_handler(void *thread_arg)
218
232
wordLen ++ ;
219
233
}
220
234
wordBuff [wordLen + 1 ] = '\0' ;
221
- bytes_sent = sendto (socket_fd , & wordBuff , sizeof (char ) * WORD , 0 , (struct sockaddr * )& client_address , address_length );
235
+ sendto (socket_fd , & wordBuff , sizeof (char ) * WORD , 0 , (struct sockaddr * )& client_address , address_length );
222
236
bytes_received = recvfrom (socket_fd , & wordBuffCheck , sizeof (char ) * WORD , 0 , (struct sockaddr * )& client_address , & address_length );
223
237
//write(STDOUT_FILENO, wordBuff, sizeof(char) * WORD);
224
238
if (!rot13_arr_check (wordBuff , wordBuffCheck , WORD ))
@@ -231,7 +245,7 @@ void *clientThread_handler(void *thread_arg)
231
245
{
232
246
/* SEND LETTER */
233
247
msg = source [i ++ ];
234
- bytes_sent = sendto (socket_fd , & msg , sizeof (msg ), 0 , (struct sockaddr * )& client_address , address_length );
248
+ sendto (socket_fd , & msg , sizeof (msg ), 0 , (struct sockaddr * )& client_address , address_length );
235
249
bytes_received = recvfrom (socket_fd , & msgCheck , sizeof (msgCheck ), 0 , (struct sockaddr * )& client_address , & address_length );
236
250
237
251
if (rot13 (msg ) != msgCheck )
@@ -240,15 +254,20 @@ void *clientThread_handler(void *thread_arg)
240
254
CONNECTED = 0 ;
241
255
}
242
256
}
257
+ if (bytes_received < 1 )
258
+ {
259
+ CONNECTED = 0 ;
260
+ }
243
261
}
244
262
//write(STDOUT_FILENO, &msg, sizeof(msg));
245
263
//printf("\nSEND: %d RECV: %d",bytes_sent, bytes_received);
246
264
nsleep (myData -> interval );
247
265
}
248
266
249
267
free (source );
250
- close (socket_fd );
268
+ close (socket_fd );
251
269
pthread_exit (NULL );
270
+ pthread_cancel (myData -> client_thread );
252
271
}
253
272
254
273
/*****************************************
@@ -302,6 +321,7 @@ void clientRegisterSignal_handler(int signum, siginfo_t *siginfo, void *ptrVoid)
302
321
{
303
322
// CREATE THREAD
304
323
pthread_t client_thread ;
324
+ currUser [CLIENTS_COUNT ].client_thread = client_thread ;
305
325
if (pthread_create (& client_thread , NULL , clientThread_handler , & currUser [CLIENTS_COUNT ++ ]) < 0 )
306
326
{
307
327
perror ("pthread_create" );
0 commit comments