C: Self-written simple FTP client, including (list, enter directory, upload files, download files, delete files) function

Source: Internet
Author: User
Tags sprintf strcmp ftp client htons

Simple FTP client
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct SOCKADDR* SOCKADDRP;
Command transfer
int sockfd;
Data transmission
int sockfd1;
Char buf[4096] = {};
Address
Char buf1[4096] = {};
Account number, password
Char buf2[4096] = {};
Store command name
Char tem1[255] = {};
Store file name
Char tem2[255] = {};
struct sockaddr_in new_addr = {af_inet};
void Func ()
{
sprintf (buf, "pasv\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
int a[4];
    int b[2];
    printf ("%s\n", buf);
    SSCANF (buf, "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)",&a[0],&a[1],&a[2],&a[3],&b[0],& B[1]);
    int msg = b[0]*256+b[1];
SOCKFD1 = socket (af_inet,sock_stream,0);
New_addr.sin_port = htons (msg);
NEW_ADDR.SIN_ADDR.S_ADDR = inet_addr ("192.168.6.66");
Connect (SOCKFD1, (SOCKADDRP) &new_addr,sizeof (NEW_ADDR));
}
Directory
void Ls_run ()
{
sprintf (buf, "list\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd1,buf,sizeof (BUF), 0);
printf ("server:%s\n", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server:%s\n", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server:%s\n", buf);
}
Go to Catalog
void Cd_run ()
{
//-----------------------------------------------------
sprintf (buf, "CWD%s\n", tem2);
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
sprintf (buf, "pwd\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd1,buf,sizeof (BUF), 0);
printf ("server1:%s", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server2:%s\n", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server3:%s\n", buf);
sprintf (BUF1, "%s", buf);
Func ();
//-------------------------------------------------------
sprintf (buf, "list\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd1,buf,sizeof (BUF), 0);
printf ("%s", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server1:%s\n", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server2:%s\n", buf);
}
Download
void Get_run ()
{
Func ();
sprintf (buf, "RETR%s\n", tem2);
Send (Sockfd,buf,strlen (BUF), 0);
printf ("%s", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd1,buf,sizeof (BUF), 0);
int tmp=sizeof (NEW_ADDR);
int fd = open (tem2,o_wronly| O_creat| o_trunc,0644);
Write (Fd,buf,strlen (BUF));
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("%s", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
Close (FD);
}
Upload
void Put_run ()
{
Char inf[1024] = {};
Func ();
int fd = open (tem2,o_rdonly);
Read (Fd,inf,strlen (BUF));
Close (FD);
sprintf (buf, "STOR%s\n", tem2);
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("%s", buf);
sprintf (buf, "%s\n", INF);
Send (Sockfd1,buf,sizeof (BUF), 0);
Close (SOCKFD1);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("%s", buf);
}
Delete
void Delete_run ()
{
Func ();
sprintf (buf, "DELE%s\n", tem2);
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("%s", buf);
//-------------------------------------------------
sprintf (buf, "list\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd1,buf,sizeof (BUF), 0);
printf ("%s", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server1:%s\n", buf);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server2:%s\n", buf);
}
Landing
void login (void)
{
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server:%s", buf);
User account Input
printf ("Enter user name:");
Gets (BUF2);
sprintf (buf, "%s%s\n", "USER", buf2);
sprintf (buf, "USER eos1806\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server:%s", buf);
User Password input
printf ("Enter password:");
Gets (BUF2);
sprintf (buf, "%s%s\n", "PASS", buf2);
sprintf (buf, "PASS iqot1806\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server:%s", buf);
sprintf (buf, "syst\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server:%s", buf);
sprintf (buf, "feat\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("server:%s", buf);
sprintf (buf, "OPTS UTF8 on\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("%s", buf);
sprintf (buf, "pwd\n");
Send (Sockfd,buf,strlen (BUF), 0);
Bzero (buf,sizeof (BUF));
Recv (Sockfd,buf,sizeof (BUF), 0);
printf ("%s", buf);
Function
while (1)
{
printf ("ftp>");
Get command
Gets (BUF);
Command Branch
if (!strcmp (buf, "ls"))
{
Func ();
Ls_run ();
Close (SOCKFD1);
Continue
}
if (!strcmp (buf, "Bye"))
{
Break
}
SSCANF (buf, "%s%s", tem1,tem2);
if (!strcmp (TEM1, "CD"))
{
Cd_run ();
Close (SOCKFD1);
Continue
}
if (!strcmp (tem1, "get"))
{
Get_run ();
Close (SOCKFD1);
Continue
}
if (!strcmp (tem1, "put"))
{
Put_run ();
Continue
}
if (!strcmp (tem1, "delete"))
{
Delete_run ();
Close (SOCKFD1);
Continue
}
}
}
Main function
int main (int argc,char* argv[])
{
Socket
SOCKFD = socket (af_inet,sock_stream,0);
if (0 > SOCKFD)
{
Perror ("socket");
return-1;
}
struct Sockaddr_in addr = {Af_inet};
Addr.sin_port = htons (21);
ADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
int ret = connect (SOCKFD, (SOCKADDRP) &addr,sizeof (addr));
if (0 > Ret)
{
Perror ("Connect");
return-1;
}
Login ();
Close (SOCKFD);
}

C: Self-written simple FTP client, including (list, enter directory, upload files, download files, delete files) function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.