服务器server.c
/*************************************************************************
> File Name: server.c
> Author: ljh
************************************************************************/
/*
Linux网络编程之基于UDP实现可靠的文件传输示例
这篇文章主要介绍了Linux网络编程之基于UDP实现可靠的文件传输示例,是很实用的技巧,需要的朋友可以参考下
了解网络传输协议的人都知道,采用TCP实现文件传输很简单。相对于TCP,由于UDP是面向无连接、不可靠的传输协议,
所以我们需要考虑丢包和后发先至(包的顺序)的问题,
所以我们想要实现UDP传输文件,则需要解决这两个问题。方法就是给数据包编号,按照包的顺序接收并存储,接收端接收到数据包后发送确认信息给发送端,
发送端接收确认数据以后再继续发送下一个包,如果接收端收到的数据包的编号不是期望的编号,则要求发送端重新发送。
下面展示的是基于linux下C语言实现的一个示例程序,该程序定义一个包的结构体,其中包含数据和包头,包头里包含有包的编号和数据大小,
经过测试后,该程序可以成功传输一个视频文件。
udp传输大文件,解决了udp传输中拆包,组包问题,
1.后包先到
2.数据包确认
3.每包crc32校验(可选,网友说udp解决了顺序组包,应答确认就可以了,协议栈专门有udp校验)
可以说这个udp可靠传输
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"clientarm/log.h"
#define SERVER_PORT 8000
#define BUFFER_SIZE 500 //发送文件udp缓冲区大小
#define FILE_NAME_MAX_SIZE 512
/* 包头 */
typedef struct
{
long int id;
int buf_size;
unsigned int crc32val; //每一个buffer的crc32值
int errorflag;
}PackInfo;
/* 接收包 */
struct SendPack
{
PackInfo head;
char buf[BUFFER_SIZE];
} data;
//----------------------crc32----------------
static unsigned int crc_table[256];
static void init_crc_table(void);
static unsigned int crc32(unsigned int crc, unsigned char * buffer, unsigned int size);
/* 第一次传入的值需要固定,如果发送端使用该值计算crc校验码, 那么接收端也同样需要使用该值进行计算 */
unsigned int crc = 0xffffffff;
/*
* 初始化crc表,生成32位大小的crc表
* 也可以直接定义出crc表,直接查表,
* 但总共有256个,看着眼花,用生成的比较方便.
*/
static void init_crc_table(void)
{
unsigned int c;
unsigned int i, j;
for (i = 0; i < 256; i++)
{
c = (unsigned int)i;
for (j = 0; j < 8; j++)
{
if (c & 1)
c = 0xedb88320L ^ (c >> 1);
else
c = c >> 1;
}
crc_table[i] = c;
}
}
/* 计算buffer的crc校验码 */
static unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)
{
unsigned int i;
for (i = 0; i < size; i++)
{
crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);
}
return crc ;
}
//主函数入口
int main()
{
/* 发送id */
long 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)
{
printe("Create Socket Failed:");
exit(1);
}
/* 绑定套接口 */
if(-1 == (bind(server_socket_fd,(struct sockaddr*)&server_addr,sizeof(server_addr))))
{
printe("Server Bind Failed:");
exit(1);
}
//crc32
init_crc_table();
/* 数据传输 */
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)
{
printe("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));
printi("%s\n", file_name);
/* 打开文件 */
FILE *fp = fopen(file_name, "r");
if(NULL == fp)
{
printi("File:%s Not Found.\n", file_name);
}
else
{
int len = 0;
/* 每读取一段数据,便将其发给客户端 */
while(1)
{
PackInfo pack_info;
bzero((char *)&data,sizeof(data)); //ljh socket发送缓冲区清零
printi("receive_id=%d\n",receive_id);
printi("send_id=%ld\n",send_id);
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; /* 记录数据长度 */
data.head.crc32val = crc32(crc,data.buf,sizeof(data));
printi("len =%d\n",len);
printi("data.head.crc32val=0x%x\n",data.head.crc32val);
//printf("data.buf=%s\n",data.buf);
resend:
if(sendto(server_socket_fd, (char*)&data, sizeof(data), 0, (struct sockaddr*)&client_addr, client_addr_length) < 0)
{
printi("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;
//如果确认包提示数据错误
if(pack_info.errorflag==1)
{
pack_info.errorflag = 0;
goto resend;
}
//usleep(50000);
}
else
{
break;
}
}
else
{
/* 如果接收的id和发送的id不相同,重新发送 */
if(sendto(server_socket_fd, (char*)&data, sizeof(data), 0, (struct sockaddr*)&client_addr, client_addr_length) < 0)
{
printi("Send File Failed:");
break;
}
printi("repeat send\n");
/* 接收确认消息 */
recvfrom(server_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&client_addr, &client_addr_length);
receive_id = pack_info.id;
//usleep(50000);
}
}
//发送结束包 0字节目的告诉客户端发送完毕
if(sendto(server_socket_fd, (char*)&data, 0, 0, (struct sockaddr*)&client_addr, client_addr_length) < 0)
{
printi("Send 0 char Failed:");
break;
}
printi("sever send file end 0 char\n");
/* 关闭文件 */
fclose(fp);
printi("File:%s Transfer Successful!\n", file_name);
//清零id,准备发送下一个文件
/* 发送id */
send_id = 0;
/* 接收id */
receive_id = 0;
}
}
close(server_socket_fd);
return 0;
}
客户端client.c
/*************************************************************************
> File Name: client.c
> Author: ljh
************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"log.h"
#define SERVER_PORT 8000
#define BUFFER_SIZE 500
#define FILE_NAME_MAX_SIZE 512
/* 包头 */
typedef struct
{
long int id;
int buf_size;
unsigned int crc32val; //每一个buffer的crc32值
int errorflag;
}PackInfo;
/* 接收包 */
struct RecvPack
{
PackInfo head;
char buf[BUFFER_SIZE];
} data;
//----------------------crc32----------------
static unsigned int crc_table[256];
static void init_crc_table(void);
static unsigned int crc32(unsigned int crc, unsigned char * buffer, unsigned int size);
/* 第一次传入的值需要固定,如果发送端使用该值计算crc校验码, 那么接收端也同样需要使用该值进行计算 */
unsigned int crc = 0xffffffff;
/*
* 初始化crc表,生成32位大小的crc表
* 也可以直接定义出crc表,直接查表,
* 但总共有256个,看着眼花,用生成的比较方便.
*/
static void init_crc_table(void)
{
unsigned int c;
unsigned int i, j;
for (i = 0; i < 256; i++)
{
c = (unsigned int)i;
for (j = 0; j < 8; j++)
{
if (c & 1)
c = 0xedb88320L ^ (c >> 1);
else
c = c >> 1;
}
crc_table[i] = c;
}
}
/* 计算buffer的crc校验码 */
static unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)
{
unsigned int i;
for (i = 0; i < size; i++)
{
crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);
}
return crc ;
}
//主函数入口
int main()
{
long int id = 1;
unsigned int crc32tmp;
/* 服务端地址 */
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("192.168.26.33");
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)
{
printe("Create Socket Failed:");
exit(1);
}
//crc32
init_crc_table();
/* 输入文件名到缓冲区 */
char file_name[FILE_NAME_MAX_SIZE+1];
bzero(file_name, FILE_NAME_MAX_SIZE+1);
printi("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)
{
printe("Send File Name Failed:");
exit(1);
}
/* 打开文件,准备写入 */
FILE *fp = fopen(file_name, "w");
if(NULL == fp)
{
printe("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)
{
printi("len =%d\n",len);
crc32tmp = crc32(crc,data.buf,sizeof(data));
//crc32tmp=5;
printi("-------------------------\n");
printi("data.head.id=%ld\n",data.head.id);
printi("id=%ld\n",id);
if(data.head.id == id)
{
printi("crc32tmp=0x%x\n",crc32tmp);
printi("data.head.crc32val=0x%x\n",data.head.crc32val);
//printf("data.buf=%s\n",data.buf);
//校验数据正确
if(data.head.crc32val == crc32tmp)
{
printi("rec data success\n");
pack_info.id = data.head.id;
pack_info.buf_size = data.head.buf_size; //文件中有效字节的个数,作为写入文件fwrite的字节数
++id; //接收正确,准备接收下一包数据
/* 发送数据包确认信息 */
if(sendto(client_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printi("Send confirm information failed!");
}
/* 写入文件 */
if(fwrite(data.buf, sizeof(char), data.head.buf_size, fp) < data.head.buf_size)
{
printi("File:\t%s Write Failed\n", file_name);
break;
}
}
else
{
pack_info.id = data.head.id; //错误包,让服务器重发一次
pack_info.buf_size = data.head.buf_size;
pack_info.errorflag = 1;
printi("rec data error,need to send again\n");
/* 重发数据包确认信息 */
if(sendto(client_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printi("Send confirm information failed!");
}
}
}//if(data.head.id == id)
else if(data.head.id < id) /* 如果是重发的包 */
{
pack_info.id = data.head.id;
pack_info.buf_size = data.head.buf_size;
pack_info.errorflag = 0; //错误包标志清零
printi("data.head.id < id\n");
/* 重发数据包确认信息 */
if(sendto(client_socket_fd, (char*)&pack_info, sizeof(pack_info), 0, (struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printi("Send confirm information failed!");
}
}//else if(data.head.id < id) /* 如果是重发的包 */
}
else //接收完毕退出
{
break;
}
}
printi("Receive File:\t%s From Server IP Successful!\n", file_name);
fclose(fp);
close(client_socket_fd);
return 0;
}