原创作品,转载时请务必以超链接形式标明文章原始出处:http://blog.csdn.net/gqb666/article/details/8970207,作者:gqb666
接上篇博文Linux网络编程之使用TCP传输文件。最近在写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 IP> <端口号> <上传文件名>
server端:./server <端口号> <保存为文件名>
其中,server端先运行,client端与server端的端口号必须一致并且不能与已知端口冲突(例如8888即可)
下面将代码贴上:
server端代码:server.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define FINISH_FLAG "FILE_TRANSPORT_FINISH" #define MAXLINE 1024 void usage(char *command) { printf("usage :%s portnum filename\n", command); exit(0); } int main(int argc,char **argv) { struct sockaddr_in serv_addr; struct sockaddr_in clie_addr; char buf[MAXLINE]; int sock_id; int recv_len; int clie_addr_len; FILE *fp; if (argc != 3) { usage(argv[0]); } /* Create the the file commented by guoqingbo*/ if ((fp = fopen(argv[2], "w")) == NULL) { perror("Creat file failed"); exit(0); } if ((sock_id = socket(AF_INET,SOCK_DGRAM,0)) < 0) { perror("Create socket failed\n"); exit(0); } /*fill the server sockaddr_in struct commented by guoqingbo*/ memset(&serv_addr,0,sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[1])); serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock_id,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0 ) { perror("Bind socket faild\n"); exit(0); } /* server part commented by guoqingbo*/ clie_addr_len = sizeof(clie_addr); bzero(buf, MAXLINE); while (recv_len = recvfrom(sock_id, buf, MAXLINE, 0,(struct sockaddr *)&clie_addr, &clie_addr_len)) { if(recv_len < 0) { printf("Recieve data from client failed!\n"); break; } printf("#"); if ( strstr(buf, FINISH_FLAG) != NULL ) { printf("\nFinish receiver finish_flag\n"); break; } int write_length = fwrite(buf, sizeof(char), recv_len, fp); if (write_length < recv_len) { printf("File write failed\n"); break; } bzero(buf, MAXLINE); } printf("Finish recieve\n"); fclose(fp); close(sock_id); return 0; }client端代码:client.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define FINISH_FLAG "FILE_TRANSPORT_FINISH" #define MAXLINE 1024 void usage(char *command) { printf("usage :%s ipaddr portnum filename\n", command); exit(0); } int main(int argc,char **argv) { FILE *fp; struct sockaddr_in serv_addr; char buf[MAXLINE]; int sock_id; int read_len; int send_len; int serv_addr_len; int i_ret; int i; if (argc != 4) { usage(argv[0]); } /* open the file to be transported commanted by guoqingbo*/ if ((fp = fopen(argv[3],"r")) == NULL) { perror("Open file failed\n"); exit(0); } /* create the socket commanted by guoqingbo*/ if ((sock_id = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Create socket failed"); exit(0); } memset(&serv_addr,0,sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[2])); inet_pton(AF_INET, argv[1], &serv_addr.sin_addr); serv_addr_len = sizeof(serv_addr); /* connect the server commanted by guoqingbo*/ i_ret = connect(sock_id, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)); if (-1 == i_ret) { perror("Connect socket failed!\n"); exit(0); } /* transport the file commented by guoqingbo*/ bzero(buf, MAXLINE); while ( (read_len = fread(buf, sizeof(char), MAXLINE, fp)) > 0 ) { send_len = send(sock_id, buf, read_len, 0); if ( send_len < 0 ) { perror("Send data failed\n"); exit(0); } bzero(buf, MAXLINE); } fclose(fp); /* send the end_flag commented by guoqingbo*/ bzero(buf, MAXLINE); strcpy(buf, FINISH_FLAG); buf[strlen(buf)] = '\0'; for (i = 1000; i>0; i--) { send_len = send(sock_id, buf, strlen(buf)+1, 0); if ( send_len < 0 ) { printf("Finish send the end string\n"); break; } } close(sock_id); printf("Send finish\n"); return 0; }
上接博文: Linux网络编程之使用TCP传输文件