working server

This commit is contained in:
Michal Starski 2018-11-29 17:15:56 +01:00
parent 0f15d510ce
commit 6e8b1c95c1

View File

@ -8,6 +8,7 @@
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
typedef struct
{
@ -40,6 +41,7 @@ typedef struct
void Error(int err);
void *dataProcessHandler(void *data);
void *videoHandler(void *arg);
int main(void)
{
@ -72,10 +74,9 @@ int main(void)
pthread_t video_thread;
if (pthread_create(&video_thread, NULL, videoHandler, NULL) < 0)
{
printf("Error");
perror('video_thread');
perror("video_thread");
return -1;
};
}
for (i = 0; i < 2; i++)
{
@ -199,22 +200,22 @@ void Error(int err)
printf("Error! Code: %d\n", err);
}
void *videoHandler()
void *videoHandler(void *arg)
{
printf("video_handler\n");
char video_buffer[1024];
struct stat *movieInfo;
struct stat movieInfo;
int counter = 0; //Counts users that request video data
int VideoSocketTCP;
int new_socket;
int read_movie;
struct sockaddr_in video_address_tcp;
int addrlen = sizeof(video_address_tcp);
struct sockaddr_in video_address_tcp, reciever_address;
int addrlen = sizeof(struct sockaddr_in);
if ((VideoSocketTCP = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("tcp video socket");
exit(1);
return NULL;
}
video_address_tcp.sin_family = AF_INET;
@ -225,33 +226,36 @@ void *videoHandler()
sizeof(video_address_tcp)) < 0)
{
perror("bind failed");
return -1;
return NULL;
}
if (listen(VideoSocketTCP, 3) < 0)
{
perror("listen");
return -1;
return NULL;
}
while (counter < 2)
while (1)
{
if ((new_socket = accept(VideoSocketTCP, (struct sockaddr *)&video_address_tcp,
(socklen_t)&addrlen) < 0))
if ((new_socket = accept(VideoSocketTCP, (struct sockaddr *)&reciever_address,
(socklen_t *)&addrlen)) < 0)
{
perror("accept");
return -1;
return NULL;
}
int movie_open = open("assets/test3.mp4", O_RDONLY);
int movie_file_stat = fstat(movie_open, movieInfo);
send(new_socket, &movieInfo->st_size, sizeof(movieInfo->st_size), 0);
int movie_open = open("assets/test4.mp4", O_RDONLY);
int movie_file_stat = fstat(movie_open, &movieInfo);
send(new_socket, &movieInfo.st_size, sizeof(movieInfo.st_size), 0);
printf("Size sended: %lld\n", movieInfo.st_size);
while ((read_movie = read(movie_open, video_buffer, sizeof(video_buffer))) > 0)
{
printf("%d ", read_movie);
send(new_socket, video_buffer, sizeof(video_buffer), 0);
}
counter++;
}
close(VideoSocketTCP);
close(new_socket);
return 0;
return NULL;
}