Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6097718
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类:

2012-05-05 12:07:24

client.c 用于发送文件,可以发送n个文件

点击(此处)折叠或打开

  1. /*
  2.  * client.c
  3.  *
  4.  * Created on: Apr 28, 2012
  5.  * Author: yyp
  6.  */
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <stdlib.h>
  12. #include <dirent.h>
  13. #include <netdb.h>
  14. #include <arpa/inet.h>
  15. #include <netinet/in.h>
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <time.h>

  20. typedef struct com_meg{
  21.     int operation;
  22.     time_t mod_time;
  23.     char file_route[1024];
  24.     int state;
  25.     int type;
  26.     size_t file_size;
  27. } msg_exch;


  28. int sendfile(char *path, int sockfd, size_t filesize);

  29. int main(int argc, char *argv[]){

  30.     
  31.     int sockfd;
  32.     struct sockaddr_in server_addr;
  33.     int socklen;
  34.     int i;
  35.     struct stat file_stat;
  36.     msg_exch msg;

  37.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
  38.          perror("socket foundation");
  39.          exit(EXIT_FAILURE);
  40.      }
  41.     /*
  42.      *命名套接字,
  43.      */
  44.      server_addr.sin_family = AF_INET;
  45.      server_addr.sin_port = htons(8080);
  46.       server_addr.sin_addr.s_addr = inet_addr("10.2.1.163");
  47.      socklen = sizeof(struct sockaddr_in);
  48.      /*
  49.      * 把套接字连接到服务器
  50.            */
  51.      if(connect(sockfd, (struct sockaddr *)&server_addr, socklen) == -1){
  52.          perror("connect failure");
  53.          exit(EXIT_FAILURE);
  54.      }
  55.      else{
  56.         printf("connect sucess\n");
  57.      }

  58.      for(i = 1; i < argc; i++){

  59.             memset(&msg, 0, sizeof(msg));
  60.             /*initialize the exc_msg*/    
  61.             int res;
  62.             res = lstat(argv[i], &file_stat);
  63.             if(res == -1){
  64.                 perror("file stat");
  65.                 exit(EXIT_FAILURE);
  66.             }
  67.             strcpy(msg.file_route, argv[i]);
  68.             msg.type = -1;
  69.             msg.file_size = file_stat.st_size;
  70.             printf("the file route is %s\n",msg.file_route);
  71.             
  72.             /*send exc_msg*/
  73.             if(send(sockfd, &msg, sizeof(msg),0) < 0 ){
  74.                 perror("send pack");
  75.                 exit(EXIT_FAILURE);
  76.             }

  77.             printf("send exch sucess\n");
  78.             if(recv(sockfd, &msg, sizeof(msg), 0) < 0){
  79.                 perror("recv pack");
  80.                 exit(EXIT_FAILURE);
  81.             }
  82.         
  83.             printf("after send the type is %d\n", msg.type);

  84.             printf("recv exch sucess\n");
  85.             if(msg.state != 0){
  86.                 fprintf(stderr,"the server have problem");
  87.                 exit(1);
  88.             }
  89.             
  90.             if(sendfile(argv[i], sockfd, msg.file_size) != 0){
  91.                 fprintf(stderr,"%s",argv[i]);
  92.                 exit(EXIT_FAILURE);
  93.             }
  94.             else{
  95.                 printf("file %s done\n", argv[i]);
  96.             }

  97.             printf("\n one done");
  98.             printf("\n\n");

  99.         }


  100.         return 0;
  101. }

  102. int sendfile(char *path, int sockfd, size_t filesize){
  103.     int nread;
  104.     char buf[2048];
  105.     int fd;
  106.     int res;

  107.     fd = open(path, O_RDONLY);
  108.     if(fd == -1){
  109.         perror("open file");
  110.         exit(EXIT_FAILURE);
  111.     }

  112.     /*if filesize is too small,then send and return*/    
  113.     
  114.     /*file is larger than 2048 then*/
  115.     while((nread = read(fd, buf, 2048)) > 0){
  116.         
  117.         res = send(sockfd, buf, nread, 0);
  118.         if(res == -1){
  119.             perror("sendfile");
  120.             exit(EXIT_FAILURE);
  121.         }
  122.     }
  123.      
  124.     printf("file: %s is done.\n", path);

  125.     return 0;
  126. }
server.c 用于接收文件

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <arpa/inet.h>
  8. #include <fcntl.h>

  9. typedef struct com_meg{
  10.     int operation;
  11.     time_t mod_time;
  12.     char file_route[1024];
  13.     int state;
  14.     int type;
  15.     size_t file_size;
  16. } msg_exch;


  17. int getfile(int clientfd, msg_exch msg);

  18. int main(int argc, char *argv[]){
  19.         int sockfd, clientfd;
  20.         int socklen, clientlen;
  21.         int nrecv;
  22.         int res;

  23.         msg_exch msg;
  24.         struct sockaddr_in server_addr, clietn_addr;
  25.         
  26.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  27.         if(sockfd == -1){
  28.             perror("socket");
  29.             exit(EXIT_FAILURE);
  30.         }
  31.         
  32.         server_addr.sin_family = AF_INET;
  33.         server_addr.sin_port = htons(8080);
  34.         server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  35.         socklen = sizeof(server_addr);

  36.         res = bind(sockfd, (struct sockaddr *)&server_addr, socklen);
  37.         if(res == -1){
  38.             perror("bind");
  39.             exit(EXIT_FAILURE);
  40.         }

  41.         listen(sockfd, 1);
  42.         clientfd = accept(sockfd, (struct sockaddr *)&clietn_addr,(socklen_t *)&clientlen);
  43.         printf("accept one: %d\n", clientfd);
  44.         if(clientfd == -1){
  45.             perror("bind");
  46.             exit(EXIT_FAILURE);
  47.         }
  48.         
  49.         while(1){    
  50.             /*initialize the exch again to against the wrong operation*/
  51.             if(memset(&msg, 0, sizeof(msg)) == NULL){
  52.                 printf("memset error\n");
  53.             }

  54.             int size = 0;
  55.             while(size < sizeof(msg)){
  56.                 nrecv = recv(clientfd, (char *)&msg + size, sizeof(msg)-size, 0);
  57.                 size = nrecv + size;
  58.                 if(nrecv <= 0){
  59.                     fprintf(stderr,"cannot recv exchinfo or all files are done \n");
  60.                     exit(EXIT_FAILURE);
  61.                 }
  62.             }
  63.             
  64.             printf("file: %s\n", msg.file_route);
  65.             if(msg.type == -1){
  66.                 msg.state = 0;
  67.                 send(clientfd, &msg, sizeof(msg), 0);

  68.             }
  69.             else{
  70.                 printf("all files send over\n");
  71.                 send(clientfd, &msg, sizeof(msg), 0);
  72.                 break;
  73.             }
  74.             
  75.             if(getfile(clientfd, msg) == 0){
  76.                 printf("getfile %s\n",msg.file_route);    
  77.             }
  78.             printf("\n");
  79.         }
  80.         
  81.         return 0;
  82. }


  83. int getfile(int clientfd, msg_exch msg){
  84.     
  85.     int nread;
  86.     int nwrite;
  87.     char buf[2048];
  88.      int fd;    
  89.     if((fd= open(msg.file_route, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){
  90.                     perror("create");
  91.                     exit(EXIT_FAILURE);
  92.             }

  93.     
  94.     /*file is larger recv and return 0*/
  95.     size_t size = msg.file_size;
  96.     size_t send_size;
  97.     while(size > 0 ){
  98.         if(size < 2048){
  99.             send_size = size;
  100.         }
  101.         else{
  102.             send_size = 2048;
  103.         }
  104.         nread = recv(clientfd, buf, send_size, 0);
  105.         size = size - nread;
  106.         nwrite = write(fd, buf, nread);
  107.         if(nwrite <= 0){
  108.             fprintf(stderr, "write the the file\n");
  109.             exit(EXIT_FAILURE);
  110.         }
  111.     }
  112.     //sleep(1);
  113.     close(fd);
  114.     return 0;
  115. }

阅读(971) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~