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_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