Ftpservre1 1
Ftpservre1 1
h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
char send_buf[10000];
control.sin_family = AF_INET;
control.sin_port = htons(port);
control.sin_addr.s_addr = INADDR_ANY;
listen(lfd, 5);
printf("\nServer ready, waiting for client....\n");
socklen_t n = sizeof(client);
confd = accept(lfd, (struct sockaddr *)&client, &n);
if (confd < 0)
{
perror("Accept failed");
close(lfd);
exit(1);
}
printf("\nClient connected....\n");
while (1)
{
recv(confd, rBuf, sizeof(rBuf), 0);
if (strcmp(rBuf, "get") == 0)
{
printf("client command: %s\n", rBuf);
data.sin_family = AF_INET;
data.sin_addr.s_addr = INADDR_ANY;
data.sin_port = htons(8080);
listen(fd, 5);
printf(“\nServer ready, waiting for client to connect to
data socket\n”);
n = sizeof(data_client);
confd2 = accept(fd, (struct sockaddr *)&data_client, &n);
if (confd2 < 0)
{
perror("Accept failed for data socket");
close(fd);
exit(1);
}
printf("\nClient connected to data socket\n");
send_file(fp);
fclose(fp);
close(confd2);
close(fd);
}
}
else if (strcmp(rBuf, "close") == 0)
{
printf("\nClient command: %s\n", rBuf);
printf("\nClient disconnected and server closed\n");
break;
}
}
close(confd);
close(lfd);
return 0;
}