博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
<源代码>FTPclient追加方式上传自己定义信息
阅读量:6078 次
发布时间:2019-06-20

本文共 7270 字,大约阅读时间需要 24 分钟。

实现功能:向FTPserver以追加方式上传自己定义信息(例程中为:2014-10-08 13:47:15 test.)

源代码下载(免积分):

ftp測试软件(免积分):

1.sock.h

#ifndef __SOCK_H#define __SOCK_H#define DBG(x)	//printf(x)#define	SERVER_CTRL_PORT	21#define	CLIENT_CTRL_PORT	5000#define	CLIENT_DATA_PORT	(CLIENT_CTRL_PORT+1)#define BUF_SIZE			1024extern char BufferReply[BUF_SIZE];extern int SocketCmd;extern int SocketData;extern char *ServerIP;extern int SERVER_DATA_PORT;extern struct sockaddr_in ServerAddr_Cmd;extern struct sockaddr_in ServerAddr_Data;extern struct sockaddr_in ClientAddr_Cmd;extern struct sockaddr_in ClientAddr_Data;int ClientInit(void);int ConnectCmdSocket(void);int SendCmmand(char *cmd);int SendData(char *buf, int len);int ReceiveReply(char *buffer, int len);int CloseSocket(void);#endif
2.sock.c

#include 
#include
#include
#include
#include
#include
#include
#include
#include "sock.h"char BufferReply[BUF_SIZE]={0};int SocketCmd=0;int SocketData=0;char *ServerIP="10.13.0.21";int SERVER_DATA_PORT=0;struct sockaddr_in ServerAddr_Cmd;struct sockaddr_in ServerAddr_Data;struct sockaddr_in ClientAddr_Cmd;struct sockaddr_in ClientAddr_Data;int ClientInit(void){ //set server addr ServerAddr_Cmd.sin_family = AF_INET; ServerAddr_Cmd.sin_addr.s_addr = inet_addr(ServerIP); ServerAddr_Cmd.sin_port = htons(SERVER_CTRL_PORT); //create new socket if((SocketCmd = socket(AF_INET, SOCK_STREAM, 0)) ==0 ) { DBG("Create SockedCmd Failed!\n"); return -1; } DBG("Create socket SocketCmd.\n"); if((SocketData = socket(AF_INET, SOCK_STREAM, 0)) ==0 ) { DBG("Create SockedData Failed!\n"); return -1; } DBG("Create socket SocketData.\n"); //set client addr ClientAddr_Cmd.sin_family = AF_INET; ClientAddr_Cmd.sin_addr.s_addr = htonl(INADDR_ANY); ClientAddr_Cmd.sin_port = htons(CLIENT_CTRL_PORT); ClientAddr_Data.sin_family = AF_INET; ClientAddr_Data.sin_addr.s_addr = htonl(INADDR_ANY); ClientAddr_Data.sin_port = htons(CLIENT_DATA_PORT); //设置端口为可反复连接 int on=1; if((setsockopt(SocketCmd, SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0) { DBG("setsockopt:set sock opt failed!\n"); return -1; } if((setsockopt(SocketData, SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0) { DBG("setsockopt:set sock opt failed!\n"); return -1; } if(bind(SocketData, (struct sockaddr*)&ClientAddr_Data, sizeof(ClientAddr_Data))<0) { DBG("bind SockedData Failed!\n"); return -1; } //bind client addr if(bind(SocketCmd, (struct sockaddr*)&ClientAddr_Cmd, sizeof(ClientAddr_Cmd))<0) { DBG("bind SockedCmd Failed!\n"); return -1; } return 0;}int ConnectCmdSocket(void){ if( connect (SocketCmd, (struct sockaddr*)&ServerAddr_Cmd, sizeof(ServerAddr_Cmd))<0) { DBG("ConnectCmdSocket:Connect Failed!\n"); CloseSocket(); return -1; } ReceiveReply(BufferReply, BUF_SIZE); return 0;}int SendCmmand(char *cmd){ if(cmd == NULL) return -1; int len = strlen(cmd); if(write(SocketCmd, cmd, len)!=len) { DBG("SendCmmand:socket write failed!\n"); return -1; } ReceiveReply(BufferReply, BUF_SIZE); return 0;}int SendData(char *buf, int len){ if(buf == NULL) return -1; if(write(SocketData, buf, len)!=len) { DBG("SendData:socket write failed!\n"); return -1; } return 0;}int ReceiveReply(char *buf, int len){ int length=0; if( (length = read(SocketCmd, buf, len))<0 ) { DBG("ReceiveReply:read failed!\n"); return -1; } printf("%s", buf); bzero (buf, BUF_SIZE); return length;}int CloseSocket(void){ if(close(SocketCmd)<0) { DBG("CloseSocket:close SocketData failed!\n"); return -1; } DBG("Close socket SocketCmd.\n"); if(close(SocketData)<0) { DBG("CloseSocket:close SocketData failed!\n"); return -1; } DBG("Close socket SocketData.\n"); return 0;}
3.ftp.h

#ifndef __FTP_H#define __FTP_Hint Init_FTP(void);int LogIn(char *user, char *password);int LogOut(void);int SendDataToFTP(char *name, char *pass, char *FileName, char *buf, int FileLen);#endif
4.ftp.c

#include 
#include
#include
#include
#include
#include
#include
#include
#include "ftp.h"#include "sock.h"int Init_FTP(void){ if(ClientInit()<0) { DBG("ClientInit Failed!\n"); return -1; } if(ConnectCmdSocket()<0) { DBG("ConnectCmdSocket Failed!\n"); return -1; } return 0;}int LogIn(char *user, char *password){ char temp[20]={0}; sprintf(temp, "USER %s\r\n", user); if(SendCmmand(temp)<0) { DBG(("SendCommand failed:%s", temp)); return -1; } sprintf(temp, "PASS %s\r\n", password); if(SendCmmand(temp)<0) { DBG(("SendCommand failed:%s", temp)); return -1; } return 0;}int LogOut(void){ char *temp="QUIT\r\n"; if(SendCmmand(temp)<0) { DBG(("SendCommand failed:%s", temp)); CloseSocket(); return -1; } CloseSocket(); return 0;}int GetPort(char *str){ int port=0; char CommerCnt=0; char *p=str; int h=0; int l=0; while(CommerCnt!=4) { if(*p == ',') CommerCnt++; p++; } h = atoi(p); p++;p++; l = atoi(p); port = (h*256) + l; printf("server data port=%d\n", port); return port;}int SetDataPort(void){ char *cmd="PASV\r\n"; int len = strlen(cmd); if(write(SocketCmd, cmd, len)!=len) { DBG("SetDataPort:socket write failed!\n"); return -1; } len=0; if( (len = read(SocketCmd, BufferReply, 100))<0 ) { DBG("SetDataPort:socket read failed!\n"); return -1; } printf("%s", BufferReply); SERVER_DATA_PORT = GetPort (BufferReply); bzero(BufferReply, BUF_SIZE); ServerAddr_Data.sin_family = AF_INET; ServerAddr_Data.sin_addr.s_addr = inet_addr(ServerIP); ServerAddr_Data.sin_port = htons(SERVER_DATA_PORT); return 0;}int PrePareForSendData(void){ if(SendCmmand("TYPE I\r\n")<0) { DBG(("SendCommand failed:%s", "TYPE I")); return -1; } if(SetDataPort()<0) { DBG(("SetDataPort failed!\r\n")); return -1; } if( connect (SocketData, (struct sockaddr*)&ServerAddr_Data, sizeof(ServerAddr_Data))<0) { DBG("ConnectDataSocket:Connect Failed!\n"); return -1; } return 0;}int FinishForSendData(void){ char *temp="QUIT\r\n"; if(close(SocketData)<0) { DBG("FinishForSendData:close SocketData failed.\n"); close(SocketCmd); return -1; } ReceiveReply(BufferReply, BUF_SIZE); if(SendCmmand(temp)<0) { DBG(("SendCommand failed:%s", temp)); close(SocketCmd); return -1; } close(SocketCmd); return 0;}int SendDataToFTP(char *name, char *pass, char *FileName, char *buf, int FileLen){ char temp[128]={0}; if(Init_FTP()<0) { return -1; } if(LogIn(name, pass)<0) { return -1; } if(PrePareForSendData()<0) { LogOut(); return -1; } sprintf(temp, "APPE %s\r\n", FileName); if(SendCmmand(temp)<0) { DBG(("SendCommand failed:%s", temp)); LogOut(); return -1; } if(SendData (buf, FileLen)<0) { DBG("SendData failed.\n"); LogOut(); return -1; } FinishForSendData(); return 0;}
5.main.c

#include 
#include
#include
#include "ftp.h"char databuf[1024];void gettime(void){ time_t now=0; struct tm *p_localtime; time(&now); p_localtime = localtime (&now); sprintf(databuf, "%04d-%02d-%02d %02d:%02d:%02d test.\r\n", p_localtime->tm_year+1900,p_localtime->tm_mon+1,p_localtime->tm_mday, p_localtime->tm_hour,p_localtime->tm_min,p_localtime->tm_sec); printf("%s", databuf);}int main(void){ gettime(); if(SendDataToFTP("plain", "cipher", "test.txt", databuf, strlen(databuf))<0) { printf("Send file Failed!!!!!!\n"); return -1; } printf("Send file Success!\n"); return 0;}

转载地址:http://uyxgx.baihongyu.com/

你可能感兴趣的文章
使用JRockit 能否加速Myeclipse
查看>>
职场老司机才知道的Excel技巧,一般人不知道哦!
查看>>
Spring3与hibernate4注解式声明事务管理
查看>>
【linux下c语言服务器开发系列1】多进程处理多客户端的连接
查看>>
线性表的顺序存储结构
查看>>
初识centos7与centos6的区别
查看>>
批量部署管理服务器的成熟方案Puppet,Func,cfengine汇总贴
查看>>
spring aop 嵌套调用的问题 (同一方法内调用切面切不到)
查看>>
CentOS6 安装Redis
查看>>
Linux手动导入导出Mysql数据库
查看>>
是不是只有在我有钱的时候你才会爱我
查看>>
wuzhicms查找:当前页面使用的哪个文件
查看>>
oracle FIND_IN_SET函数
查看>>
IOS 单例方法的建立
查看>>
Ubuntu下搭建Apache+MySQL+PHP开发环境
查看>>
使用U盘安装Kali的曲折经历
查看>>
Mybatis增删改查之Oracle
查看>>
symfony中不加载layout.php模板
查看>>
构建高可用服务器之三 Keepalive冗余Mysql
查看>>
编程里面的耗时更新任务需要进度
查看>>