main.cpp
资源名称:nettest.rar [点击查看]
上传用户:pmma55
上传日期:2020-12-28
资源大小:17k
文件大小:2k
源码类别:
手机WAP编程
开发平台:
Visual C++
- #include <stdio.h>
- #include "M5HttpDown.h"
- #define TARGET_URI "http://www.5mbox.com/bbs/test1.mp3"
- #define LOCAL_FNAME "test.mp3"
- CM5HttpDown * g_http_down ;
- void recv_func(char * recv_buf, int recv_len, void * data) ;
- bool check_exists(char * file_name) ;
- int get_file_size(char * file_name) ;
- bool check_exists(char * file_name)
- {
- FILE * fp = NULL ;
- if(!file_name) return false ;
- fp = fopen(file_name, "r") ;
- if(fp == NULL) return false ;
- fclose(fp) ;
- return true ;
- }
- int get_file_size(char * file_name)
- {
- FILE * fp = NULL ;
- int file_size = 0 ;
- if(!file_name) return 0 ;
- fp = fopen(file_name, "rb") ;
- if(fp == NULL) return 0 ;
- fseek(fp, 0, SEEK_END) ;
- file_size = (int)(ftell(fp)) ;
- fclose(fp) ;
- return file_size ;
- }
- void recv_func(char * recv_buf, int recv_len, void * data)
- {
- FILE * fp = (FILE *)(data) ;
- double rate = 0.0f ;
- rate = (double)(g_http_down->http_recv_size())/(double)(g_http_down->http_total_size()) ;
- printf("rrecv : %d%%", (int)(rate * 100)) ;
- fflush(stdout) ;
- fwrite(recv_buf, recv_len, 1, fp) ;
- fflush(fp) ;
- if(g_http_down->http_total_size() <= g_http_down->http_recv_size()) {
- fclose(fp) ;
- g_http_down->http_stop() ;
- }
- }
- int main(int argc, char *argv[])
- {
- FILE * fp = NULL ;
- int recv_bytes = 0 ;
- // check file exist will perform resume down
- if(check_exists(LOCAL_FNAME)) {
- recv_bytes = get_file_size(LOCAL_FNAME) ;
- fp = fopen(LOCAL_FNAME, "ab") ;
- fseek(fp, 0, SEEK_END) ;
- } else {
- fp = fopen(LOCAL_FNAME, "wb") ;
- }
- g_http_down = new CM5HttpDown() ;
- if(!g_http_down->http_down(TARGET_URI, recv_func, (void *)(fp), recv_bytes)) {
- // printf("connect failed !n") ;
- } else {
- while(g_http_down->is_running()) {
- Sleep(1000) ;
- }
- }
- g_http_down->http_stop() ;
- delete g_http_down ;
- return EXIT_SUCCESS;
- }