Server GET_ALL_FILES, klient c(send.c), example.jpg tylko zeby lista byla dluzsza

This commit is contained in:
s449273 2019-11-25 12:54:10 +01:00
parent c0591d87e4
commit 9df7d6f6b3
4 changed files with 186 additions and 4 deletions

4
server/.gitignore vendored
View File

@ -1 +1,3 @@
server
server
send
a.out

0
server/example.jpg Normal file
View File

111
server/send.c Normal file
View File

@ -0,0 +1,111 @@
#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)
{
char odbior[10];
struct sockaddr_in endpoint;
int sdsocket, addrlen, sdconnection;
const int port = 7332;
const char ip[512] = "192.168.0.43";
char buff[512];
char eee[1];
struct dirent **namelist;
int n;
char wiadomosc[512];
char mess[512];
endpoint.sin_family = AF_INET;
endpoint.sin_port = htons(port);
endpoint.sin_addr.s_addr = inet_addr(ip);
addrlen = sizeof(struct sockaddr_in);
while (1)
{
printf("wpisz 11 aby nawiazac polaczenie\nwpisz 22 aby moc napisac polecenie\n");
scanf("%s", mess);
if (strncmp("11", mess, 2) == 0)
{
printf("Realizowany jest connect\n");
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
bzero(odbior, 512);
recv(sdsocket, odbior, 10, 0);
printf("Wiadomosc tresc: %s\n", odbior);
}
else if (strncmp("22", mess, 2) == 0)
{
printf("Wpisz zapytanie do serwera\n");
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
scanf("%s", buff);
send(sdsocket, buff, 512, 0);
close(sdsocket);
//////////////////////////////////
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
bzero(odbior, 512);
recv(sdsocket, odbior, 512, 0);
printf("%s\n", odbior);
}else{
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
return 0;
}
scanf("%s", buff);
send(sdsocket, buff, 512, 0);
close(sdsocket);
}
}
return 0;
}

View File

@ -7,12 +7,17 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
int main(int argc, char **argv)
{
struct sockaddr_in myaddr, endpoint;
int sdsocket, sdconnection, addrlen, received;
const int port = 7332;
struct sockaddr_in myaddr, endpoint;
int sdsocket, sdconnection, addrlen, sdconnection2, n;
char odbior[512];
struct dirent **namelist;
char name_file[512];
char names_files[512];
sdsocket = socket(AF_INET, SOCK_STREAM, 0);
addrlen = sizeof(struct sockaddr_in);
@ -38,11 +43,75 @@ int main(int argc, char **argv)
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
printf("Connected\n");
send(sdconnection, "CONNECT", 7, 0);
close(sdconnection);
break;
}
while (1)
{
while ((sdconnection =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
bzero(odbior, 512);
recv(sdconnection, odbior, 512, 0);
close(sdconnection);
if (strncmp("GET_ALL_FILES", odbior, 13) == 0)
{
printf("Sending list of names files\n");
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);
while ((sdconnection2 =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
send(sdconnection2, names_files, 512, 0);
close(sdconnection2);
break;
}
}
else
{
printf("Wrong query\n");
while ((sdconnection2 =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
send(sdconnection2, "Wrong query", 512, 0);
close(sdconnection2);
break;
}
}
}
}
close(sdsocket);
return 0;
}