https://pastein.ru/t/EC
скопируйте уникальную ссылку для отправки
#include <stdio.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
int main()
{
struct sockaddr_in dest;
struct sockaddr_in my_dest;
struct hostent *host;
struct hostent *my_host;
char hostname[50];
char my_hostname[50];
int my_sock;
int sock;
int bytes_read;
int bytes_send;
short int portnum;
int MAXBUF = 1000;
char buffer[MAXBUF];
char tosend[MAXBUF];
struct sockaddr_in list;
struct sockaddr_in client;
int listsock;
int my_port = 0;
int i = 0;
int high_counter = 0;
int low_counter = 0;
int main_counter = 0;
char store_chars[10];
char *store_high;
char *store_low;
int j = 0;
my_sock = socket(PF_INET, SOCK_STREAM, 0);
sock = socket(PF_INET, SOCK_STREAM, 0);
listsock = socket(PF_INET, SOCK_STREAM, 0);
bzero(&list, sizeof(list));
list.sin_family = AF_INET;
list.sin_port = htons(2397);
list.sin_addr.s_addr = INADDR_ANY;
/*if (bind(listsock, (const struct sockaddr *)&list, sizeof(list)) != 0)
printf("ERROR IN BIND!");
if (listen(listsock, 20) != 0)
printf("ERROR IN LISTEN!");*/
printf("Insert server address: ");
scanf("%s", hostname);
printf("Insert port number: ");
scanf("%i", &portnum);
host = gethostbyname(hostname);
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(portnum);
dest.sin_addr.s_addr = *(long*)(host->h_addr);
if (connect(sock,(struct sockaddr *)&dest, sizeof(dest)) != 0)
{
printf("Connection fail!\n");
abort();
}
bytes_read = read(sock, buffer, MAXBUF-1);
printf("Server's answer:\t%s", buffer);
bzero(buffer, MAXBUF);
printf("\nUsername? ");
scanf("%s", buffer);
sprintf(tosend,"user %s\r\n", buffer);
bytes_send = send(sock, tosend, strlen(tosend), 0);
bzero(buffer, MAXBUF);
bytes_read = recv(sock, buffer, MAXBUF-1, 0);
printf("Server's answer:\t%s", buffer);
bzero(buffer, MAXBUF);
printf("Password? ");
scanf("%s", buffer);
bzero(tosend, MAXBUF);
sprintf(tosend, "pass %s\r\n", buffer);
bytes_send = send(sock, tosend, strlen(tosend), 0);
//printf("%s", tosend);
bzero(buffer, MAXBUF);
bytes_read = recv(sock, buffer, MAXBUF-1, 0);
printf("Server's answer:\t%s", buffer);
bzero(buffer, MAXBUF);
bzero(tosend, MAXBUF);
sprintf(tosend, "PASV\n");
bytes_send = send(sock, tosend, strlen(tosend), 0);
bytes_read = recv(sock, buffer, MAXBUF, 0);
printf("Server's answer:\t%s", buffer);
//==========================================================================
//printf("%d\n" , strlen(buffer));
for(i = 37; i < strlen(buffer);i++)
{
if(buffer[i] == ')' ){
low_counter = main_counter - high_counter;
break;
}
if(buffer[i]!=')' && buffer[i] != ',')
{
//printf("%c\n",buffer[i]);
store_chars[main_counter] = buffer[i];
main_counter++;
}
if(buffer[i] == ','){
high_counter = main_counter;
}
}
//printf("main counter = %d , high counter = %d , low counter = %d\n",main_counter,high_counter,low_counter);
//for(i = 0 ; i < main_counter ; i++)
//{
// printf("store %c\n",store_chars[i]);
//}
store_high = (char *)malloc(high_counter);
store_low = (char *)malloc(low_counter);
for(i = 0 ; i < high_counter ; i++){
store_high[i] = store_chars[i];
}
for(i = 0 ; i < high_counter ; i++){
//printf("%c\n",store_high[i]);
}
for(i=0;i<low_counter;i++)
{
store_low[i] = store_chars[high_counter+i];
}
my_port = atoi(store_high) * 256;
my_port = my_port + atoi(store_low);
printf("port = %d\n" , my_port);
bzero(tosend, MAXBUF);
sprintf(tosend, "LIST\n");
bytes_send = send(sock, tosend, strlen(tosend), 0);
//---------------------------------------------------------
my_host = gethostbyname(hostname);
bzero(&my_dest, sizeof(my_dest));
my_dest.sin_family = AF_INET;
my_dest.sin_port = htons(my_port);
my_dest.sin_addr.s_addr = *(long*)(my_host->h_addr);
if (connect(my_sock, (struct sockaddr *)&my_dest, sizeof(my_dest)) != 0)
{
printf("Connection fail!\n");
abort();
}
bytes_read = recv(my_sock, buffer, MAXBUF-1, 0);
//printf("Server's answer:\t%s", buffer);
/*bzero(tosend, MAXBUF);
sprintf(tosend, "LIST\n");
bytes_send = send(my_sock, tosend, strlen(tosend), 0);
bzero(buffer, MAXBUF);
bytes_read = recv(my_sock, buffer, MAXBUF-1, 0);
printf("Server's answer:\t%s", buffer);*/
//---------------------------------------------------------
bzero(tosend, MAXBUF);
bzero(buffer, MAXBUF);
sprintf(tosend, "QUIT\n");
bytes_send = send(sock, tosend, strlen(tosend), 0);
shutdown(listsock, 2);
shutdown(sock, 2);
bzero(tosend, MAXBUF);
}