[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[网络工具] 原创命令行下载工具

http://bcn.bathome.net/s/tool/index.html?key=down

举例:
  1. down http://dl_dir.qq.com/qqfile/qq/QQ2012/QQ2012.exe
复制代码

求源码

TOP

这么单一的功能,48K体积 太夸张了, 论坛里有700 字节的下载工具,并且有源码
SOS --- >> lllsoslll@163.com

TOP

WSAStartup

socket

gethostbyname

inet_ntoa

inet_addr

ntohs

socket

connect

send

recv

TOP

本帖最后由 Demon 于 2012-10-5 23:51 编辑

源码大概是这样(该代码仅为汇编还原,不代表本人的编码水平)
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <winsock2.h>
  4. #include <windows.h>
  5. SOCKET g_socket;
  6. char backspace[256], buffer[1024];
  7. int crlf, total, count;
  8. int len = 1;
  9. int init()
  10. {
  11.     int result = 0;
  12.     struct WSAData WSAData;
  13.     memset(&WSAData, 0, sizeof(WSAData));
  14.     if (WSAStartup(MAKEWORD(2, 2), &WSAData))
  15.         result = WSAGetLastError();
  16.     else
  17.         g_socket = socket(AF_INET, SOCK_STREAM, 0);
  18.     return result;
  19. }
  20. char *getip(char *name)
  21. {
  22.     struct hostent *h = gethostbyname(name);
  23.     return inet_ntoa(*(struct in_addr *)h->h_addr_list[0]);
  24. }
  25. int cleanup()
  26. {
  27.     WSACleanup();
  28.     return 0;
  29. }
  30. int download(char *host, u_short port, char *path)
  31. {
  32.     char *filename, *ip;
  33.     SOCKADDR a;
  34.     char buf[256], c;
  35.     FILE *fp;
  36.     SOCKET s;
  37.     int cb;
  38.     if (!init()) {
  39.         ip = getip(host);
  40.         filename = strrchr(path, '/');
  41.         if (filename)
  42.             filename++;
  43.         else
  44.             filename = "index.htm";
  45.         a.sa_family = AF_INET;
  46.         *(DWORD *)&a.sa_data[2] = inet_addr(ip);
  47.         *(WORD *)&a.sa_data[0] = htons(port);
  48.         s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  49.         if (connect(s, &a, sizeof(a)))
  50.             return WSAGetLastError();
  51.         memset(buf, 0, 256);
  52.         sprintf(buf, "GET /%s HTTP/1.1\r\nhost:%s\r\naccept:*/*\r\n\r\n", path, ip);
  53.         send(s, buf, strlen(buf), 0);
  54.         fp = fopen(filename, "wb");
  55.         if (!fp) {
  56.             printf("can't open file %s\r\n", filename);
  57.             return 0;
  58.         }
  59.         while (1) {
  60.             cb = recv(s, &c, len, 0);
  61.             if (!cb || cb == -1)
  62.                 break;
  63.             if (crlf == 3) {
  64.                 len = 256;
  65.                 total += cb;
  66.                 printf("%s%s size:%d bytes %.3f kb", backspace, filename, total, (double)total / 1024);
  67.                 if (!fwrite(&c, cb, 1, fp))
  68.                     return 0;
  69.             } else {
  70.                 if (count < 1024)
  71.                     buffer[count++] = c;
  72.                 if (c == '\n') {
  73.                     crlf++;
  74.                 } else {
  75.                     if (crlf && c == '\r')
  76.                         crlf++;
  77.                     else
  78.                         crlf = 0;
  79.                 }
  80.             }
  81.         }
  82.         printf("\r\n\r\n-----connect closed!-----\r\n\r\n");
  83.         fclose(fp);
  84.         printf("\r\n%s\r\n", buffer);
  85.         printf("\r\n-----get more info plz visit http://9674758.qzone.qq.com-----\r\n");
  86.         cleanup();
  87.     }
  88.     return 0;
  89. }
  90. int main(int argc, char **argv)
  91. {
  92.     char *p1, *p2;
  93.     if (argc != 1) {
  94.         if ((p1 = strstr(argv[1], "http://")) != NULL ||
  95.             (p1 = strstr(argv[1], "HTTP://")) != NULL) {
  96.             
  97.             p1 += 7;
  98.             p2 = strchr(p1, '/');
  99.             if (p2 == NULL || (*p2 = 0, ++p2 == '\0'))
  100.                 p2 = "index.htm";
  101.             memset(backspace, 8, 255);
  102.             download(p1, 80, p2);
  103.         }
  104.     }
  105.     return 0;
  106. }
复制代码
1

评分人数

    • CrLf: 技术++技术 + 1

TOP

呃,这样也可以...真恐怖
膜拜楼上

TOP

无语了……

TOP

返回列表