Linux收集編程之基於UDP完成靠得住的文件傳輸示例。本站提示廣大學習愛好者:(Linux收集編程之基於UDP完成靠得住的文件傳輸示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Linux收集編程之基於UDP完成靠得住的文件傳輸示例正文
懂得收集傳輸協定的人都曉得,采取TCP完成文件傳輸很簡略。絕對於TCP,因為UDP是面向無銜接、弗成靠的傳輸協定,所以我們須要斟酌丟包和後發先至(包的次序)的成績,所以我們想要完成UDP傳輸文件,則須要處理這兩個成績。辦法就是給數據包編號,依照包的次序吸收並存儲,吸收端吸收到數據包後發送確認信息給發送端,發送端吸收確認數據今後再持續發送下一個包,假如吸收端收到的數據包的編號不是希冀的編號,則請求發送端從新發送。
上面展現的是基於linux下C說話完成的一個示例法式,該法式界說一個包的構造體,個中包括數據和包頭,包頭裡包括有包的編號和數據年夜小,經由測試後,該法式可以勝利傳輸一個視頻文件。
詳細完成代碼以下:
server端代碼以下:
/************************************************************************* > File Name: server.c > Author: SongLee ************************************************************************/ #include<sys/types.h> #include<sys/socket.h> #include<unistd.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<netdb.h> #include<stdarg.h> #include<string.h> #define SERVER_PORT 8000 #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE 512 /* 包頭 */ typedef struct { int id; int buf_size; }PackInfo; /* 吸收包 */ struct SendPack { PackInfo head; char buf[BUFFER_SIZE]; } data; int main() { /* 發送id */ int send_id = 0; /* 吸收id */ int receive_id = 0; /* 創立UDP套接口 */ struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(SERVER_PORT); /* 創立socket */ int server_socket_fd = socket(AF_INET, SOCK_DGRAM, 0); if(server_socket_fd == -1) { perror("Create Socket Failed:"); exit(1); } /* 綁定套接口 */ if(-1 == (bind(server_socket_fd,(struct sockaddr*)&server_addr,sizeof(server_addr)))) { perror("Server Bind Failed:"); exit(1); } /* 數據傳輸 */ while(1) { /* 界說一個地址,用於捕捉客戶端地址 */ struct sockaddr_in client_addr; socklen_t client_addr_length = sizeof(client_addr); /* 吸收數據 */ char buffer[BUFFER_SIZE]; bzero(buffer, BUFFER_SIZE); if(recvfrom(server_socket_fd, buffer, BUFFER_SIZE,0,(struct sockaddr*)&client_addr, &client_addr_length) == -1) { perror("Receive Data Failed:"); exit(1); } /* 從buffer中拷貝出file_name */ char file_name[FILE_NAME_MAX_SIZE+1]; bzero(file_name,FILE_NAME_MAX_SIZE+1); strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(buffer)); printf("%s\n", file_name); /* 翻開文件 */ FILE *fp = fopen(file_name, "r"); if(NULL == fp) { printf("File:%s Not Found.\n", file_name); } else { int len = 0; /* 每讀取一段數據,便將其發給客戶端 */ while(1) { PackInfo pack_info; if(receive_id == send_id) { ++send_id; if((len = fread(data.buf, sizeof(char), BUFFER_SIZE, fp)) > 0) { data.head.id = send_id; /* 發送id放進包頭,用於標志次序 */ data.head.buf_size = len; /* 記載數據長度 */ if(sendto(server_socket_fd, (char*)&data, sizeof(data), 0, (struct sockaddr*)&client_addr, client_addr_length) < 0) { perror("Send File Failed:"); break; } /* 吸收確認新聞 */ recvfrom(server_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&client_addr, &client_addr_length); receive_id = pack_info.id; } else { break; } } else { /* 假如吸收的id和發送的id不雷同,從新發送 */ if(sendto(server_socket_fd, (char*)&data, sizeof(data), 0, (struct sockaddr*)&client_addr, client_addr_length) < 0) { perror("Send File Failed:"); break; } /* 吸收確認新聞 */ recvfrom(server_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&client_addr, &client_addr_length); receive_id = pack_info.id; } } /* 封閉文件 */ fclose(fp); printf("File:%s Transfer Successful!\n", file_name); } } close(server_socket_fd); return 0; }
client端代碼以下:
/************************************************************************* > File Name: client.c > Author: SongLee ************************************************************************/ #include<sys/types.h> #include<sys/socket.h> #include<unistd.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<netdb.h> #include<stdarg.h> #include<string.h> #define SERVER_PORT 8000 #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE 512 /* 包頭 */ typedef struct { int id; int buf_size; }PackInfo; /* 吸收包 */ struct RecvPack { PackInfo head; char buf[BUFFER_SIZE]; } data; int main() { int id = 1; /* 辦事端地址 */ struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); server_addr.sin_port = htons(SERVER_PORT); socklen_t server_addr_length = sizeof(server_addr); /* 創立socket */ int client_socket_fd = socket(AF_INET, SOCK_DGRAM, 0); if(client_socket_fd < 0) { perror("Create Socket Failed:"); exit(1); } /* 輸出文件名到緩沖區 */ char file_name[FILE_NAME_MAX_SIZE+1]; bzero(file_name, FILE_NAME_MAX_SIZE+1); printf("Please Input File Name On Server: "); scanf("%s", file_name); char buffer[BUFFER_SIZE]; bzero(buffer, BUFFER_SIZE); strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name)); /* 發送文件名 */ if(sendto(client_socket_fd, buffer, BUFFER_SIZE,0,(struct sockaddr*)&server_addr,server_addr_length) < 0) { perror("Send File Name Failed:"); exit(1); } /* 翻開文件,預備寫入 */ FILE *fp = fopen(file_name, "w"); if(NULL == fp) { printf("File:\t%s Can Not Open To Write\n", file_name); exit(1); } /* 從辦事器吸收數據,並寫入文件 */ int len = 0; while(1) { PackInfo pack_info; if((len = recvfrom(client_socket_fd, (char*)&data, sizeof(data), 0, (struct sockaddr*)&server_addr,&server_addr_length)) > 0) { if(data.head.id == id) { pack_info.id = data.head.id; pack_info.buf_size = data.head.buf_size; ++id; /* 發送數據包確認信息 */ if(sendto(client_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&server_addr, server_addr_length) < 0) { printf("Send confirm information failed!"); } /* 寫入文件 */ if(fwrite(data.buf, sizeof(char), data.head.buf_size, fp) < data.head.buf_size) { printf("File:\t%s Write Failed\n", file_name); break; } } else if(data.head.id < id) /* 假如是重發的包 */ { pack_info.id = data.head.id; pack_info.buf_size = data.head.buf_size; /* 重發數據包確認信息 */ if(sendto(client_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&server_addr, server_addr_length) < 0) { printf("Send confirm information failed!"); } } else { } } else { break; } } printf("Receive File:\t%s From Server IP Successful!\n", file_name); fclose(fp); close(client_socket_fd); return 0; }
感興致的同伙可以著手測試一下該法式,信任會對年夜家的Linux下C說話收集編程帶來必定的贊助。