Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->Linux程序设计 ->正文

内核技术:TCP/IP编程实现远程文件传输

来源: 作者: 时间:2007-04-11 点击: [收藏] [投稿]

  while((k=read(source,buf,sizeof(buf)))>;0)

   write(ns,buf,k);

  printf(″\n\n\t\t传输完毕!!!\n″);

  close(ns);

}

  close(source);

  exit(0);

  /*客户机源程序khj.c*/

  #include;

  #include;

  #include;

  #include;

  #include;

  #include;

  #include;

  #include ;

  main()

  {

   char buf[1024],file[30];

   char *strs=″\n\n\t\t正在接收文件″;

   int target;

   register int k,s;

   struct sockaddr_in sin;

   struct hostent *hp;

   system(″clear″);

   printf(″\n″);

   

   hp=gethostbyname(″server″);

   if(hp==NULL){

          perror(″返回服务器地址信息错!!!″);

    exit(1);

   }

   s=socket(AF_INET,SOCK_STREAM,0);

   if(s<0){

    perror(″获取SOCKET号失败!!!″);

    exit(2);

   }

   sin.sin_family=AF_INET;

   sin.sin_port=htons(1500);/*端口号需与服务器程序使用的一致*/

   bcopy(hp->;h_addr,&sin.sin_addr,hp->;h_length);

   printf(″\n\n\t\t正在与服务器连接…″);

   if(connect(s,&sin,sizeof(sin),0)<0){

    perror(″不能与服务器连接!!!″);

    exit(3);

   }

   while((k=read(s,file,sizeof(file)))<=0/*接收文件名*/

   if((target=open(file,o_WRONLY|O_CREAT|O_TRUNC,0644))<0){

    perror(″不能打开目标文件!!″);

    exit(4);

  }

  strcat(strs,file);

  strcat(strs,″,稍候…″);

  write(1,strs,strlen(strs));

  while((k=read(s,buf,sizeof(buf)))>;0)

   write(tatget,buf,k);

  printf(″\n\n\t\t接收文件成功!!!\n″);

  close(s);

  close(target);

  }

  上述程序在Sco Unix System v3.2及Sco TCP/IP Rumtime环境下调试通过。

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:Linux系统套接字编程中存在的五个隐患   下一篇:Linux实时内存数据库eXtremeDB性能

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号