Socket传输图片的成功率问题(200分)

  • 主题发起人 主题发起人 chnthia
  • 开始时间 开始时间
C

chnthia

Unregistered / Unconfirmed
GUEST, unregistred user!
我的项目中需要进行图片的保存,由于图片规格较大,应客户的要求,以文件方式保存
所以我用Socket来解决,在服务端用TServerSocket,客户端用TClientSocket
现在的问题是:经常在传输时接收流长度大于需接收的字节数,图片传输失败
按我的测试结果,只有25%左右的成功率
实在纳闷,把代码贴出来,请大家帮忙看看,问题出在那了?
或者有没有更好的办法来解决网络图片传输问题

附:各主要事件代码
服务端的ClientRead事件如下
procedure TForm1.ssSaveClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
MyBuffer: array[0..10000] of byte;
MyReceviceLength: integer;
S, S1: string;
int1: integer;
MyBmp: TBitmap;
MyJpg: TJpegimage;
begin
if MySaveSize = 0 then
begin
try
S := Socket.ReceiveText;
int1:= Pos('path', s);
MySaveSize := Strtoint(copy(S, 1, int1-1));
SaveName:= copy(s, int1+5, length(s) - int1 - 4);
Socket.SendText('ready');
except
//
end;
end
else
begin
MyReceviceLength := socket.ReceiveLength;
Socket.ReceiveBuf(MyBuffer, MyReceviceLength);
MySaveStream.Write(MyBuffer, MyReceviceLength);
if MySaveStream.Size = MySaveSize then {
begin
MySaveStream.Position := 0;
MyBmp := tbitmap.Create;
MyJpg := tjpegimage.Create;
try
MyJpg.LoadFromStream(MySaveStream);
MyBmp.Assign(MyJpg); {将JPG转为BMP}
Image1.Picture.Bitmap.Assign(MyBmp); {分配给image1元件 }
MyBmp.SaveToFile(SaveName);
Socket.SendText('ok');
finally
MySaveSize := 0;
MyBmp.free;
MyJpg.free;
MySaveStream.Clear;
end;
end
else
if MySaveStream.Size > MySaveSize then
begin
MySaveSize := 0;
MySaveStream.Clear;
Socket.SendText('error');
Image1.Picture.Metafile.Clear;
end;
end;
end;

客户端的Send按钮事件
procedure TForm1.SendPic;
var
S, S1: string;
MyBmp: TBitmap;
Myjpg: TJpegimage;
begin
Myjpg := TJpegimage.Create;
Myjpg1 := TJpegimage.Create;
MyBmp := TBitmap.Create;
MyBmp1:= TBitmap.Create;
try
MySaveStream := TMemorystream.Create;
try
MyBmp.Assign(Image1.Picture);
Myjpg.Assign(MyBmp);
except
Myjpg.Assign(Image1.Picture);
end;
Myjpg.CompressionQuality := 100;
Myjpg.SaveToStream(MySaveStream);
MySaveStream.Position := 0;
s1 := inttostr(MySaveStream.size)+'path:'+Edit2.Text;{流的大小及保存路径}
csSave.Socket.sendtext(s1); {发送流大小}
finally
Myjpg.free;
Myjpg1.free;
MyBmp.free;
MyBmp1.Free;
end;
end;
客户端ClientSocket的OnRead事件
procedure TForm1.csSaveRead(Sender: TObject; Socket: TCustomWinSocket);
var
s: string;
begin
S := Socket.ReceiveText;
if s = 'ready' then
begin
MySaveStream.Position := 0;
Socket.SendStream(MySaveStream);
end
else
if s = 'ok' then
begin
StatusBar1.SimpleText :='上传成功!';
end
else
if s = 'error' then
begin
if (SendCount<5) then
begin
inc(SendCount);
Sleep(10);
SendPic;
end
else
StatusBar1.SimpleText :='上传失败!';
end;
end;

 
你的代码有问题,如果控制的好,传输成功率应该是99%,除开的是TCP的校验不能察觉的1%出错误...
 
我给你的例子可以100%的接收成功,你采用IE,netscape,clientSocket都可以接收,主要是你接收的问题!
 
呵呵,不可能100%,TCP的校验也是有可能检查不到的出的一些小错误,
应该是99.99999% 才对,呵呵
 
我也是这么想的,一定是程序出了问题

To: dcsdcs
具体一点,能给我发个例子吗?
你也看到了,为了解决发送不成功的问题,我加了五次循环
但还是有不成功的时候
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1968309
示例请告诉mail
 
TO:dcsdcs
我的Email:mystart@netease.com
先谢了。给分了。
 

Similar threads

I
回复
0
查看
560
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部