Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209136
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-22 15:50
文章存档

2014年(16)

2013年(16)

分类: LINUX

2013-12-18 18:22:38

                Linux网络编程之使用UDP传输文件
    本文转载自http://blog.csdn.net/gqb_driver/article/details/8970207 作者:gqb666

    最近在写Linux网络方面的demo,用UDP实现了一个简单的传输文件程序,适用于网卡设备及TCP/IP协议栈及网络环境测试时使用。当然这里要说的是,一般通过网络传输文件不会选择UDP协议,因为UPD提供不可靠的传输,文件传输过程中会出现丢包现象而导致文件传输错误。这里要实现UDP传输文件的目的是为了测试网卡的丢包率,然后与正常网络的丢包率进行比较,从而可以检验网卡及网络环境的质量。本文仅将其雏形写出。其功能是使用UDP协议从client端向server端传输文件,用法如下:

     编译:

           client:gcc -o client client.c

           server:gcc -o server server.c

     运行:


           client端:./client <端口号> <上传文件名>

           server端:./server <端口号> <保存为文件名>

其中,server端先运行,client端与server端的端口号必须一致并且不能与已知端口冲突(例如8888即可)

下面将代码贴上:

server端代码:server.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10.   
  11. #define FINISH_FLAG "FILE_TRANSPORT_FINISH"
  12. #define MAXLINE 1024
  13.   
  14. void usage(char *command)
  15. {
  16.     printf("usage :%s portnum filename\n", command);
  17.     exit(0);
  18. }
  19. int main(int argc,char **argv)
  20. {
  21.     struct sockaddr_in serv_addr;
  22.     struct sockaddr_in clie_addr;
  23.     char buf[MAXLINE];
  24.     int sock_id;
  25.     int recv_len;
  26.     int clie_addr_len;
  27.     FILE *fp;
  28.   
  29.     if (argc != 3) {
  30.         usage(argv[0]);
  31.     }
  32.     /* Create the the file commented by guoqingbo*/
  33.     if ((fp = fopen(argv[2], "w")) == NULL) {
  34.         perror("Creat file failed");
  35.         exit(0);
  36.     }
  37.     if ((sock_id = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
  38.         perror("Create socket failed\n");
  39.         exit(0);
  40.     }
  41.     /*fill the server sockaddr_in struct commented by guoqingbo*/
  42.     memset(&serv_addr,0,sizeof(serv_addr));
  43.     serv_addr.sin_family = AF_INET;
  44.     serv_addr.sin_port = htons(atoi(argv[1]));
  45.     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  46.   
  47.     if (bind(sock_id,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0 ) {
  48.         perror("Bind socket faild\n");
  49.         exit(0);
  50.     }
  51.     /* server part commented by guoqingbo*/
  52.     clie_addr_len = sizeof(clie_addr);
  53.     bzero(buf, MAXLINE);
  54.     while (recv_len = recvfrom(sock_id, buf, MAXLINE, 0,(struct sockaddr *)&clie_addr, &clie_addr_len)) {
  55.         if(recv_len < 0) {
  56.             printf("Recieve data from client failed!\n");
  57.             break;
  58.         }
  59.         printf("#");
  60.         if ( strstr(buf, FINISH_FLAG) != NULL ) {
  61.             printf("\nFinish receiver finish_flag\n");
  62.             break;
  63.         }
  64.         int write_length = fwrite(buf, sizeof(char), recv_len, fp);
  65.         if (write_length < recv_len) {
  66.             printf("File write failed\n");
  67.             break;
  68.         }
  69.         bzero(buf, MAXLINE);
  70.     }
  71.   
  72.     printf("Finish recieve\n");
  73.     fclose(fp);
  74.     close(sock_id);
  75.     return 0;
  76. }
client端代码:client.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11.   
  12. #define FINISH_FLAG "FILE_TRANSPORT_FINISH"
  13. #define MAXLINE 1024
  14.   
  15. void usage(char *command)
  16. {
  17.     printf("usage :%s ipaddr portnum filename\n", command);
  18.     exit(0);
  19. }
  20. int main(int argc,char **argv)
  21. {
  22.     FILE *fp;
  23.     struct sockaddr_in serv_addr;
  24.     char buf[MAXLINE];
  25.     int sock_id;
  26.     int read_len;
  27.     int send_len;
  28.     int serv_addr_len;
  29.     int i_ret;
  30.     int i;
  31.   
  32.     if (argc != 4) {
  33.         usage(argv[0]);
  34.     }
  35.     /* open the file to be transported commanted by guoqingbo*/
  36.     if ((fp = fopen(argv[3],"r")) == NULL) {
  37.         perror("Open file failed\n");
  38.         exit(0);
  39.     }
  40.     /* create the socket commanted by guoqingbo*/
  41.     if ((sock_id = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  42.         perror("Create socket failed");
  43.         exit(0);
  44.     }
  45.     memset(&serv_addr,0,sizeof(serv_addr));
  46.     serv_addr.sin_family = AF_INET;
  47.     serv_addr.sin_port = htons(atoi(argv[2]));
  48.     inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);
  49.     serv_addr_len = sizeof(serv_addr);
  50.     /* connect the server commanted by guoqingbo*/
  51.     i_ret = connect(sock_id, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
  52.     if (-1 == i_ret) {
  53.         perror("Connect socket failed!\n");
  54.         exit(0);
  55.     }
  56.     /* transport the file commented by guoqingbo*/
  57.     bzero(buf, MAXLINE);
  58.     while ( (read_len = fread(buf, sizeof(char), MAXLINE, fp)) > 0 ) {
  59.         send_len = send(sock_id, buf, read_len, 0);
  60.         if ( send_len < 0 ) {
  61.             perror("Send data failed\n");
  62.             exit(0);
  63.         }
  64.         bzero(buf, MAXLINE);
  65.     }
  66.     fclose(fp);
  67.     /* send the end_flag commented by guoqingbo*/
  68.     bzero(buf, MAXLINE);
  69.     strcpy(buf, FINISH_FLAG);
  70.     buf[strlen(buf)] = '\0';
  71.     for (i = 1000; i>0; i--) {
  72.         send_len = send(sock_id, buf, strlen(buf)+1, 0);
  73.         if ( send_len < 0 ) {
  74.             printf("Finish send the end string\n");
  75.             break;
  76.         }
  77.     }
  78.     close(sock_id);
  79.     printf("Send finish\n");
  80.     return 0;
  81. }
阅读(3084) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~