⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 302.htm

📁 unix高级编程原吗
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>apue</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="195.htm">上一层</a>][<a href="303.htm">下一篇</a>]
<hr><p align="left"><small>发信人: Suning (资优生+干苦力), 信区: LINUX <br>

标  题: 一个简明的断点续传程序 <br>

发信站: 武汉白云黄鹤站 (Sat Sep 16 10:59:28 2000), 转信 <br>

  <br>

                        一个简明的断点续传程序. <br>

  <br>

发信站: BBS 水木清华站 (Wed Apr 26 00:14:44 2000) <br>

/*      Get and Resume Elite EDition source code Ver .4 (Uh huh!) <br>

        First off, sorry about the mess and lack of error checking.. <br>

                I'll clean it up later ;) <br>

Get and Resume Elite EDition (GREED) <br>

Copyright (C) 1999  Anoakie Turner <br>

This program is free software; you can redistribute it and/or modify <br>

it under the terms of the GNU General Public License as published by <br>

the Free Software Foundation; either version 2 of the License, or <br>

(at your option) any later version. <br>

This program is distributed in the hope that it will be useful, <br>

but WITHOUT ANY WARRANTY; without even the implied warranty of <br>

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the <br>

GNU General Public License for more details. <br>

You should have received a copy of the GNU General Public License <br>

along with this program; if not, write to the Free Software <br>

Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. <br>



For more information on the GPL, please go to: <br>

http://www.gnu.org/copyleft/gpl.html <br>

        Contact:  Anoakie Turner <br>

                  Anoakie.Turner@asu.edu <br>

                  13240 N. 94th Pl. <br>

                  Scottsdale, AZ 85260 <br>

*/ <br>

/* <br>

**  To compile, use g++ -O -Wall -o greed greed.cpp <br>

*/ <br>

#include <stdio.h> <br>

#include <sys/socket.h> <br>

#include <netinet/in.h> <br>

#include <netdb.h> <br>

#include <unistd.h> <br>

#include <string.h> <br>

#include <stdlib.h> <br>

#include <time.h> <br>

#define HTTP 1 <br>

#define FTP 2 <br>

#define CURRENT_VERSION "GREED/0.4" <br>

#define GGR "http://www.public.asu.edu/~arturner/greed.tar.gz" <br>



#define GGRS "http://www.public.asu.edu/~arturner/greed-source.tar.gz" <br>

#define rollback 4096 <br>

int dlstat; <br>

long dldots; <br>

struct sockaddr_in address; <br>

unsigned int dport; <br>

time_t start, end; <br>

struct URLstruct; <br>

typedef struct URLstruct* URLp; <br>

struct URLstruct <br>

{       char server[512]; <br>

        char file[512]; <br>

        char name[1024]; <br>

        char filename[256]; <br>

        char user[64]; <br>

        char pass[64]; <br>

        char left[10]; <br>

        char size[10]; <br>

        char total[12]; <br>

        char cport[5]; <br>

        unsigned int port; <br>

        int protocol; <br>



        int sockfd; <br>

        struct hostent *host; <br>

        struct servent *serv; <br>

        long long lleft; <br>

        long long lsize; <br>

        long long ltotal; <br>

        FILE *fp; <br>

        bool done; <br>

        bool resume; <br>

        bool retry; <br>

} URLst; <br>

void Download(URLp URL); <br>

bool ReadHTTPHeader(URLp URL); <br>

bool ReadFTPListing(URLp URL); <br>

bool Parse(URLp URL); <br>

bool Connect(URLp URL); <br>

void ReadString(char *str1, int *ind, char stopchar, int stopint, char* str2 <br>

); <br>

void ReadString(char *str1, int &ind, char stopchar, int stopint, char* str2 <br>

) <br>

////////////////////////////////////// <br>

//      void ReadString(char *str1, int &ind, char stopchar, <br>



//                      int stopint, char* str2); <br>

// <br>

// Pre:  Assigned (str1) && Assigned (ind) && Assigned (stopchar) <br>

//       && Assigned (stopint) <br>

// Post: a string starting at ind and ending at (stopchar || <br>

//       stopint) is copied int str2 <br>

////////////////////////////////////// <br>

{       int stringIndex = 0; <br>

        for (; ind < stopint && str1[ind] != stopchar; ind++) <br>

                str2 [stringIndex++] = str1[ind]; <br>

        str2 [stringIndex] = '\0'; <br>

} <br>

bool Parse(URLp URL) <br>

////////////////////////////////////// <br>

//      bool Parse(URLp URL); <br>

// <br>

//      Parses a URL <br>

// <br>

// Pre:  Assigned (URL->name) <br>

// Post: URL->name parsed, URL->(filename, file, protocol, user, pass, cport <br>

, seerver, port) <br>

//       set.  Returns 1 if successful. <br>



////////////////////////////////////// <br>

{       int at = 0; <br>

        int i = 0; <br>

        dlstat = 0; <br>

        dldots = 0; <br>

        URL->retry = 0; <br>

        for(i = strlen(URL->name); i > 0 && URL->name[i] != '@'; i--); <br>

        if(URL->name[i] == '@') at = i; <br>

        if (strstr(URL->name, "http://") != NULL || strstr(URL->name, "ftp:/ <br>

/") != NULL) <br>

        {       if(URL->name[0] == 'f') <br>

                {       i = 6; <br>

                        URL->protocol = FTP; <br>

                } <br>

                else <br>

                {       i = 7; <br>

                        URL->protocol = HTTP; <br>

                } <br>

                if (at) <br>

                {       ReadString(URL->name, i, ':', at, URL->user); <br>

                        i++,ReadString(URL->name, i, '\0', at, URL->pass); <br>

                        i++; <br>



                } <br>

                else <br>

                {       strcpy(URL->user, "anonymous"); <br>

                        strcpy(URL->pass, "-anonuser@anonymous.com"); <br>

                } <br>

        } else <br>

        {       i = 0; <br>

                if(URL->name[0] == 'f') <br>

                        URL->protocol = FTP; <br>

                else <br>

                        URL->protocol = HTTP; <br>

        } <br>

        at = i; <br>

        ReadString(URL->name, i, ':', strlen(URL->name), URL->server); <br>

        if (URL->name[i] != ':') <br>

        {       i = at; <br>

                ReadString(URL->name, i, '/', strlen(URL->name), URL->server <br>

); <br>

                if (URL->protocol == FTP) <br>

                        strcpy (URL->cport, "21"); <br>

                else <br>

                        strcpy (URL->cport, "80"); <br>



        } <br>

        else <br>

                i++,ReadString(URL->name, i, '/', strlen(URL->name), URL->cp <br>

ort) <br>

        ReadString(URL->name, i, '\0', strlen(URL->name), URL->file); <br>

        for (i = strlen(URL->name); i > 0 && URL->name[i] != '/'; i--); <br>

        i++,ReadString(URL->name, i, '\0', strlen(URL->name), URL->filename) <br>

; <br>

        if (URL->file[0] == '\0') <br>

        {       strcpy (URL->file, "/"); <br>

                strcpy (URL->filename, "index.html"); <br>

        } else if (URL->file[strlen(URL->file) - 1] == '/') <br>

                strcpy (URL->filename, "index.html"); <br>

        URL->port = atoi(URL->cport); <br>

        return (1); <br>

} <br>

bool Connect(URLp URL) <br>

////////////////////////////////////// <br>

// <br>

// <br>

// <br>

// <br>

// <br>

////////////////////////////////////// <br>

{       URL->host = gethostbyname(URL->server); <br>

        if (URL->host == NULL) <br>

        {       printf("Error resolving host!\n"); <br>

                return(0); <br>

        } <br>

        printf("Connecting to: %s:%d\n", URL->server, URL->port); <br>

        URL->sockfd = socket(AF_INET, SOCK_STREAM, 0); <br>

        if (URL->sockfd == -1) <br>

        {       printf("Error opening socket to server\n"); <br>

                return(0); <br>

        } <br>

        address.sin_family = AF_INET; <br>

        address.sin_port = htons(URL->port); <br>

        memcpy(&address.sin_addr, URL->host->h_addr, URL->host->h_length); <br>

        if(connect(URL->sockfd, (struct sockaddr *)&address, sizeof(address) <br>

) < 0) <br>

        {       printf("Error in connecting to server!\n"); <br>

                return(0); <br>

        } else <br>

        if (URL->protocol == HTTP) <br>

        {       write(URL->sockfd, "GET ", 4); <br>



                write(URL->sockfd, URL->file, strlen(URL->file)); <br>

                write(URL->sockfd, " HTTP/1.1\r\nHost: ", 17); <br>

                write(URL->sockfd, URL->server, strlen(URL->server)); <br>

                write(URL->sockfd, ":", 1); <br>

                write(URL->sockfd, URL->cport, strlen(URL->cport)); <br>

                write(URL->sockfd, "\r\nUser-Agent: ", 14); <br>

                write(URL->sockfd, CURRENT_VERSION, strlen(CURRENT_VERSION)) <br>

; <br>

                write(URL->sockfd, "\r\nAccept: *.*, */*\r\n", 20); <br>

                write(URL->sockfd, "Range: bytes=", 13); <br>

        } else <br>

        {       write(URL->sockfd, "USER ", 5); <br>

                write(URL->sockfd, URL->user, strlen(URL->user)); <br>

                write(URL->sockfd, "\r\nPASS ", 7); <br>

                write(URL->sockfd, URL->pass, strlen(URL->pass)); <br>

                write(URL->sockfd, "\r\nPASV\r\nREST 0\r\n", 16); <br>

                write(URL->sockfd, "NOOP\r\n", 6); <br>

                URL->resume = 1; <br>

                if (strstr(URL->user, "anonymous") != NULL) <br>

                        write(URL->sockfd, "LIST -L ", 8); <br>

                else <br>

                        write(URL->sockfd, "LIST -L ~", 9); <br>



                write(URL->sockfd, URL->file, strlen(URL->file)); <br>

                write(URL->sockfd, "\r\n", 2); <br>

        } <br>

        return 1; <br>

} <br>

bool ReadHTTPHeader(URLp URL) <br>

////////////////////////////////////// <br>

//      bool ReadHTTPHeader(URLp URL) <br>

// <br>

//      URL->protcol == HTTP, HTTP header read. <br>

// <br>

// Pre:  Assigned(URL->size) && Assigned(URL->filename) && Assigned(URL->soc <br>

kfd) <br>

// Post: URL->fp, URL->lsize, URL->left, and URL->lleft set. Returns 1 <br>

//       if successful <br>

////////////////////////////////////// <br>

{       int bsize = 0; <br>

        char *temp, *fin; <br>

        char buffer[4096]; <br>

        int i; <br>

        URL->done = 0; <br>

        URL->fp = fopen (URL->filename, "a+b"); <br>



        URL->lsize = ftell(URL->fp); <br>

        if ((URL->lsize >= URL->ltotal) && URL->ltotal != 0) <br>

        {       printf("Error... It seems this file has already been downloa <br>

ded..\n"); <br>

                return(0); <br>

        } else <br>

        if (URL->fp == NULL) <br>

        {       printf("Error opening file %s\n", URL->filename); <br>

                return(0); <br>

        } else <br>

        if (URL->lsize == -1) <br>

                URL->lsize = 0; <br>

        sprintf(URL->size, "%lld", URL->lsize); <br>

        write(URL->sockfd, URL->size, strlen(URL->size)); <br>

        write(URL->sockfd, "-\r\n\r\n", 5); <br>

        dldots = URL->lsize / rollback; <br>

        bsize = read(URL->sockfd, buffer, sizeof(buffer) - 1); <br>

        if (!(fin=strstr(buffer,"\r\n\r\n"))) <br>

        {       printf("Error - webserver headers too large, fatal!\n"); <br>

                return(0); <br>

        } <br>

        *fin='\0'; fin+=4; <br>



        if (strstr(buffer,"Transfer-Encoding: chunked")) <br>

        {       printf("Error - \"chunked\" Transfer-Encoding not implemente <br>

d, II'm violating HTTP 1.1!\n"); <br>

                return(0); <br>

        } <br>

        if (URL->lsize && !strstr(buffer,"Content-Range:")) <br>

        {       printf("Error - server says we already have the completed fi <br>

le, resuming too dangerous!\n"); <br>

                return(0); <br>

        } <br>

        printf("Resuming from %s bytes.\r\n", URL->size); <br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -