时间:2021-05-20
了解网络传输协议的人都知道,采用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() { int send_id = 0; int receive_id = 0; 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); 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); } 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; 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 { 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); 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语言网络编程带来一定的帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了python实现UDP文件传输的具体代码,供大家参考,具体内容如下UDP协议下文件传输:服务端importsocketcount=0s=so
前几天已经讲过了UDP协议下大文件传输的python实现代码,今天再实现TCP协议下大文件传输的python实现代码。TCP和UDP的实现过程还是比较不一样的。
为了解决SPDY协议底层基于TCP连接导致传输效率低的问题,Google基于UDP实现了一种新型可靠、多路复用的网络传输协议QUIC,它的主要目标是提供低延迟、
数据报组件的使用在网络传输中相当普遍,该组件的传输是基于UDP的传输协议,一种无连接的传输层协议。UDP传输协议在传输过程中,一次只传送少量数据、不可靠的网络应
本文实例讲述了python网络编程之数据传输UDP实现方法。分享给大家供大家参考。具体分析如下:一、问题:你觉得网络上像msn,qq之类的工具在多台机器之间互相