consolidate all repos to one for archive
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
all: build run
|
||||
|
||||
debug: buildd run clean
|
||||
|
||||
zip:
|
||||
zip naloga.zip main.c Makefile
|
||||
|
||||
build: main.c
|
||||
gcc -m32 -O3 main.c -o demo_client
|
||||
|
||||
buildd: main.c
|
||||
gcc -g -m32 main.c -o demo_client -fsanitize=address -fno-omit-frame-pointer
|
||||
|
||||
run:
|
||||
./demo_client localhost 8003 test1
|
||||
|
||||
clean:
|
||||
rm -f demo_client main
|
||||
|
||||
chm:
|
||||
chmod 755 script.sh
|
241
semester_4/namenska_programska_oprema/Naloga_2/Client/main.c
Normal file
241
semester_4/namenska_programska_oprema/Naloga_2/Client/main.c
Normal file
@@ -0,0 +1,241 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#define MAXDATASIZE 1024
|
||||
#define DATOTEKA 2147483648
|
||||
#define MAPA 1073741824
|
||||
#define KONEC 0
|
||||
|
||||
typedef struct Data
|
||||
{
|
||||
uint32_t size;
|
||||
uint32_t type;
|
||||
char name[128];
|
||||
} Data;
|
||||
|
||||
int isDir(const char *path)
|
||||
{
|
||||
struct stat path_stat;
|
||||
stat(path, &path_stat);
|
||||
return S_ISDIR(path_stat.st_mode);
|
||||
}
|
||||
|
||||
void sendFile(int sockfd, const char *fileName)
|
||||
{
|
||||
struct stat st;
|
||||
stat(fileName, &st);
|
||||
|
||||
Data dataS;
|
||||
dataS.size = st.st_size;
|
||||
dataS.type = DATOTEKA;
|
||||
memset(dataS.name, '\0', sizeof(dataS.name));
|
||||
strcpy(dataS.name, fileName);
|
||||
|
||||
if (send(sockfd, &dataS, sizeof(dataS), 0) < 0)
|
||||
{
|
||||
perror("send");
|
||||
return;
|
||||
}
|
||||
|
||||
FILE *ptr = fopen(fileName, "rb");
|
||||
if (ptr == NULL)
|
||||
{
|
||||
perror("File");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t buffer[MAXDATASIZE];
|
||||
size_t bytesRead = 0;
|
||||
while (bytesRead < st.st_size)
|
||||
{
|
||||
size_t read = fread(buffer, 1, sizeof(buffer), ptr);
|
||||
|
||||
if (send(sockfd, buffer, read, 0) < 0)
|
||||
{
|
||||
perror("send");
|
||||
}
|
||||
bytesRead += read;
|
||||
}
|
||||
|
||||
fclose(ptr);
|
||||
|
||||
printf("USPEL\tZBIRKA\t%s\t%d\n", dataS.name, dataS.size);
|
||||
}
|
||||
|
||||
void sendDir(int sockfd, const char *folderName)
|
||||
{
|
||||
Data dataToSend;
|
||||
dataToSend.size = 0;
|
||||
dataToSend.type = MAPA;
|
||||
strcpy(dataToSend.name, folderName);
|
||||
|
||||
if (send(sockfd, &dataToSend, sizeof(dataToSend), 0) < 0)
|
||||
{
|
||||
perror("send");
|
||||
return;
|
||||
}
|
||||
printf("USPEL\tIMENIK\t%s\t%d\n", dataToSend.name, dataToSend.size);
|
||||
}
|
||||
|
||||
void reciveConferm(int sockfd)
|
||||
{
|
||||
uint8_t ret;
|
||||
if ((recv(sockfd, &ret, sizeof(ret), 0)) == -1)
|
||||
{
|
||||
perror("recv");
|
||||
}
|
||||
}
|
||||
|
||||
void sendEnd(int sockfd)
|
||||
{
|
||||
Data dataToSend;
|
||||
dataToSend.size = 0;
|
||||
dataToSend.type = KONEC;
|
||||
strcpy(dataToSend.name, "");
|
||||
|
||||
if (send(sockfd, &dataToSend, sizeof(dataToSend), 0) < 0)
|
||||
{
|
||||
perror("send");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void sendFullFile(int sockfd, char *input)
|
||||
{
|
||||
//nik/nik/nik.txt
|
||||
char *ptr = strtok(input, "/");
|
||||
char fileName[128];
|
||||
memset(fileName, '\0', sizeof(fileName));
|
||||
while (ptr != NULL)
|
||||
{
|
||||
strcat(fileName, ptr);
|
||||
|
||||
if (isDir(fileName))
|
||||
{
|
||||
sendDir(sockfd, fileName);
|
||||
strcat(fileName, "/");
|
||||
}
|
||||
else
|
||||
{
|
||||
sendFile(sockfd, fileName);
|
||||
}
|
||||
|
||||
reciveConferm(sockfd);
|
||||
|
||||
ptr = strtok(NULL, "/");
|
||||
}
|
||||
}
|
||||
|
||||
void sendFullDir(int sockfd, char *input)
|
||||
{
|
||||
char fileName[128];
|
||||
memset(fileName, '\0', sizeof(fileName));
|
||||
//nik/test
|
||||
char *ptr = strtok(input, "/");
|
||||
while (ptr != NULL)
|
||||
{
|
||||
strcat(fileName, ptr);
|
||||
|
||||
if (isDir(fileName))
|
||||
{
|
||||
sendDir(sockfd, fileName);
|
||||
strcat(fileName, "/");
|
||||
}
|
||||
reciveConferm(sockfd);
|
||||
|
||||
ptr = strtok(NULL, "/");
|
||||
}
|
||||
|
||||
struct dirent *files;
|
||||
DIR *dir = opendir(fileName);
|
||||
if (dir == NULL)
|
||||
{
|
||||
printf("Directory cannot be opened!");
|
||||
return;
|
||||
}
|
||||
//nik/test/nik.txt
|
||||
char srcName[128];
|
||||
while ((files = readdir(dir)) != NULL)
|
||||
{
|
||||
|
||||
if(strncmp( "..", files->d_name, 2) > -1) continue;
|
||||
if(strncmp( ".", files->d_name, 1) > -1) continue;
|
||||
strcpy(srcName, fileName);
|
||||
strcat(srcName, files->d_name);
|
||||
if (!isDir(srcName))
|
||||
{
|
||||
sendFile(sockfd, srcName);
|
||||
reciveConferm(sockfd);
|
||||
}
|
||||
else
|
||||
{
|
||||
sendFullDir(sockfd, srcName);
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
//write(0, "Uporaba: TCPtimec ime vrata datoteka\n\0", 29);
|
||||
printf("Uporaba: TCPtimec ime vrata datoteka\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int sockfd; // socekt file descriptor, new file descriptor
|
||||
struct hostent *he; // pointer to the structure hostent (returned by gethostbyname)
|
||||
struct sockaddr_in their_addr; // server address
|
||||
|
||||
// get the server IP address
|
||||
if ((he = gethostbyname(argv[1])) == NULL)
|
||||
{
|
||||
herror("gethostbyname"); // prints string + value of h_error variable [HOST_NOT_FOUND || NO_ADDRESS or NO_DATA || NO_RECOVERY || TRY_AGAIN]
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// create socket
|
||||
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
|
||||
{
|
||||
perror("socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
their_addr.sin_family = AF_INET; // family to Address Family InterNET
|
||||
their_addr.sin_port = htons(atoi(argv[2])); // get server's port number - should be checked for input errors (not a number, etc.)
|
||||
their_addr.sin_addr = *((struct in_addr *)he->h_addr); // server's IP address from hostent structure pointed to by he variable...
|
||||
memset(&(their_addr.sin_zero), '\0', 8); // for conversion between structures only, a trick to ensure pointer casting...
|
||||
|
||||
// connect to the server... (no bind necessary as we are connecting to remote host... Kernel will find a free port for us and will bind it to sockfd)
|
||||
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
perror("connect");
|
||||
exit(1);
|
||||
}
|
||||
//nik
|
||||
if (isDir(argv[3]))
|
||||
{
|
||||
sendFullDir(sockfd, argv[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
sendFullFile(sockfd, argv[3]);
|
||||
}
|
||||
|
||||
sendEnd(sockfd);
|
||||
// close socket
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 9.3 KiB |
@@ -0,0 +1 @@
|
||||
test2
|
Binary file not shown.
After Width: | Height: | Size: 9.3 KiB |
@@ -0,0 +1 @@
|
||||
test2
|
@@ -0,0 +1,21 @@
|
||||
all: build run
|
||||
|
||||
debug: buildd run clean
|
||||
|
||||
zip:
|
||||
zip naloga.zip main.c Makefile
|
||||
|
||||
build: main.c
|
||||
gcc -m32 -O3 main.c -o demo_streznik -pthread
|
||||
|
||||
buildd: main.c
|
||||
gcc -g -m32 main.c -o demo_streznik -fsanitize=address -fno-omit-frame-pointer
|
||||
|
||||
run:
|
||||
./demo_streznik 8003
|
||||
|
||||
clean:
|
||||
rm -f demo_streznik
|
||||
|
||||
cleanAll:
|
||||
rm -f img.jpg text.txt Video1.mp4 Video2.mp4
|
160
semester_4/namenska_programska_oprema/Naloga_2/Server/main.c
Normal file
160
semester_4/namenska_programska_oprema/Naloga_2/Server/main.c
Normal file
@@ -0,0 +1,160 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAXDATASIZE 1024
|
||||
#define DATOTEKA 2
|
||||
#define MAPA 1
|
||||
#define KONEC 0
|
||||
|
||||
typedef struct Data
|
||||
{
|
||||
uint32_t sizeOfFile;
|
||||
uint32_t type;
|
||||
char fileName[128];
|
||||
} Data;
|
||||
|
||||
void reciveFile(int newfd, Data data)
|
||||
{
|
||||
uint8_t buffer[MAXDATASIZE];
|
||||
int numbytes = 0;
|
||||
FILE *wptr = fopen(data.fileName, "wb");
|
||||
size_t bytesRecived = 0;
|
||||
while (bytesRecived < data.sizeOfFile)
|
||||
{
|
||||
if ((numbytes = recv(newfd, buffer, MAXDATASIZE, 0)) == -1)
|
||||
{
|
||||
perror("recv");
|
||||
return;
|
||||
}
|
||||
fwrite(buffer, numbytes, 1, wptr);
|
||||
bytesRecived += numbytes;
|
||||
}
|
||||
fclose(wptr);
|
||||
}
|
||||
|
||||
void sendConf(int newfd)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
if (send(newfd, &ret, sizeof(ret), 0) < 0)
|
||||
{
|
||||
perror("send1");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void reciveFolder(Data data)
|
||||
{
|
||||
mkdir(data.fileName, 0777);
|
||||
}
|
||||
|
||||
void *recive(void *fd)
|
||||
{
|
||||
int newfd = *(int *)fd;
|
||||
int exit = 0;
|
||||
while (1)
|
||||
{
|
||||
|
||||
Data recivedData;
|
||||
if ((recv(newfd, &recivedData, sizeof(recivedData), 0)) == -1)
|
||||
{
|
||||
perror("recv");
|
||||
// exit(1);
|
||||
}
|
||||
|
||||
switch (recivedData.type)
|
||||
{
|
||||
case DATOTEKA:
|
||||
reciveFile(newfd, recivedData);
|
||||
printf("USPEL\tZBIRKA\t%s\t%d\n", recivedData.fileName, recivedData.sizeOfFile);
|
||||
break;
|
||||
case MAPA:
|
||||
reciveFolder(recivedData);
|
||||
printf("USPEL\tIMENIK\t%s\t%d\n", recivedData.fileName, recivedData.sizeOfFile);
|
||||
break;
|
||||
case KONEC:
|
||||
close(newfd);
|
||||
exit = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (exit)
|
||||
break;
|
||||
sendConf(newfd);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sockfd; // socekt file descriptor, new file descriptor
|
||||
socklen_t length; // socket length (length of clinet address)
|
||||
struct sockaddr_in saddr, caddr; // server address, client address
|
||||
time_t itime; // time format
|
||||
char *tstr; // var holding the string to be send via TCP
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("Uporaba: demo_client vrata (vrata 0-1024 so rezervirana za jedro)\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// create socket
|
||||
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
||||
{
|
||||
perror("socket");
|
||||
}
|
||||
|
||||
saddr.sin_family = AF_INET; // IPv4
|
||||
saddr.sin_addr.s_addr = INADDR_ANY; // localhost
|
||||
saddr.sin_port = htons(atoi(argv[1])); // port converted from ascii to integer
|
||||
|
||||
// binds the socket file description to the actual port (similar to open)
|
||||
if (bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
|
||||
{
|
||||
perror("bind");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// start listening on the given port
|
||||
if (listen(sockfd, 0) < 0)
|
||||
{
|
||||
perror("listen");
|
||||
return 0;
|
||||
}
|
||||
|
||||
length = sizeof(caddr); // length of client address
|
||||
|
||||
pthread_t thread_id[10];
|
||||
int newfd[10];
|
||||
uint8_t kaz = 0;
|
||||
while (1)
|
||||
{
|
||||
|
||||
if ((newfd[kaz] = accept(sockfd, (struct sockaddr *)&caddr, &length)) < 0)
|
||||
{
|
||||
perror("accept");
|
||||
return 0;
|
||||
}
|
||||
recive(newfd);
|
||||
pthread_create(&thread_id[kaz], NULL, recive, &newfd[kaz]);
|
||||
// recive(newfd[kaz]);
|
||||
kaz++;
|
||||
kaz = kaz % 10;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
{
|
||||
pthread_join(thread_id[i], NULL);
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user