<源代码>FTPclient追加方式上传自己定义信息

时间:2023-01-18 18:40:46

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

源代码下载(免积分):http://download.csdn.net/detail/jipingyuan/8012533

ftp測试软件(免积分):http://download.csdn.net/detail/jipingyuan/8012381

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 1024 extern 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#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_H int 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 <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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 <stdio.h>
#include <string.h>
#include <time.h>
#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;
}

&lt;源代码&gt;FTPclient追加方式上传自己定义信息的更多相关文章

  1. Java FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  2. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

  3. Ajax方式上传文件

    用到两个对象 第一个对象:FormData 第二个对象:XMLHttpRequest 目前新版的Firefox 与 Chrome 等支持HTML5的浏览器完美的支持这两个对象,但IE9尚未支持 For ...

  4. 通过Ajax方式上传文件,使用FormData进行Ajax请求

    通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...

  5. Android必知必会-使用okhttp的PUT方式上传文件

    注:如果移动端排版有问题,请看 简书版 (<-点击左边),希望CSDN能更好的支持移动端. 背景 公司的文件上传接口使用PUT协议,之前一直用的都是老项目中的上传类,现在项目中使用了okhttp ...

  6. koa2:通过Ajax方式上传文件,使用FormData进行Ajax请求

    koa2通过表单上传的网上很多,但通过Ajax方式上传文件,使用FormData进行Ajax请求,不好找. 参考了这个用base64上传图片的例子.https://github.com/Yuki-Mi ...

  7. 前端 - jquery方式 &sol; iframe &plus;form 方式 上传文件

    环境与上一章一样 jquery 方式上传文件: HTML代码 {#html代码开始#} <input type="file" id="img" > ...

  8. &lbrack;转&rsqb; 通过Ajax方式上传文件,使用FormData进行Ajax请求

    通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...

  9. IOUtils方式上传下载文件

    package com.css.hdfs04; import java.io.File; import java.io.FileInputStream; import java.io.FileOutp ...

随机推荐

  1. 查看npm全局安装的模块

    1.查看所有高级的npm moudles npm list --depth= 2.查看所有全局安装的模块 npm list --depth= -global

  2. 已安装php 编译安装 gd库拓展模块

    参考资料:http://wenku.baidu.com/link?url=EgXFShYxeJOZSYNQ_7RCBC-6X8OcRRCqVm4qCv49uBk57d6vLBoUpfYdQ-KqJRs ...

  3. Linux 下系统调用的三种方法

    系统调用(System Call)是操作系统为在用户态运行的进程与硬件设备(如CPU.磁盘.打印机等)进行交互提供的一组接口.当用户进程需要发生系统调用时,CPU 通过软中断切换到内核态开始执行内核系 ...

  4. 《为ipad而设计 打造畅销APP》读书笔记

    (1)应用应该对用户程序持ipad的变化做出响应 (2)要设计出确实出色的触摸屏交互界面需要能够触摸到自己的设计,反复触摸,直到找到感觉 (3)chipmunk physics 2D的物理图形库 (4 ...

  5. poj 1016 Numbers That Count

    点击打开链接 Numbers That Count Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17922   Accep ...

  6. 逗号&quot&semi; , &quot&semi;表达式

    概述 逗号操作符 对它的每个操作对象求值(从左至右),然后返回最后一个操作对象的值. 语法 expr1, expr2, expr3... 参数 expr1, expr2, expr3... 任一表达式 ...

  7. spring mvc3中的addFlashAttribute方法

    spring mvc3中的addFlashAttribute方法  

  8. eval &amp&semi; exec&lpar;绕过长度限制思路学习)

    eval & exec知识点记录--原文章phithon,只是记录一下我自己的学习过程. 1.eval & exec if(strlen($param)<17 &&amp ...

  9. Angular2 &Hat; 资源链接

     Angular2 资源链接 Material Desgin 2 githubhttps://github.com/Promact/md2 DEMOhttp://code.promactinfo.co ...

  10. 20165214 2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 Week5

    <网络对抗技术>Exp3 免杀原理与实践 Week5 一.实验内容 1.正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,加壳工具,使用shell ...