实现功能:向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);#endif2.sock.c
#include3.ftp.h#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;}
#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);#endif4.ftp.c
#include5.main.c#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;}
#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;}