返回列表 发帖

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

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

举例:
down http://dl_dir.qq.com/qqfile/qq/QQ2012/QQ2012.exeCOPY

无语了……

TOP

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

TOP

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

源码大概是这样(该代码仅为汇编还原,不代表本人的编码水平)
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
SOCKET g_socket;
char backspace[256], buffer[1024];
int crlf, total, count;
int len = 1;
int init()
{
    int result = 0;
    struct WSAData WSAData;
    memset(&WSAData, 0, sizeof(WSAData));
    if (WSAStartup(MAKEWORD(2, 2), &WSAData))
        result = WSAGetLastError();
    else
        g_socket = socket(AF_INET, SOCK_STREAM, 0);
    return result;
}
char *getip(char *name)
{
    struct hostent *h = gethostbyname(name);
    return inet_ntoa(*(struct in_addr *)h->h_addr_list[0]);
}
int cleanup()
{
    WSACleanup();
    return 0;
}
int download(char *host, u_short port, char *path)
{
    char *filename, *ip;
    SOCKADDR a;
    char buf[256], c;
    FILE *fp;
    SOCKET s;
    int cb;
    if (!init()) {
        ip = getip(host);
        filename = strrchr(path, '/');
        if (filename)
            filename++;
        else
            filename = "index.htm";
        a.sa_family = AF_INET;
        *(DWORD *)&a.sa_data[2] = inet_addr(ip);
        *(WORD *)&a.sa_data[0] = htons(port);
        s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (connect(s, &a, sizeof(a)))
            return WSAGetLastError();
        memset(buf, 0, 256);
        sprintf(buf, "GET /%s HTTP/1.1\r\nhost:%s\r\naccept:*/*\r\n\r\n", path, ip);
        send(s, buf, strlen(buf), 0);
        fp = fopen(filename, "wb");
        if (!fp) {
            printf("can't open file %s\r\n", filename);
            return 0;
        }
        while (1) {
            cb = recv(s, &c, len, 0);
            if (!cb || cb == -1)
                break;
            if (crlf == 3) {
                len = 256;
                total += cb;
                printf("%s%s size:%d bytes %.3f kb", backspace, filename, total, (double)total / 1024);
                if (!fwrite(&c, cb, 1, fp))
                    return 0;
            } else {
                if (count < 1024)
                    buffer[count++] = c;
                if (c == '\n') {
                    crlf++;
                } else {
                    if (crlf && c == '\r')
                        crlf++;
                    else
                        crlf = 0;
                }
            }
        }
        printf("\r\n\r\n-----connect closed!-----\r\n\r\n");
        fclose(fp);
        printf("\r\n%s\r\n", buffer);
        printf("\r\n-----get more info plz visit http://9674758.qzone.qq.com-----\r\n");
        cleanup();
    }
    return 0;
}
int main(int argc, char **argv)
{
    char *p1, *p2;
    if (argc != 1) {
        if ((p1 = strstr(argv[1], "http://")) != NULL ||
            (p1 = strstr(argv[1], "HTTP://")) != NULL) {
            
            p1 += 7;
            p2 = strchr(p1, '/');
            if (p2 == NULL || (*p2 = 0, ++p2 == '\0'))
                p2 = "index.htm";
            memset(backspace, 8, 255);
            download(p1, 80, p2);
        }
    }
    return 0;
}COPY
1

评分人数

    • CrLf: 技术++技术 + 1

TOP

WSAStartup

socket

gethostbyname

inet_ntoa

inet_addr

ntohs

socket

connect

send

recv

TOP

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

TOP

求源码

TOP

返回列表