Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418794
  • 博文数量: 247
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 1005
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-10 10:39
文章分类

全部博文(247)

文章存档

2015年(3)

2014年(21)

2013年(53)

2012年(170)

分类: LINUX

2013-02-16 14:43:41

原文是在

上发布的(好像这个地址的也是转载^_^)
不过文章中提供的源代码问题确实太多了,昨天一位网友求助让我帮其调试程序,一看,想不到竟是这片文章提供的代码,而且自己还回过贴,呵呵,真是有缘,晚上便将代码调试了一下,顺便做了些格式上的修改,相信大概的思想大家都已经明白了,今天早上调通了试了试还不错,传了个图片和应用程序,蛮快的,贴出来与大家分享一下:

(1):首先在/etc/host中添加Server地址,我的机上是这样
  1. [root@localhost root]# cat /etc/hosts
  2. # Do not remove the following line, or various programs
  3. # that require network functionality will fail.
  4. 127.0.0.1               localhost.localdomain localhost
复制代码

只需要在下面的一行添加上
  1. 192.168.1.21    localhost.localdomain Server
复制代码

就可以了,当然,这里的192.168.1.21要改成你的server端程序所在的机子的IP地址,其次,Server
可以随便改成你喜欢的名字,阿毛阿狗都可以,只要跟后面程序中的host_name中一致就行了
下面贴出代码
服务器端,也就是文件存放端的程序
  1. /**********************FileServer.c******************************/
  2. //Author: May be duanjigang, ^_^
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #define command "clear"
  10. //这里其实应改写成ifdef _WIN32 define command "cls" else define "clear",更合理些
  11. #include
  12. #define  big_size  2048
  13. #define small_size  50
  14. int main(void)
  15. {
  16.         char c= 0, 
  17.         buf[big_size],//读文件的缓冲
  18.         file[small_size],//文件名
  19.   host_name[small_size];//主机名
  20.         int fromlen = 0,source = 0;
  21.         register int k,s,ns;
  22.         struct sockaddr_in sin;
  23.         struct hostent * hp;
  24.         system(command);//清屏
  25.         printf("输入要传输的文件名:");
  26.         memset(file, 0, small_size);
  27.         scanf("%s",file);
  28.         if ((source = open(file, O_RDONLY) ) < 0)
  29.         {
  30.                 perror("源文件打开出错");
  31.                 exit(1);
  32.         }
  33.         memset(host_name, 0, small_size);
  34.   //写入server的名子,然后gethostbyname将会根据这个名子去找主机的信息
  35.   sprintf(host_name, "%s", "Server");
  36.         if((hp = gethostbyname(host_name)) == NULL)
  37.         {
  38.                    perror("返回主机地址信息错!!!");
  39.                    exit(2);
  40.         }
  41.         if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  42.         {
  43.                    printf("获取SOCKET号失败!!!");
  44.                    exit(3);
  45.         }
  46.         sin.sin_family=AF_INET;
  47.         sin.sin_port=htons(1500); 
  48.         memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
  49.         if(bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0 )
  50.         {
  51.                    printf("不能将服务器地址捆绑到SOCKET号上!!!\n");
  52.                    close(s);
  53.                    exit(4);
  54.         }
  55.          if(listen(s,5) < 0)
  56.          {
  57.                    fprintf(stderr, "sever:listen\n");
  58.                    exit(5);
  59.          }
  60.           while(1)
  61.           {
  62.                    printf("等待传送文件中.......\n");
  63.                    if((ns = accept(s, NULL, NULL)) < 0 )
  64.                    {
  65.                            fprintf(stderr, "sever:accept\n");
  66.                            exit(6);
  67.                    }
  68.                    lseek(source,0L,0);
  69.                    printf("开始传送文件.....\n");
  70.         //先把文件名发过求
  71.                    write(ns,file,sizeof(file)); 
  72.                    printf("%s\n", file);
  73.                    //发送文件名
  74.                    memset(buf, 0, big_size);
  75.                    while( (k = read(source, buf, sizeof(buf))) > 0)
  76.                    {
  77.                              //循环发送直到,文件结束
  78.                              write(ns,buf,k);
  79.                        memset(buf, 0, big_size);
  80.                    }
  81.                    printf("\n传输完毕\n");
  82.                    close(ns);
  83.            }
  84.            close(source);
  85.            exit(0);
  86. }  
  87. [code]
  88. 客户端,即要下载文件的机子端程序
  89. [code]
  90. /*****************************FileClient.c********************************/
  91. #include
  92. #include

  93. #include
  94. #include
  95. #include
  96. #include
  97. #define command "clear"

  98. #include
  99. #include

  100. #define big_size 1024
  101. #define small_size 50

  102. int main(void)
  103. {
  104.         char buf[big_size] ={0},file[small_size]={0},
  105.          host_name[small_size]={0};
  106.         int target,ret;
  107.         register int bytes,sockfd;
  108.         struct sockaddr_in sin ;
  109.         struct hostent *hp;
  110.         system(command);
  111.                sprintf(host_name, "%s", "Server");
  112.         if((hp = gethostbyname(host_name)) == NULL)
  113.         {
  114.                 fprintf(stderr, "返回服务器地址信息错!!!\n");
  115.                 exit(1);
  116.         }
  117.         if((sockfd = socket(AF_INET, SOCK_STREAM, 0 )) < 0)
  118.         {
  119.                 fprintf(stderr, "获取SOCKET号失败!!!\n");
  120.                 exit(2);
  121.         }
  122.         
  123.         memset((char*)&sin, '\0', sizeof(sin));
  124.         
  125.         sin.sin_family = AF_INET;
  126.         sin.sin_port = htons(1500);
  127.   
  128.         sin.sin_addr = *(struct in_addr*)hp->h_addr;
  129.         if((ret = connect(sockfd, (struct sockaddr*)&sin, sizeof(sin))) == -1)
  130.         {
  131.                 fprintf(stderr, "不能与服务器连接!!!\n");
  132.                 exit(3);
  133.         }
  134.         memset(buf, 0, big_size);
  135.         while(read(sockfd, file, sizeof(file)) < 0);        
  136.         sprintf(buf, "%s_%s", "receive", file);

  137.         if((target=open(buf, O_WRONLY|O_CREAT|O_TRUNC,0644)) < 0)
  138.         {
  139.                 perror("不能打开目标文件!!");
  140.                 exit(4);
  141.         }
  142.         
  143.         memset(buf, 0, big_size);
  144.         while((bytes = read(sockfd, buf, sizeof(buf))) > 0)
  145.         {
  146.                 write(target,buf,bytes);
  147.                 memset(buf, 0, big_size);        
  148.         }
  149.         printf("接收文件成功!!!\n");
  150.         close(sockfd);
  151.         close(target);
  152. }
复制代码

后话
(1):实现windows和Linux的多平台程序,昨天在windows下调通了,可是客户端老是出错,就没做太多研究,有兴趣的朋友可以尝试一下。
(2):今天忽然想到,既然都做到这个地步了,我们为何不把它作成一个FTP程序,而且,考虑到多客户串行端传输比较费时,所以最好在accept到一个客户端的时候就生成一个线程,实现并行下载,对于FTP我也不是很懂,不过觉的这样应改可以实现吧,后面有空了一定要尝试的,在此先提一下自己的想法,有兴趣的朋友可以自己实现一下,呵呵,最近太忙了,都没时间光顾CU了,学习,学习。。。。。。。
阅读(1135) | 评论(0) | 转发(0) |
0

上一篇:getaddrinfo详解

下一篇:C语言中volatile

给主人留下些什么吧!~~