发送.txt文本文件就没事!
比如接收1MB左右的文本文件 就没问题,内容都一样
但接收.jpg的图片文件就不行,虽然接收的字节大小没问题,但就是接收后打开显示的是乱码 无法显示图片图象
这是怎么回事呢?
下面是代码
服务端用IOCP模型
在传输文件块时我用到了zlib压缩库
客户端:
FILE *dfile=fopen(readfile,"rb");
int sendcount;
DWORD lastfileblock;
DWORD sendfilebyte=1024;
if (FileLength<sendfilebyte)
{
sendcount=1;
lastfileblock=FileLength;
}
if (FileLength>sendfilebyte)
{
sendcount=FileLength/sendfilebyte;
lastfileblock=FileLength%sendfilebyte;
}
if (lastfileblock!=0 && FileLength>sendfilebyte)
{
sendcount+=1;
}
int i;
for(i=0;i<sendcount;i++)
{
DWORD dwMsg;
if (i==sendcount-1 && lastfileblock!=0)
{
dwMsg=lastfileblock;
}
else
dwMsg=sendfilebyte;
char *file_buf = new char[dwMsg];
fread(file_buf,1,dwMsg,dfile);
压缩
send.....
}
服务端:
解压缩后
到写入文件的函数
WriteFile(hfile,pfile->singlefilepacket,sendfilebyte,&dwbuf,NULL);
fileoffsetbyte+=1024;
SetFilePointer(hfile,fileoffsetbyte,0,FILE_BEGIN);
比如接收1MB左右的文本文件 就没问题,内容都一样
但接收.jpg的图片文件就不行,虽然接收的字节大小没问题,但就是接收后打开显示的是乱码 无法显示图片图象
这是怎么回事呢?
下面是代码
服务端用IOCP模型
在传输文件块时我用到了zlib压缩库
客户端:
FILE *dfile=fopen(readfile,"rb");
int sendcount;
DWORD lastfileblock;
DWORD sendfilebyte=1024;
if (FileLength<sendfilebyte)
{
sendcount=1;
lastfileblock=FileLength;
}
if (FileLength>sendfilebyte)
{
sendcount=FileLength/sendfilebyte;
lastfileblock=FileLength%sendfilebyte;
}
if (lastfileblock!=0 && FileLength>sendfilebyte)
{
sendcount+=1;
}
int i;
for(i=0;i<sendcount;i++)
{
DWORD dwMsg;
if (i==sendcount-1 && lastfileblock!=0)
{
dwMsg=lastfileblock;
}
else
dwMsg=sendfilebyte;
char *file_buf = new char[dwMsg];
fread(file_buf,1,dwMsg,dfile);
压缩
send.....
}
服务端:
解压缩后
到写入文件的函数
WriteFile(hfile,pfile->singlefilepacket,sendfilebyte,&dwbuf,NULL);
fileoffsetbyte+=1024;
SetFilePointer(hfile,fileoffsetbyte,0,FILE_BEGIN);
字符串是以可见字符组成,以0X00结尾的字节串,没仔细看你的代码,你会不会对发送和接收的数据用了类似STRLEN的方式判断长度?最好将CHAR[]或CHAR*改为UCHAR[]或UCHAR*,视觉上先别混乱
造成图片无法显示
服务端 用uncompress()解压缩
难道 不能压缩jpg格式的图片吗
哪位高手 给段代码
看看有没有问题
没问题了再把压缩加上
可能是压缩的问题
TXT文件压缩前后一样,没有区别
JPG压缩后就不同了
fileoffsetbyte+=1024;
SetFilePointer(hfile,fileoffsetbyte,0,FILE_BEGIN);
你默认写入的字节是1024,但是有可能不是1024