C语言:自己编写的简易ftp客户端,包含(列表,进入目录,上传文件,下载文件,删除文件)功能

//简易ftp客户端#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;
//命令传输
int sockfd;
//数据传输
int sockfd1;

char buf[4096] = {};
//地址
char buf1[4096] = {};
//帐号、密码
char buf2[4096] = {};
//存放命令名
char tem1[255] = {};
//存放文件名
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));
}


//目录
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);
	
}
//进入目录
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);
	
}
//下载
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);
}
//上传
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);
	
	
	
}
//删除
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);

}
	
//登陆
void login(void)
{
	
	recv(sockfd,buf,sizeof(buf),0);
	printf("server:%s",buf);
//用户帐号输入
	printf("输入用户名:");
	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);
//用户密码输入
	printf("输入密码:");
	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);
	
//功能
	while(1)
	{	
		printf("ftp> ");
		//获取命令	
		gets(buf);
		//命令分支
		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;
		}
	}
}
//主函数
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);
}

原文地址:https://www.cnblogs.com/gzk1171848896/p/9426935.html

时间: 2024-10-09 18:07:49

C语言:自己编写的简易ftp客户端,包含(列表,进入目录,上传文件,下载文件,删除文件)功能的相关文章

webDAV服务的开启以及客户端的上传、下载、删除、新建文件夾、列表的代码(C#)

windows server 2003开启webDAV服务 1. 启动“IIS管理器”选择“WEB服务扩展”,选择“WEBDAV”的允许按钮启动WEBDAV功能 2.建立一个虚拟目录,对应到一个本地目录. 3.启动系统“服务”中的“WebClient”服务 参考网址 WebDAV文档rfc2518    http://www.ietf.org/rfc/rfc2518.txt webdav常用方法和概念总结   http://blog.csdn.net/mahongming/archive/200

C#通过FTP账号上传、修改、删除文件 FTPClient

下面类文件中,主要做的工作是:从ftp服务器上下载文件把本地文件替换.添加.或删除功能,在替换本地文件时会先备份一下本地的文件,若整个操作都完成了就会发出commit命令,表示全部替换成功.若中间操作有失败的,则把本地文件回滚到替换前的样子. 描述: SynchroDataTest.cs(Winform页面类)中的addfiles数组代表要下载到本地的文件(有可能是添加或替换),delfiles数组是要把本地的这些文件删除的数组 string[] addfiles = new string[]

java上传、下载、删除ftp文件

一共三个类,一个工具类Ftputil.,一个实体类Kmconfig.一个测试类Test 下载地址:http://download.csdn.net/detail/myfmyfmyfmyf/6697107 Ftputil类--------------- [java] view plaincopy package ftpDemo; import java.io.DataOutputStream; import java.io.InputStream; import java.io.OutputStr

android实现ftp上传、下载,支持文件夹

提供一个android手机实现ftp上传.下载的demo地址: http://download.csdn.net/detail/a358763471/8728637

C# ftp 上传、下载、删除

1 public class FtpHelper 2 { 3 public static readonly FtpHelper Instance = new FtpHelper(); 4 5 /// <summary> 6 /// 取得文件名 7 /// </summary> 8 /// <param name="ftpPath">ftp路径</param> 9 /// <returns></returns> 10

C# FTP 上传、下载、获取文件列表

static public class FtpHelper { //基本设置 static private string path = @"ftp://" + Helper.GetAppConfig("obj") + "/"; //目标路径 static private string ftpip =Helper.GetAppConfig("obj"); //ftp IP地址 static private string user

PHPstorm设置连接FTP,进行文件上传、下载、比较

                                             内容转载自:http://www.cnblogs.com/jikey/p/3486621.html   如何在Webstorm/Phpstorm中设置连接FTP, 并快速进行文件比较,上传下载,同步等操作 补充: 1. 打开Remote Host窗口的方式: Tools | Deployment | Browse Remote Host 或 View | Tool Windows | Remote Host

【问题分析】FTP文件上传与下载

问题描述:通常应用服务器与文件服务器分别在不同的机器上,涉及到文件的上传与下载.通过建立网络映射盘的形式,将文件服务器保存文件的文件夹映射到应用服务器的某个盘符下,经过试验,在tomcat下两台笔记本是可以实现的,但是在生产环境的websphere下试验,经过多番尝试,仍然实现不了. 问题分析:采用FTP的方式实现文件的上传与下载功能,在Java代码中编写客户端的上传与下载,在文件服务器上,直接装一个FTP服务器软件即可.注意生产环境的防火墙以及客户是否允许使用FTP. 解决方案: 工程中导入J

使用python操作FTP上传和下载

Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下 ftp登陆连接 from ftplib import FTP #加载ftp模块 ftp=FTP() #设置变量 ftp.set_debuglevel(2) #打开调试级别2,显示详细信息 ftp.connect("IP","port") #连接的ftp sever和端口 ftp.login("user","