Skip to content

Commit 13d364b

Browse files
committed
add test, clean thread
1 parent 1aa2fce commit 13d364b

File tree

5 files changed

+87
-15
lines changed

5 files changed

+87
-15
lines changed

README.md

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
# Linux_stream_server
22
Stream text files using socket
33

4-
RUN:
4+
## GET STARTED:
5+
6+
```
57
make
8+
```
9+
10+
11+
## Example:
612

7-
command
8-
./server.out -k store/ -p .info
9-
./client.out -x1 -o64 -r10 -p .info -f l -s 3135
13+
Server side:
14+
```
15+
./server.out -k store/Pan_Tadeusz -p .info
16+
```
17+
Client side:
18+
```
19+
./client.out -r 10 -o 64 -f l -p .info -x 1 -s <server_pid>
20+
```
1021

22+
## Run options:
1123

24+
```
25+
Klient:
26+
-s <pid> PID procesu serwera
27+
-r <signal> numer sygnału RT, który ma być zastosowany przy komunikacie zwrotnym
28+
-x <int> numer księgi, którą ma odczytać
29+
-o <int> długość interwału pomiędzy komunikatami
30+
-f <char> (fragmentacja tekstu)
31+
z - slowa
32+
l - linia
33+
s - slowa
34+
-p <file> sciezka do tablicy ogloszeniowej
35+
```
36+
```
37+
Serwer:
38+
-k <katalog> katalog, w którym są przechowywane pliki do udostępniania
39+
-p <plik> ścieżka do pliku pełniącego rolę tablicy ogłoszeniowej.
1240
13-
// ***
14-
cntrl+shift+i //code format
41+
```

src/client.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Name: Book streaming (CLIENT)
3+
* @Author: Patryk Nizio
4+
* Date: 2018.02
5+
* https://github.com/Dyzio18/Linux_stream_server
6+
*/
7+
18
#include <stdio.h>
29
#include <unistd.h>
310
#include <sys/time.h>
@@ -16,7 +23,6 @@
1623

1724
#include "common.h"
1825

19-
#define BUF_SIZE 10
2026
#define MAX_PATH 16
2127
#define LINE 256
2228
#define WORD 24
@@ -212,7 +218,7 @@ int main(int argc, char **argv)
212218
memset(&client_address, 0, sizeof(client_address));
213219
client_address.sun_family = AF_UNIX;
214220
char clientAddr_path[MAX_PATH];
215-
char *pathUniq = "cli";
221+
const char *pathUniq = "cli";
216222
snprintf(clientAddr_path, sizeof(char) * (MAX_PATH + 5), "%s%s", SV_SOCK_PATH, pathUniq);
217223
strcpy(client_address.sun_path, clientAddr_path);
218224
client_address.sun_path[0] = 0; // Abstract namespace

src/common.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
void helpDisplay_client()
1616
{
17-
printf("\nKlient:\n\t-s <pid> PID procesu serwera\n\t-r <sygnał> numer sygnału RT, który ma być zastosowany przy komunikacie zwrotnym\n\t-x <nr> numer księgi, którą ma odczytać\n\t-p <czas> długość interwału pomiędzy komunikatami\n\n\n");
17+
printf("\nKlient:\n\t-s <pid> PID procesu serwera\n\t-r <signal> numer sygnału RT, który ma być zastosowany przy komunikacie zwrotnym\n\t-x <int> numer księgi, którą ma odczytać\n\t-o <int> długość interwału pomiędzy komunikatami\n\t-f <char> (fragmentacja tekstu)\n\t\t z - slowa\n\t\t l - linia \n\t\t s - slowa\n\t-p <file> sciezka do tablicy ogloszeniowej\n\n\n");
1818
}
1919

2020
/**

src/server.c

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Name: Book streaming (SERVER)
3+
* @Author: Patryk Nizio
4+
* Date: 2018.02
5+
* https://github.com/Dyzio18/Linux_stream_server
6+
*/
7+
18
#include <stdio.h>
29
#include <unistd.h>
310
#include <sys/time.h>
@@ -29,6 +36,7 @@ struct threadData
2936
char fragmentation;
3037
int interval;
3138
char *path;
39+
pthread_t client_thread;
3240
};
3341
int nsleep(int time);
3442

@@ -61,6 +69,11 @@ void *clientThread_handler(void *thread_arg)
6169
char *source = NULL;
6270
FILE *fp = fopen(sourcePath, "r");
6371

72+
if (fp == NULL){
73+
snprintf(sourcePath, sizeof(sourcePath), "%s/%d.txt", catalog, myData->book);
74+
fp = fopen(sourcePath, "r");
75+
}
76+
6477
long bufsize;
6578
if (fp != NULL)
6679
{
@@ -98,6 +111,7 @@ void *clientThread_handler(void *thread_arg)
98111
else
99112
{
100113
printf("\nResources not found...\n ");
114+
pthread_cancel(myData->client_thread);
101115
pthread_exit(NULL);
102116
}
103117

@@ -109,7 +123,7 @@ void *clientThread_handler(void *thread_arg)
109123
int socket_fd;
110124
struct sockaddr_un server_address, client_address;
111125
int bytes_received = 0;
112-
int bytes_sent = 0;
126+
//int bytes_sent = 0;
113127
socklen_t address_length = sizeof(struct sockaddr_un);
114128

115129
if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
@@ -157,7 +171,7 @@ void *clientThread_handler(void *thread_arg)
157171
// CHECK FIRST MSG WITH CLIENT PID
158172
if (INITIALIZATION_FLAG == 0)
159173
{
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);
161175
if (INITIALIZATION_FLAG == myData->clientPID)
162176
{
163177
printf("\n\nCLIENT with PID %d authorized\n", INITIALIZATION_FLAG);
@@ -192,7 +206,7 @@ void *clientThread_handler(void *thread_arg)
192206
lineLen++;
193207
}
194208
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);
196210
bytes_received = recvfrom(socket_fd, &lineBuffCheck, sizeof(char) * LINE, 0, (struct sockaddr *)&client_address, &address_length);
197211
//write(STDOUT_FILENO, lineBuffCheck, sizeof(char) * LINE);
198212
if (!rot13_arr_check(lineBuff, lineBuffCheck, WORD))
@@ -218,7 +232,7 @@ void *clientThread_handler(void *thread_arg)
218232
wordLen++;
219233
}
220234
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);
222236
bytes_received = recvfrom(socket_fd, &wordBuffCheck, sizeof(char) * WORD, 0, (struct sockaddr *)&client_address, &address_length);
223237
//write(STDOUT_FILENO, wordBuff, sizeof(char) * WORD);
224238
if (!rot13_arr_check(wordBuff, wordBuffCheck, WORD))
@@ -231,7 +245,7 @@ void *clientThread_handler(void *thread_arg)
231245
{
232246
/* SEND LETTER */
233247
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);
235249
bytes_received = recvfrom(socket_fd, &msgCheck, sizeof(msgCheck), 0, (struct sockaddr *)&client_address, &address_length);
236250

237251
if (rot13(msg) != msgCheck)
@@ -240,15 +254,20 @@ void *clientThread_handler(void *thread_arg)
240254
CONNECTED = 0;
241255
}
242256
}
257+
if (bytes_received < 1)
258+
{
259+
CONNECTED = 0;
260+
}
243261
}
244262
//write(STDOUT_FILENO, &msg, sizeof(msg));
245263
//printf("\nSEND: %d RECV: %d",bytes_sent, bytes_received);
246264
nsleep(myData->interval);
247265
}
248266

249267
free(source);
250-
close(socket_fd);
268+
close(socket_fd);
251269
pthread_exit(NULL);
270+
pthread_cancel(myData->client_thread);
252271
}
253272

254273
/*****************************************
@@ -302,6 +321,7 @@ void clientRegisterSignal_handler(int signum, siginfo_t *siginfo, void *ptrVoid)
302321
{
303322
// CREATE THREAD
304323
pthread_t client_thread;
324+
currUser[CLIENTS_COUNT].client_thread = client_thread;
305325
if (pthread_create(&client_thread, NULL, clientThread_handler, &currUser[CLIENTS_COUNT++]) < 0)
306326
{
307327
perror("pthread_create");

test.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
server_pid=$1
3+
4+
echo "PID: $server_pid "
5+
# ksiega 12 slowa co 32 sig 12
6+
lxterminal -e ./client.out -r 12 -o 32 -f s -p .info -x 12 -s $server_pid
7+
# ksiega 12 slowa co 64 sig 12
8+
lxterminal -e ./client.out -r 12 -o 64 -f s -p .info -x 12 -s $server_pid
9+
# ksiega 1 linia co 16 sig 8
10+
lxterminal -e ./client.out -r 8 -o 16 -f l -p .info -x 1 -s $server_pid
11+
# ksiega 1 linia co 128 sig 9
12+
lxterminal -e ./client.out -r 9 -o 128 -f l -p .info -x 1 -s $server_pid
13+
14+
sleep 2
15+
# ksiega 2 znaki co 44 sig 9
16+
lxterminal -e ./client.out -r 9 -o 44 -f z -p .info -x 2 -s $server_pid
17+
sleep 1
18+
# ksiega 2 znaki co 4 sig 9
19+
lxterminal -e ./client.out -r 9 -o 4 -f z -p .info -x 2 -s $server_pid

0 commit comments

Comments
 (0)