熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

linux下的應用開發起步--簡化螞蟻制作(三)

2022-06-13   來源: Oracle 

   C前面看了一大段是不是有點雲裡霧裡的啊?是有點羅嗦但是俗話說萬事總是開頭難OK現在總算可以寫主程序文件了
  
    下面就是antc文件
  
  #include anth
  #include signalh
  //表示不用表示用代理
  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;
  //響應異常退出將urlant數據結構中的變量值存放到log文件
  void justpause()
  {
  int i;
  FILE* log;
  log=fopen(logfilew);
  fprintf(log%s msurl);
  for(i=;iamount!=)
  fprintf(log %d:%d ants[i]>positionants[i]>amount);
  fclose(log);
  exit();
  }
  //總算挨到主程序了呵呵
  int main(int argcchar** argv)
  {
  int n;
  int maxfd;
  char savefile[];
  FILE* goods;
  FILE* log;
  fd_set rsetaset;
  int finished;
  int length;
  char status[];
  char c;
  struct sigaction sa;
  //初始化環境參數
  init_env( );
  //對命令行傳進來的參數做判斷
  while((c=getopt(argcargvn:u:s:p:l:h))!=EOF){
  switch (c){
  //取得螞蟻數目
  case n:
  ant_num=atoi(optarg);
  if(ant_num > MAXANTNUM)
  ant_num = MAXANTNUM;
  break;
  //取得目標的url地址
  case u:
  strcpy(msurl optarg);
  break;
  //取得代理的地址
  case s:
  strcpy(proxy_server optarg);
  use_proxy=;
  break;
  //取得代理的端口
  case p:
  use_proxy=;
  proxy_port=(u_short)atoi(optarg);
  break;
  //取得日志文件名
  case l:
  strcpy(logfileoptarg);
  break;
  case h:
  printf(usage: ant n antnum u url s proxyserver p
  prxoy_port l logfile );
  return ;
  }
  }
  //從日志文件中讀取url
  if(logfile[] != ){
  log = fopen(logfile r+);
  strcpy(msurlget_url_from_log(log));
  }
  //將值賦代理服務器的相關變量
  if(use_proxy){
  strcpy(mshostproxy_server);
  msport=proxy_port;
  }else{
  strcpy(mshost extract_from_url(msurl SITENAME));
  msport = atoi(extract_from_url(msurl PORTNAME));
  }
  //如果有下載日志從日志中取相關信息
  if(logfile[] != ){
  printf(The file you havent downloadcontinue);
  n=;
  strcpy(msurlget_url_from_log(log));
  while () {
  ants[n] = (struct Ant*)malloc(sizeof(struct Ant));
  memset(ants[n] sizeof(struct Ant));
  if(!(assign_mission(log ants[n])))
  break;
  n++;
  if(n > MAXANTNUM){
  printf(You logfile must be wrong! );
  exit();
  }
  }
  fclose(log);
  free(ants[n]);
  ant_num=n;
  }
  else{
   //取得下載文件的大小為每只螞蟻分配下載位置
  bulk=get_size_of_url(&ms);
  if(bulk==){
   printf(Cannot get the size of this object! );
  ant_num=;
  ants[] = (struct Ant*)malloc(sizeof(struct Ant));
  ants[]>position=;
  ants[]>amount=;
  }
  else if(bulk==){
   printf(Cannot connect to the site! );
   exit();
  }
  else{
  for(n=; nposition = (bulk/ant_num)*n;
  if(n!=(ant_num))
  ants[n]>amount = bulk/ant_num;
  else
  ants[n]>amount = bulk(bulk/ant_num)*(ant_num);
  }
  }
  }
  //}
  maxfd=;
  //重設&rset
  FD_ZERO(&rset);
  //下載按螞蟻數開線程
  for(n=; nants[n]>s?maxfd:ants[n]>s;
  }
  }
  if(!maxfd){
  printf(no ant go work! );
  return ;
  }
  memset(&sasizeof(struct sigaction));
   //打開存儲文件
  strcpy(savefile extract_from_url(msurl FILENAME));
  if(!(goods=fopen(savefile r+))){
   if(!(goods=fopen(savefilew))){
   printf(Can not open file to save );
   exit();
   }
  }
  strcpy(logfile savefile);
  strcat(logfile log);
  //處理進程改變
  sasa_handler=justpause;
  sasa_flags=SA_ONESHOT;
  sigaction(SIGINT &sa NULL);
  maxfd++;
  finished = ;
  progress = ;
  printf( );
  printf(Downloading now! );
  while(){
  if(finished==ant_num){
  break;
  }
  aset=rset;
  select(maxfd&asetNULLNULLNULL);
  for(n=;ns&aset)){
   if(!(length=receive(ants[n] goods))){
  unenroll(ants[n] &rset);
  finished++;
  }
  }
  }
  }
  unlink(logfile);
  log=(FILE*)NULL;
   //打開日志文件
  if(ant_num!=){
   for(n=;namount){
   if(!log){
  log=fopen(logfile w+);
  fputs(msurl log);
  fputc( log);
   }
   //保存狀態
   save_status(ants[n] log);
  
   printf(Ant %d have not done!%d n+ants[n]>amount);
  }
  else
   printf(Ant %d done! n+);
  free(ants[n]);
   }
  }
  else
   free(ants[]);
  fclose(goods);
  if(log)
  fclose(log);
  return ;
  }
  
    D寫完程序下面就是做makefile程序比較小所以MAKEFILE也比較簡單
  
  CC = gcc
  CFLAGS =
  source=anth antc funcsc
  object=anto funcso
  ant:$(object)
  $(CC) $(object) o $@
  %o:%c
  $(CC) c $< o $@ $(CFLAGS)
  bak:
  tar czvf anttgz Makefile $(source)
  rm f $(object)
  
    OK到這總算飛到目的地了現在你只要在命令行下用make命令就可以編譯了
  
    教訓
  
    有一個錯誤調了很久才發現就是當fprintf(log%s msurl)這樣的語句的指針變量為空時程序執行過程中就會發生Segmentation fault (core dumped)這樣的錯誤另外由於程序沒有多加判斷在輸入url時千萬要帶上//ftp://哪位網友有興趣可以自己修改
  

From:http://tw.wingwit.com/Article/program/Oracle/201311/16787.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.