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;
所以我用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;