SEND_namefile
This commit is contained in:
parent
b29dcc30da
commit
37adef3dd3
@ -12,17 +12,18 @@
|
|||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const int port = 7332;
|
const int port = 7332;
|
||||||
const char ip[512] = "192.168.1.30";
|
const char ip[512] = "192.168.0.43";
|
||||||
struct sockaddr_in endpoint;
|
struct sockaddr_in endpoint;
|
||||||
struct dirent **namelist;
|
struct dirent **namelist;
|
||||||
char buff[512];
|
char buff[512];
|
||||||
char name_file[512];
|
char name_file[512];
|
||||||
int sdsocket, addrlen, sdconnection, n;
|
int sdsocket, addrlen, sdconnection, n, x;
|
||||||
|
|
||||||
endpoint.sin_family = AF_INET;
|
endpoint.sin_family = AF_INET;
|
||||||
endpoint.sin_port = htons(port);
|
endpoint.sin_port = htons(port);
|
||||||
endpoint.sin_addr.s_addr = inet_addr(ip);
|
endpoint.sin_addr.s_addr = inet_addr(ip);
|
||||||
addrlen = sizeof(struct sockaddr_in);
|
addrlen = sizeof(struct sockaddr_in);
|
||||||
|
ssize_t count = 0;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@ -38,7 +39,7 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
else if (strncmp("HELP", buff, 4) == 0)
|
else if (strncmp("HELP", buff, 4) == 0)
|
||||||
{
|
{
|
||||||
printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n FTP_namefile\n");
|
printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n FTP_namefile\n SEND_namefile\n");
|
||||||
goto start;
|
goto start;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +107,39 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
|
goto start;
|
||||||
|
}
|
||||||
|
if (strncmp("SEND", buff, 4) == 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
close(sdsocket);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
goto start;
|
goto start;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ int main(int argc, char **argv)
|
|||||||
bzero(names_files, 512);
|
bzero(names_files, 512);
|
||||||
bzero(name_file, 512);
|
bzero(name_file, 512);
|
||||||
|
|
||||||
n = scandir(".", &namelist, NULL, alphasort);
|
n = scandir("..", &namelist, NULL, alphasort);
|
||||||
if (n == -1)
|
if (n == -1)
|
||||||
{
|
{
|
||||||
perror("scandir");
|
perror("scandir");
|
||||||
@ -90,6 +90,53 @@ int main(int argc, char **argv)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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 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 ((sdconnection2 =
|
||||||
|
accept(sdsocket,
|
||||||
|
(struct sockaddr *)&endpoint,
|
||||||
|
&addrlen)) >= 0)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while ((n = recv(sdconnection2, buff, 4096, 0)) > 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);
|
||||||
|
close(sdconnection2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
else if (strncmp("EXIT", buff, 4) == 0)
|
else if (strncmp("EXIT", buff, 4) == 0)
|
||||||
{
|
{
|
||||||
printf("Server shut down..\n");
|
printf("Server shut down..\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user