国产九九热视频-国产九九精品视频-国产精自产拍久久久久久蜜-国产精自产拍久久久久久-国产精在线-国产精亚洲视频

以文本方式查看主題

-  曙海教育集團(tuán)論壇  (http://www.hkzrsy.com/bbs/index.asp)
--  Linux應(yīng)用開發(fā)  (http://www.hkzrsy.com/bbs/list.asp?boardid=32)
----  linux下的應(yīng)用開發(fā)起步--簡化螞蟻制作(三)  (http://www.hkzrsy.com/bbs/dispbbs.asp?boardid=32&id=1660)

--  作者:wangxinxin
--  發(fā)布時間:2010-11-23 10:29:54
--  linux下的應(yīng)用開發(fā)起步--簡化螞蟻制作(三)

C.前面看了一大段是不是有點云里霧里的啊?是有點羅嗦,但是俗話說:“萬事總是開頭難”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( );

  //對命令行傳進(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);

  }

  }

  }

  /

 


主站蜘蛛池模板: 台北县| 将乐县| 曲水县| 稻城县| 乳源| 富平县| 秦安县| 宜黄县| 曲沃县| 新竹市| 福建省| 静海县| 平和县| 通海县| 长丰县| 宝坻区| 宜良县| 元朗区| 托里县| 福鼎市| 泗阳县| 灌云县| 淮安市| 拉萨市| 涟源市| 开化县| 云梦县| 正蓝旗| 邻水| 张家港市| 玉山县| 腾冲县| 徐水县| 云安县| 绥江县| 进贤县| 仙游县| 抚远县| 浮山县| 桐梓县| 满洲里市|