接上篇Linux網絡編程之使用TCP傳輸文件 【http://www.linuxidc.com/Linux/2013-06/85844.htm】。最近在寫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;
}