C
下面就是ant
#include
#include
//
int use_proxy;
//代理服務器的地址
char proxy_server[
//端口
u_short proxy_port;
//螞蟻數目
int ant_num;
//下載量
int bulk;
//下載進程
int progress;
char cfgfile[
char logfile[
struct Ant* ants[
struct Mission ms;
//響應異常退出
void justpause()
{
int i;
FILE* log;
log=fopen(logfile
fprintf(log
for(i=
fprintf(log
fclose(log);
exit(
}
//總算挨到主程序了
int main(int argc
{
int n;
int maxfd;
char savefile[
FILE* goods;
FILE* log;
fd_set rset
int finished;
int length;
char status[
char c;
struct sigaction sa;
//初始化環境參數
init_env( );
//對命令行傳進來的參數做判斷
while((c=getopt(argc
switch (c){
//取得螞蟻數目
case
ant_num=atoi(optarg);
if(ant_num > MAXANTNUM)
ant_num = MAXANTNUM;
break;
//取得目標的url地址
case
strcpy(ms
break;
//取得代理的地址
case
strcpy(proxy_server
use_proxy=
break;
//取得代理的端口
case
use_proxy=
proxy_port=(u_short)atoi(optarg);
break;
//取得日志文件名
case
strcpy(logfile
break;
case
printf(
prxoy_port
return
}
}
//從日志文件中讀取url
if(logfile[
log = fopen(logfile
strcpy(ms
}
//將值賦代理服務器的相關變量
if(use_proxy){
strcpy(ms
ms
}else{
strcpy(ms
ms
}
//如果有下載日志
if(logfile[
printf(
n=
strcpy(ms
while (
ants[n] = (struct Ant*)malloc(sizeof(struct Ant));
memset(ants[n]
if(!(assign_mission(log
break;
n++;
if(n > MAXANTNUM){
printf(
exit(
}
}
fclose(log);
free(ants[n]);
ant_num=n;
}
else{
//取得下載文件的大小
bulk=get_size_of_url(&ms);
if(bulk==
printf(
ant_num=
ants[
ants[
ants[
}
else if(bulk==
printf(
exit(
}
else{
for(n=
if(n!=(ant_num
ants[n]
else
ants[n]
}
}
}
//}
maxfd=
//重設&rset
FD_ZERO(&rset);
//下載
for(n=
}
}
if(!maxfd){
printf(
return
}
memset(&sa
//打開存儲文件
strcpy(savefile
if(!(goods=fopen(savefile
if(!(goods=fopen(savefile
printf(
exit(
}
}
strcpy(logfile
strcat(logfile
//處理進程改變
sa
sa
sigaction(SIGINT
maxfd++;
finished =
progress =
printf(
printf(
while(
if(finished==ant_num){
break;
}
aset=rset;
select(maxfd
for(n=
if(!(length=receive(ants[n]
unenroll(ants[n]
finished++;
}
}
}
}
unlink(logfile);
log=(FILE*)NULL;
//打開日志文件
if(ant_num!=
for(n=
if(!log){
log=fopen(logfile
fputs(ms
fputc(
}
//保存狀態
save_status(ants[n]
printf(
}
else
printf(
free(ants[n]);
}
}
else
free(ants[
fclose(goods);
if(log)
fclose(log);
return
}
D
CC = gcc
CFLAGS =
source=ant
object=ant
ant:$(object)
$(CC) $(object)
%o:%c
$(CC)
bak:
tar czvf ant
rm
OK
有一個錯誤
From:http://tw.wingwit.com/Article/program/Oracle/201311/16787.html