QT5本身对ftp和tftp的支持不太好,找了很多地方也没找到好用的方法,无奈之下只好使用开源的curl来实现。但是该怎么使用tftp一直没找到说明。在curl的doc下有一堆的examples,但是就是没有tftp的,但是文档里明明说实现了tftp啊?答案是人家感觉tftp太简单了,都懒得单独写个demo了,╮(╯▽╰)╭深深的被鄙视了。下面我就说一下该怎么使用tftp功能。
1.建立tftp测试环境
写代码过程中随时会出问题,一旦环境出了问题,一切都是白搭,所以首先建立tftp的测试环境,方法非常简单。为了减少意外,最好使用两台电脑(或者电脑+虚拟机,总之尽量隔离开),每个电脑安装一个tftpd程序,window自带该功能的,但是太麻烦,下载一个tftpd32.exe就行了。
打开其中一台电脑的tftp32.exe程序,选择“tftp server”端,当做服务器。然后在另一个电脑打开tftp32.exe之后选择“client(客户端)”选项,如下图所示:
注意这里的设置:
1.“current directory”是tftp的工作目录,该目录下的文件可以直接tftp传输,下载的文件也自动放在这里。
2.“server interface”是本机IP地址,这个要注意网卡和网段,选错的话会导致传输连接无法建立。
3.Host:服务器的IP地址,Port:端口,tftp默认都是69
4.LocalFile:本地文件,例如“ds.txt”。Remote File:服务器端文件名,例如“2.2”。注意这里的设置,如果是下载的话(Get模式),就是将服务器端的”2.2“文件保存到本地,名称为ds.txt。如果是上传的话(Put模式),就是将本地文件”ds.txt“上传到服务器,名称为”2.2“。如果重复操作,文件会被覆盖,因此这里要注意。
传输完成,会出现下面的界面:
2.curl实现文件下载
好了,上面测试成功的话,就可以用curl写代码了。过程就不细说了,直接上代码:
/*upload*/ #define LOCAL_FILE "D:/tmp/curl.c" #define REMOTE_URL "tftp://192.168.1.110:69/uploading.txt" static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) { curl_off_t nread; /* in real-world cases, this would probably get this data differently as this fread() stuff is exactly what the library already would do by default internally */ size_t retcode = fread(ptr, size, nmemb, (FILE*)stream); nread = (curl_off_t)retcode; fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T " bytes from file\n", nread); return retcode; } int tftpfileUpload() { CURL *curl; CURLcode res; FILE *hd_src; struct stat file_info; curl_off_t fsize; struct curl_slist *headerlist=NULL; /* get the file size of the local file */ if(stat(LOCAL_FILE, &file_info)) { printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno)); return 1; } fsize = (curl_off_t)file_info.st_size; printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize); /* get a FILE * of the same file */ hd_src = fopen(LOCAL_FILE, "rb"); /* In windows, this will init the winsock stuff */ curl_global_init(CURL_GLOBAL_ALL); /* get a curl handle */ curl = curl_easy_init(); if(curl) { /* we want to use our own read function */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); /* enable uploading */ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); /* specify target */ curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL); /* pass in that last of FTP commands to run after the transfer */ curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); /* now specify which file to upload */ curl_easy_setopt(curl, CURLOPT_READDATA, hd_src); curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize); /* Now run off and do what you've been told! */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); /* clean up the FTP commands list */ curl_slist_free_all (headerlist); /* always cleanup */ curl_easy_cleanup(curl); } fclose(hd_src); /* close the local file */ curl_global_cleanup(); return 0; }
外部调用方法:
downloadFileFromServer();
执行的时候就可以发现程序将服务器端下的“tftps.cpp”保存到本地了。
3.实现文件上传
还是直接上代码;
/*upload*/ #define LOCAL_FILE "D:/tmp/curl.c" #define UPLOAD_FILE_AS "uploading.txt" #define REMOTE_URL "tftp://192.168.1.110:69/uploading.txt" #define RENAME_FILE_TO "renamed.txt" static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) { curl_off_t nread; /* in real-world cases, this would probably get this data differently as this fread() stuff is exactly what the library already would do by default internally */ size_t retcode = fread(ptr, size, nmemb, (FILE*)stream); nread = (curl_off_t)retcode; fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T " bytes from file\n", nread); return retcode; } int tftpfileUpload() { CURL *curl; CURLcode res; FILE *hd_src; struct stat file_info; curl_off_t fsize; struct curl_slist *headerlist=NULL; static const char buf_1 [] = UPLOAD_FILE_AS; static const char buf_2 [] = RENAME_FILE_TO; /* get the file size of the local file */ if(stat(LOCAL_FILE, &file_info)) { printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno)); return 1; } fsize = (curl_off_t)file_info.st_size; printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize); /* get a FILE * of the same file */ hd_src = fopen(LOCAL_FILE, "rb"); /* In windows, this will init the winsock stuff */ curl_global_init(CURL_GLOBAL_ALL); /* get a curl handle */ curl = curl_easy_init(); if(curl) { /* we want to use our own read function */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); /* enable uploading */ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); /* specify target */ curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL); /* pass in that last of FTP commands to run after the transfer */ curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); /* now specify which file to upload */ curl_easy_setopt(curl, CURLOPT_READDATA, hd_src); /* Set the size of the file to upload (optional). If you give a *_LARGE option you MUST make sure that the type of the passed-in argument is a curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must make sure that to pass in a type 'long' argument. */ curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize); /* Now run off and do what you've been told! */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); /* clean up the FTP commands list */ curl_slist_free_all (headerlist); /* always cleanup */ curl_easy_cleanup(curl); } fclose(hd_src); /* close the local file */ curl_global_cleanup(); return 0; }
调用方法为
tftpfileUpload();
OK !就这么简单。