接上篇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;
}