introduce wersion v2 single connect
This commit is contained in:
parent
bc58d89d4d
commit
2d79d2a265
4
server/.gitignore
vendored
4
server/.gitignore
vendored
@ -1,3 +1,5 @@
|
||||
server
|
||||
send
|
||||
a.out
|
||||
a.out
|
||||
serverv2
|
||||
sendv2
|
@ -137,8 +137,6 @@ int main(int argc, char **argv)
|
||||
|
||||
fclose(file);
|
||||
close(sdsocket);
|
||||
|
||||
|
||||
|
||||
goto start;
|
||||
}
|
||||
|
153
server/sendv2.c
Normal file
153
server/sendv2.c
Normal file
@ -0,0 +1,153 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const int port = 7332;
|
||||
const char ip[512] = "192.168.0.43";
|
||||
struct sockaddr_in endpoint;
|
||||
struct dirent **namelist;
|
||||
char buff[512];
|
||||
char name_file[512];
|
||||
int sdsocket, addrlen, n, x, lenbyt;
|
||||
|
||||
endpoint.sin_family = AF_INET;
|
||||
endpoint.sin_port = htons(port);
|
||||
endpoint.sin_addr.s_addr = inet_addr(ip);
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
ssize_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
start:
|
||||
printf("Enter the server query\n");
|
||||
bzero(buff, 512);
|
||||
scanf("%s", buff);
|
||||
|
||||
if (strncmp("CLIENTOFF", buff, 9) == 0)
|
||||
{
|
||||
close(sdsocket);
|
||||
return 0;
|
||||
}
|
||||
else if (strncmp("HELP", buff, 4) == 0)
|
||||
{
|
||||
printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n FTP_namefile\n SEND_namefile\n");
|
||||
goto start;
|
||||
}
|
||||
|
||||
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||
{
|
||||
printf("socket() failed\n");
|
||||
return 1;
|
||||
}
|
||||
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
|
||||
{
|
||||
printf("connect() failed\n");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
printf("CONNECTED\n");
|
||||
|
||||
send(sdsocket, buff, 512, 0);
|
||||
|
||||
if (strncmp("FTP", buff, 3) == 0)
|
||||
{
|
||||
ssize_t total = 0;
|
||||
strncpy(name_file, buff + 4, 509);
|
||||
printf("Name of file :%s\n", name_file);
|
||||
|
||||
FILE *file = fopen(name_file, "wb");
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("fopen() failed");
|
||||
}
|
||||
|
||||
char length[512];
|
||||
recv(sdsocket, length, 512, 0);
|
||||
printf("Rozmiar pliku to: %s\n", length);
|
||||
lenbyt = atoi(length);
|
||||
printf("Rozmiar pliku w intach to: %d\n", lenbyt);
|
||||
char addr[INET_ADDRSTRLEN];
|
||||
printf("Downloading file: %s od %s\n", name_file, inet_ntop(AF_INET, &endpoint.sin_addr.s_addr, addr, INET_ADDRSTRLEN));
|
||||
|
||||
ssize_t n;
|
||||
char buff[4096] = {0};
|
||||
while (total < lenbyt)
|
||||
{
|
||||
n = recv(sdsocket, buff, 4096, 0);
|
||||
total += n;
|
||||
if (n == -1)
|
||||
{
|
||||
printf("recv() failed");
|
||||
}
|
||||
|
||||
if (fwrite(buff, sizeof(char), n, file) != n)
|
||||
{
|
||||
printf("fwrite() failed");
|
||||
}
|
||||
memset(buff, 0, 4096);
|
||||
}
|
||||
|
||||
printf("Downloading succes, numer of bytes = %ld\n", total);
|
||||
|
||||
fclose(file);
|
||||
|
||||
goto start;
|
||||
}
|
||||
if (strncmp("SEND", buff, 4) == 0)
|
||||
{
|
||||
ssize_t count = 0;
|
||||
bzero(name_file, 512);
|
||||
strncpy(name_file, buff + 5, 507);
|
||||
printf("File to send :%s\n", name_file);
|
||||
|
||||
FILE *file = fopen(name_file, "rb");
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("fopen() failed");
|
||||
}
|
||||
|
||||
fseek(file, 0L, SEEK_END);
|
||||
|
||||
long int res = ftell(file);
|
||||
printf("Rozmiar pliku to: %ld\n", res);
|
||||
char length[512];
|
||||
sprintf(length, "%ld", res);
|
||||
send(sdsocket, length, 512, 0);
|
||||
|
||||
rewind(file);
|
||||
|
||||
char package[4096] = {0};
|
||||
while ((x = fread(package, sizeof(char), 4096, file)) > 0)
|
||||
{
|
||||
count += x;
|
||||
|
||||
if (send(sdsocket, package, x, 0) == -1)
|
||||
{
|
||||
printf("send() failed");
|
||||
}
|
||||
memset(package, 0, 4096);
|
||||
}
|
||||
|
||||
printf("Send success, number of bytes: %ld\n", count);
|
||||
|
||||
fclose(file);
|
||||
|
||||
goto start;
|
||||
}
|
||||
|
||||
bzero(buff, 512);
|
||||
recv(sdsocket, buff, 512, 0);
|
||||
printf("%s\n", buff);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -115,9 +115,7 @@ int main(int argc, char **argv)
|
||||
&addrlen)) >= 0)
|
||||
{
|
||||
|
||||
|
||||
|
||||
while ((n = recv(sdconnection2, buff, 4096, 0)) > 0)
|
||||
while ((n = recv(sdconnection2, buff, 4096, 0)) > 0)
|
||||
{
|
||||
total += n;
|
||||
if (n == -1)
|
||||
|
184
server/serverv2.c
Normal file
184
server/serverv2.c
Normal file
@ -0,0 +1,184 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <dirent.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
const int port = 7332;
|
||||
struct sockaddr_in myaddr, endpoint;
|
||||
struct dirent **namelist;
|
||||
char buff[512];
|
||||
char name_file[512];
|
||||
char names_files[512];
|
||||
int sdsocket, sdconnection, addrlen, n, x, lenbyt;
|
||||
|
||||
sdsocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
|
||||
myaddr.sin_family = AF_INET;
|
||||
myaddr.sin_port = htons(port);
|
||||
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if (bind(sdsocket, (struct sockaddr *)&myaddr, addrlen) < 0)
|
||||
{
|
||||
printf("bind() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listen(sdsocket, 10) < 0)
|
||||
{
|
||||
printf("listen() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
start:
|
||||
while ((sdconnection =
|
||||
accept(sdsocket,
|
||||
(struct sockaddr *)&endpoint,
|
||||
&addrlen)) >= 0)
|
||||
{
|
||||
|
||||
printf("CONNECT FROM : %s\n ", inet_ntoa(endpoint.sin_addr));
|
||||
|
||||
bzero(buff, 512);
|
||||
recv(sdconnection, buff, 512, 0);
|
||||
|
||||
if (strncmp("GET_ALL_FILES", buff, 13) == 0)
|
||||
{
|
||||
|
||||
printf("Sending list of names files\n");
|
||||
bzero(names_files, 512);
|
||||
bzero(name_file, 512);
|
||||
|
||||
n = scandir(".", &namelist, NULL, alphasort);
|
||||
if (n == -1)
|
||||
{
|
||||
perror("scandir");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while (n--)
|
||||
{
|
||||
sprintf(name_file, "%s\n", namelist[n]->d_name);
|
||||
strcat(names_files, name_file);
|
||||
|
||||
free(namelist[n]);
|
||||
}
|
||||
free(namelist);
|
||||
|
||||
send(sdconnection, names_files, 512, 0);
|
||||
goto start;
|
||||
}
|
||||
else if (strncmp("SEND", buff, 4) == 0)
|
||||
{
|
||||
ssize_t total = 0;
|
||||
strncpy(name_file, buff + 5, 508);
|
||||
printf("Name of file :%s\n", name_file);
|
||||
|
||||
printf("I'm receving a file: %s\n", buff);
|
||||
|
||||
FILE *file = fopen(name_file, "wb");
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("fopen() failed");
|
||||
}
|
||||
|
||||
char length[512];
|
||||
recv(sdconnection, length, 512, 0);
|
||||
printf("Rozmiar pliku to: %s\n", length);
|
||||
lenbyt = atoi(length);
|
||||
printf("Rozmiar pliku w intach to: %d\n", lenbyt);
|
||||
|
||||
char addr[INET_ADDRSTRLEN];
|
||||
printf("Downloading file: %s od %s\n", name_file, inet_ntop(AF_INET, &endpoint.sin_addr.s_addr, addr, INET_ADDRSTRLEN));
|
||||
|
||||
ssize_t n;
|
||||
|
||||
char buff[4096] = {0};
|
||||
|
||||
while (total < lenbyt)
|
||||
{
|
||||
n = recv(sdconnection, buff, 4096, 0);
|
||||
total += n;
|
||||
if (n == -1)
|
||||
{
|
||||
printf("recv() failed");
|
||||
}
|
||||
|
||||
if (fwrite(buff, sizeof(char), n, file) != n)
|
||||
{
|
||||
printf("fwrite() failed");
|
||||
}
|
||||
memset(buff, 0, 4096);
|
||||
}
|
||||
printf("Downloading succes, numer of bytes = %ld\n", total);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
else if (strncmp("EXIT", buff, 4) == 0)
|
||||
{
|
||||
printf("Server shut down..\n");
|
||||
|
||||
close(sdsocket);
|
||||
return 0;
|
||||
}
|
||||
else if (strncmp("FTP", buff, 3) == 0)
|
||||
{
|
||||
ssize_t count = 0;
|
||||
bzero(name_file, 512);
|
||||
strncpy(name_file, buff + 4, 509);
|
||||
printf("File to send :%s\n", name_file);
|
||||
|
||||
FILE *file = fopen(name_file, "rb");
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("fopen() failed");
|
||||
}
|
||||
fseek(file, 0L, SEEK_END);
|
||||
|
||||
long int res = ftell(file);
|
||||
printf("Rozmiar pliku to: %ld\n", res);
|
||||
char length[512];
|
||||
sprintf(length, "%ld", res);
|
||||
send(sdconnection, length, 512, 0);
|
||||
|
||||
rewind(file);
|
||||
|
||||
char package[4096] = {0};
|
||||
while ((x = fread(package, sizeof(char), 4096, file)) > 0)
|
||||
{
|
||||
count += x;
|
||||
|
||||
if (send(sdconnection, package, x, 0) == -1)
|
||||
{
|
||||
printf("send() failed");
|
||||
}
|
||||
memset(package, 0, 4096);
|
||||
}
|
||||
|
||||
printf("Send success, number of bytes: %ld\n", count);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Wrong query: %s\n", buff);
|
||||
|
||||
send(sdsocket, "Wrong query", 512, 0);
|
||||
goto start;
|
||||
}
|
||||
}
|
||||
}
|
||||
close(sdsocket);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user