曙海教育集團(tuán)論壇Linux專區(qū)Linux應(yīng)用開發(fā) → linux下的應(yīng)用開發(fā)起步--簡(jiǎn)化螞蟻制作(三)


  共有7874人關(guān)注過本帖樹形打印

主題:linux下的應(yīng)用開發(fā)起步--簡(jiǎn)化螞蟻制作(三)

美女呀,離線,留言給我吧!
wangxinxin
  1樓 個(gè)性首頁 | 博客 | 信息 | 搜索 | 郵箱 | 主頁 | UC


加好友 發(fā)短信
等級(jí):青蜂俠 帖子:1393 積分:14038 威望:0 精華:0 注冊(cè):2010-11-12 11:08:23
linux下的應(yīng)用開發(fā)起步--簡(jiǎn)化螞蟻制作(三)  發(fā)帖心情 Post By:2010-11-23 10:29:54

C.前面看了一大段是不是有點(diǎn)云里霧里的啊?是有點(diǎn)羅嗦,但是俗話說:“萬事總是開頭難”O(jiān)K,現(xiàn)在總算可以寫主文件了。

  

    下面就是ant.c

  

  #include "ant.h"

  #include "signal.h"

  //0表示不用,1表示用代理

  int use_proxy;

  //代理的地址

  char proxy_server[100];

  //端口

  u_short proxy_port;

  //螞蟻數(shù)目

  int ant_num;

  //下載量

  int bulk;

  //下載進(jìn)程

  int progress;

  char cfgfile[256];

  char logfile[256];

  struct Ant* ants[10];

  struct Mission ms;

  //響應(yīng)異常退出,將url、ant數(shù)據(jù)結(jié)構(gòu)中的變量值,存放到log。

  void justpause()

  {

  int i;

  FILE* log;

  log=fopen(logfile,"w");

  fprintf(log,"%s ",ms.url);

  for(i=0;iamount!=0)

  fprintf(log, "%d:%d ",ants[i]->position,ants[i]->amount);

  fclose(log);

  exit(1);

  }

  //總算挨到主了,呵呵。。。。。

  int main(int argc,char** argv)

  {

  int n;

  int maxfd;

  char savefile[256];

  FILE* goods;

  FILE* log;

  fd_set rset,aset;

  int finished;

  int length;

  char status[80];

  char c;

  struct sigaction sa;

  //初始化環(huán)境參數(shù)

  init_env( );

  //對(duì)命令行傳進(jìn)來的參數(shù)做判斷

  while((c=getopt(argc,argv,"n:u:s:p:l:h"))!=EOF){

  switch (c){

  //取得螞蟻數(shù)目

  case 'n':

  ant_num=atoi(optarg);

  if(ant_num > MAXANTNUM)

  ant_num = MAXANTNUM;

  break;

  //取得目標(biāo)的url地址。

  case 'u':

  strcpy(ms.url, optarg);

  break;

  //取得代理的地址

  case 's':

  strcpy(proxy_server, optarg);

  use_proxy=1;

  break;

  //取得代理的端口

  case 'p':

  use_proxy=1;

  proxy_port=(u_short)atoi(optarg);

  break;

  //取得日志文件名

  case 'l':

  strcpy(logfile,optarg);

  break;

  case 'h':

  printf("usage: ant -n antnum -u url -s proxyserver -p

  prxoy_port -l logfile ");

  return 1;

  }

  }

  //從日志文件中讀取url

  if(logfile[0] != 0){

  log = fopen(logfile, "r+");

  strcpy(ms.url,get_url_from_log(log));

  }

  //將值賦代理的相關(guān)變量

  if(use_proxy){

  strcpy(ms.host,proxy_server);

  ms.port=proxy_port;

  }else{

  strcpy(ms.host, extract_from_url(ms.url, SITENAME));

  ms.port = atoi(extract_from_url(ms.url, PORTNAME));

  }

  //如果有下載日志,從日志中取相關(guān)信息

  if(logfile[0] != 0){

  printf("The file you havent download,continue.......");

  n=0;

  strcpy(ms.url,get_url_from_log(log));

  while (1) {

  ants[n] = (struct Ant*)malloc(sizeof(struct Ant));

  memset(ants[n], 0, sizeof(struct Ant));

  if(!(assign_mission(log, ants[n])))

  break;

  n++;

  if(n > MAXANTNUM){

  printf("You logfile must be wrong! ");

  exit(1);

  }

  }

  fclose(log);

  free(ants[n]);

  ant_num=n;

  }

  else{

   //取得下載文件的大小,為每只螞蟻分配下載位置。

  bulk=get_size_of_url(&ms);

  if(bulk==0){

   printf("Cannot get the size of this object! ");

  ant_num=1;

  ants[0] = (struct Ant*)malloc(sizeof(struct Ant));

  ants[0]->position=0;

  ants[0]->amount=1073741823;

  }

  else if(bulk==-1){

   printf("Cannot connect to the site! ");

   exit(1);

  }

  else{

  for(n=0; nposition = (bulk/ant_num)*n;

  if(n!=(ant_num-1))

  ants[n]->amount = bulk/ant_num;

  else

  ants[n]->amount = bulk-(bulk/ant_num)*(ant_num-1);

  }

  }

  }

  /

 


支持(0中立(0反對(duì)(0單帖管理 | 引用 | 回復(fù) 回到頂部

返回版面帖子列表

linux下的應(yīng)用開發(fā)起步--簡(jiǎn)化螞蟻制作(三)








簽名
主站蜘蛛池模板: 欧美综合第一页| 欧美视频日韩视频| 国产无遮挡裸体免费视频在线观看 | 奇米色在线视频| 久久天天躁狠狠躁夜夜不卡| 狠狠色综合网站久久久久久久| 国产区综合另类亚洲欧美| 999在线视频精品免费播放观看| 日本三级2021最新理论在线观看| 亚洲成a人片在线观看中文动漫| 美女把尿口扒开让男人桶| 国产特级毛片aaaaaa高清| juy031白木优子中文字幕| 日本免费xxx| 亚洲免费观看视频| 狠狠久久精品中文字幕无码| 国产三级网站在线观看播放| 伊人婷婷综合缴情亚洲五月| 好湿好大硬得深一点动态图| 久久久午夜精品福利内容| 欧美夫妇交换俱乐部在线观看| 免费福利视频导航| 青青国产在线视频| 国产精品偷伦视频免费观看了| jizz中国免费| 成年人网站免费观看| 久久精品国产亚洲AV蜜臀色欲 | 欧美va天堂在线电影| 人人爽人人爽人人片a免费| 老师办公室被吃奶好爽在线观看| 国产欧美精品一区二区三区四区| 97精品伊人久久久大香线蕉| 快一点使劲c我在线观看| 久久久久亚洲av无码尤物| 欧欧美18videosex性哦欧美美| 亚洲精品白色在线发布| 精品久久久久久久中文字幕| 国产ts人妖视频| 香港黄页精品视频在线| 国产激情一区二区三区| 久久国产精品二国产精品|