遇到TCP传输大文件问题,主要是
(1)、传输快,但拼接成的大文件数据错
(2)、加校验,传输变慢
(3)、接收包数跟发送包数不对应
费了一段时间才解决。
今天,整理一下,留以后备用。
首先,TCP是有连接、自带校验的传输协议,不需要再另外加代码(如接收端回复码给发送端)确保其正确性,这样可以保证TCP的速度,基本能达到4M/s以上。
如接收包数和发送包数不一致,这只是说两边发送、接收的速度不对应,发送慢、接受快了,就会出现接收的次数多(不信你可以找找,你接收的数据个数中肯定有小于BUFFER_SIZE的)。所以在拼接成大文件时,就不能按照预设的size进行,应该按照实际接收到的数据size进行偏移。
TCP传输时,发送端和接收端有缓冲区,send()和recv()函数其实只是实现copy功能,把缓冲区的内容拷出来。所以会出现缓冲区爆掉、接收接不全数据的情况。
附上自己调试时写的小代码,仅供参考
客户端实现的功能是,把一段1344000的数据(bmp图片的数据大小去掉54字节的header)发送给服务器端。文件流获取图片数据去图片头,模拟拍照等获取到的纯图片数据。实际传输的数据为1344000的数据。
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 6666
#define BUFFER_SIZE 1024
struct image_data{
int value;
unsigned char * imageData;
int length;
struct image_data * next;
};
int main(int argc,char **argv)
{
if(argc!=2)
{
printf("参数错误,清输入两个参数\n");
exit(1);
}
FILE *stream;
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET; //internet协议族
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(SERVER_PORT);
int sfd;
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
printf("socket error\n");
exit(0);
}
int file_length;
int tcpnum;
int file_send_len;
unsigned char snddata[1344000];
unsigned char buffer[BUFFER_SIZE];
file_length = sizeof(snddata);
FILE *fp;
if (!(fp = fopen("1.bmp", "rb")))
{
return -1;
}
fseek(fp, SEEK_SET, 54);
fread(snddata, sizeof(unsigned char), file_length, fp);
fclose(fp);
struct image_data * temp_data = NULL;
temp_data = (struct image_data *)malloc(sizeof(struct image_data));
temp_data->imageData = (unsigned char *)malloc(sizeof(unsigned char) * file_length);
temp_data ->imageData = snddata;
temp_data ->length = file_length;
if(connect(sfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0)
{
printf("Can Not Connect To %s\n",argv[1]);
exit(1);
}
bzero(buffer,BUFFER_SIZE);
printf("正在传输...\n");
int len=0;
//不断读取并发送数据
tcpnum = file_length/BUFFER_SIZE;
if((tcpnum * BUFFER_SIZE) < file_length)
{
tcpnum++;
}
printf("tcpnum = %d\n", tcpnum);
int i;
for(i = 0 ; i < tcpnum; i++)
{
if((file_length - i * BUFFER_SIZE) >= BUFFER_SIZE)
{
file_send_len = BUFFER_SIZE;
}
else
{
file_send_len = file_length - i * BUFFER_SIZE;
}
bzero(buffer,BUFFER_SIZE);
memcpy(buffer, temp_data->imageData + (i * BUFFER_SIZE), file_send_len);
len = send(sfd, buffer, file_send_len, 0);
if(len < 0)
{
printf("send file error\n");
break;
}
}
close(sfd);
printf("tcp over\n");
return 0;
}
server.c
服务器端把数据存进一个数据,接收完之后,生成bmp图片。还可以直接存进文件(被注释了),但必须在客户端把图片header加上,一起发给服务器端。
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 6666
#define LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
typedef struct tagRGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
} RGBQUAD;
static int youwritetobmp1(RGBQUAD*pixarr, int xsize, int ysize, int num) ;
int num = 0;
int main(int argc,char **argv)
{
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));//全部置零
//设置地址相关的属性
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htons(INADDR_ANY);
server_addr.sin_port=htons(SERVER_PORT);
//创建套接字
int server_socket=socket(AF_INET,SOCK_STREAM,0);
if(server_socket<0)
{
printf("socket create error\n");
exit(1);
}
//绑定端口
if(bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))
{
printf("bind error\n");
exit(1);
}
//服务器端监听
if(listen(server_socket,LISTEN_QUEUE))
{
printf("Server listen error\n");
exit(1);
}
//服务器端一直运行
while(1)
{
pid_t pid;
struct sockaddr_in client_addr;
socklen_t length=sizeof(client_addr);
//accept返回一个新的套接字与客户端进行通信
int new_server_socket=accept(server_socket,(struct sockaddr*)&client_addr,&length);
//1*begin******************************************************************************
if(new_server_socket==-1)
{
printf("accept error\n");
continue;
}
else
{
printf("客户端%s连接成功\n",inet_ntoa(client_addr.sin_addr));
pid=fork();
//3*begin**运行子进程*************************************************************
if(pid==0)
{
int data_len,flag=0;
char buffer[BUFFER_SIZE];
// 定义文件流
FILE *stream;
bzero(buffer,BUFFER_SIZE);
strcpy(buffer,"Please enter the total path");
strcat(buffer,"\n");
send(new_server_socket,buffer,BUFFER_SIZE,0);
bzero(buffer,BUFFER_SIZE);
//2*begin**服务器接受数据*********************************************
// if((stream=fopen("data","w"))==NULL)
// {
// printf("file open error\n");
// exit(1);
// }
// else
// {
// bzero(buffer,BUFFER_SIZE);
// }
unsigned char recv_data[1344000];
int offset = 0;
bzero(recv_data, 1344000);
// printf("正在接收来自%s的文件....\n",inet_ntoa(client_addr.sin_addr));
//先将数据接受到缓冲区buffer中,再写入到新建的文件中
while((data_len=recv(new_server_socket,buffer,BUFFER_SIZE,0)) > 0)
{
flag++;
printf("flag is %d\n",flag);
printf("data_len is %d\n",data_len);
if(flag==1)
{
printf("正在接收来自%s的文件....\n",inet_ntoa(client_addr.sin_addr));
}
if(data_len<0)
{
printf("接收错误\n");
exit(1);
}
//向文件中写入数据
//int write_len=fwrite(buffer,sizeof(char),data_len,stream);
//if(write_len>data_len)
// {
// printf("file write failed\n");
// exit(1);
// }
printf("offset is %d\n",offset);
memcpy(recv_data + offset, buffer, data_len);
offset += data_len;
bzero(buffer,BUFFER_SIZE);
}
if(flag>0)
printf("%s的文件传送完毕\n",inet_ntoa(client_addr.sin_addr));
if(flag==0)
printf("%s的文件传输失败\n",inet_ntoa(client_addr.sin_addr));
//2*end**服务器接受数据****************************************************
// rename("data",inet_ntoa(client_addr.sin_addr));
//fclose(stream);
//rename("data",inet_ntoa(client_addr.sin_addr));
printf("to bmp file\n");
youwritetobmp1((RGBQUAD *) recv_data, 1280, 350, num);
num++;
exit(1);
}
//3*end**运行子进程**********************************************************
else
{
close(new_server_socket);
}
}
//1*end**************************************************************************************
close(new_server_socket);
}
return 0;
}
/**描述:生成bmp图片
**参数:1、图片数据 2、宽 3、长 4、保存图片区分字符
*返回:0 成功 -1 不成功
**/
static int youwritetobmp1(RGBQUAD*pixarr, int xsize, int ysize, int num)
{
unsigned char header[54] =
{
0x42, 0x4d, 0, 0, 0, 0, 0,
0, 0, 0,54, 0, 0, 0, 40, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 24, 0,0, 0, 0, 0, 0, 0,
0, 0, 0, 0,0, 0, 0, 0, 0, 0,
0, 0, 0, 0,0, 0, 0, 0
};
RGBQUAD * pixarr1;
int i;
int j;
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width;
if(!(xsize%4)) width=xsize;
else width= xsize+(4-xsize%4); //Èç²»ÊÇ4µÄ±¶Êý£¬Ôòת»»³É4µÄ±¶Êý
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "capture_%d.bmp",num);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
RGBQUAD zero={0,0,0}; //²»×ã×֜ڣ¬ÓÃzeroÌî³ä
pixarr1 = pixarr + xsize * (ysize -1);
for(j=0;j
if(!(xsize%4))
{
for(i=0;i
fwrite(pixarr1+i, sizeof(RGBQUAD),1, fp);
}
pixarr1 -=xsize;
}
else
{
for(i=0;i
fwrite(pixarr+i, sizeof(RGBQUAD),1, fp);
}
for(i=xsize;i
fwrite(&zero, sizeof(RGBQUAD),1, fp);
}
}
}
fclose(fp);
return 0;
}