ZSIK_Projekt/server/send.c

129 lines
3.2 KiB
C

#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.1.30";
char buff[512];
char eee[1];
struct dirent **namelist;
int n;
char wiadomosc[512];
char mess[512];
char name_file[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)
{
start:
printf("Wpisz zapytanie do serwera\n");
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");
goto start;
}
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;
}
else
printf("CONNECTED\n");
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;
}
if (strncmp("FTP", buff, 3) == 0)
{
ssize_t total = 0;
strncpy(name_file, buff + 4, 509);
printf("dlugość nazwy:%ld\n", strlen(name_file));
printf("Plik do wyslania :%s\n", name_file);
FILE *file = fopen(name_file, "wb");
if (file == NULL)
{
printf("Nie udalo sie otowrzyc pliku");
}
char addr[INET_ADDRSTRLEN];
printf("Pobieranie pliku: %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 ((n = recv(sdsocket, buff, 4096, 0)) > 0)
{
total += n;
if (n == -1)
{
printf("recv Error");
}
if (fwrite(buff, sizeof(char), n, file) != n)
{
printf("fwrite Error");
}
memset(buff, 0, 4096);
}
printf("Przesylanie udane liczba bajtow = %ld\n", total);
fclose(file);
goto start;
}
bzero(odbior, 512);
recv(sdsocket, odbior, 512, 0);
printf("%s\n", odbior);
}
return 0;
}