K
kelongchina
Unregistered / Unconfirmed
GUEST, unregistred user!
[求助]远程传输文件内存流传输出错?已附上代码具体代码http://www.meidiedz.cn/memorystream.rar(只有20行代码)300分相送 ( 积分: 100 )<br />我用的是TServersocket1和TClientSocket,客户端和服务端都可以上传和下载文件,我是用内存流来操作的,但是每次下载完一个,再下载第二个的时候,就会报错,大家帮忙看下是什么问题,谢谢
isdown和m都是全局变量
-------------------------------------------------------控制端:
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
str,s:string;
MyBuffer: array[0..10000] of byte; {设置接收缓冲区}
len:integer;
begin
if isdown=false then //被控制端请求下载文件
begin
str:=socket.ReceiveText;
s:=Copy(str,1,5);
if s='11111' then
begin
m.LoadFromFile(copy(str,6,length(str)));//读文件
m.Position:=0;
len:=m.Size;
self.ServerSocket1.Socket.Connections[0].SendText(inttostr(len)) ;
end;
if s='22222' then
begin
m.Position:=0;
self.ServerSocket1.Socket.Connections[0].SendStream(m);
end;
end
else
begin //控制端接受被控制端发过来的文件
if msize = 0 then {MySize为服务端发送的字节数,如果为0表示为尚未开始接收}
begin
S := Socket.ReceiveText;
msize := Strtoint(S); {设置需接收的字节数}
self.ProgressBar1.Max:= msize;
ServerSocket1.Socket.Connections[0].SendText('22222'); {发指令通知服务端开始发送图象}
end
else
begin
len := socket.ReceiveLength; {读出包长度}
ProgressBar1.Position:=ProgressBar1.Position+len;
Socket.ReceiveBuf(MyBuffer, len); {接收数据包并读入缓冲区内}
m.Write(MyBuffer, len); {将数据写入流中}
if m.Size >= msize then {如果流长度大于需接收的字节数,则接收完毕}
begin
try
m.SaveToFile('d:/2.mp3');
finally {以下为清除工作 }
msize:=0;
m.Clear; //清空内存流
ProgressBar1.Position:=ProgressBar1.Max;
showmessage('接受完毕');
end;
end;
end;
end;
end;
----------------------------------------------被控制端,因为和控制端的功能一样,所以代码也是一样的
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
s,str:string;
MyBuffer: array[0..10000] of byte; {设置接收缓冲区}
len:integer;
begin
//下载
if isdown=false then
begin
str:=socket.ReceiveText;
s:=Copy(str,1,5);
if s='11111' then
begin
m.LoadFromFile(copy(str,6,length(str)));
m.Position:=0;
len:=m.Size;
socket.SendText(inttostr(len)) ;
end;
if s='22222' then
begin
m.Position:=0;
Socket.SendStream(m);
end;
end
else
begin
if msize = 0 then {MySize为服务端发送的字节数,如果为0表示为尚未开始接收}
begin
S := Socket.ReceiveText;
msize := Strtoint(S); {设置需接收的字节数}
self.ProgressBar1.Max:=msize;
Socket.SendText('22222'); {发指令通知服务端开始发送图象}
end
else
begin
len := socket.ReceiveLength; {读出包长度}
ProgressBar1.Position:=ProgressBar1.Position+len;
Socket.ReceiveBuf(MyBuffer, len); {接收数据包并读入缓冲区内}
m.Write(MyBuffer, len); {将数据写入流中}
if m.Size >= msize then {如果流长度大于需接收的字节数,则接收完毕}
begin
try
m.SaveToFile('d:/2.mp3');
finally {以下为清除工作 }
msize:=0;
m.Clear;
ProgressBar1.Position:=self.ProgressBar1.Max;
showmessage('接受完毕');
end;
end;
end;
end;
end;
上面的问题是,不管是哪一端下载文件,老是下第二个文件的时候就报错,应该是内存错误,我改过好多处地方,问题依然,请高手们帮忙解决下,谢谢
isdown和m都是全局变量
-------------------------------------------------------控制端:
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
str,s:string;
MyBuffer: array[0..10000] of byte; {设置接收缓冲区}
len:integer;
begin
if isdown=false then //被控制端请求下载文件
begin
str:=socket.ReceiveText;
s:=Copy(str,1,5);
if s='11111' then
begin
m.LoadFromFile(copy(str,6,length(str)));//读文件
m.Position:=0;
len:=m.Size;
self.ServerSocket1.Socket.Connections[0].SendText(inttostr(len)) ;
end;
if s='22222' then
begin
m.Position:=0;
self.ServerSocket1.Socket.Connections[0].SendStream(m);
end;
end
else
begin //控制端接受被控制端发过来的文件
if msize = 0 then {MySize为服务端发送的字节数,如果为0表示为尚未开始接收}
begin
S := Socket.ReceiveText;
msize := Strtoint(S); {设置需接收的字节数}
self.ProgressBar1.Max:= msize;
ServerSocket1.Socket.Connections[0].SendText('22222'); {发指令通知服务端开始发送图象}
end
else
begin
len := socket.ReceiveLength; {读出包长度}
ProgressBar1.Position:=ProgressBar1.Position+len;
Socket.ReceiveBuf(MyBuffer, len); {接收数据包并读入缓冲区内}
m.Write(MyBuffer, len); {将数据写入流中}
if m.Size >= msize then {如果流长度大于需接收的字节数,则接收完毕}
begin
try
m.SaveToFile('d:/2.mp3');
finally {以下为清除工作 }
msize:=0;
m.Clear; //清空内存流
ProgressBar1.Position:=ProgressBar1.Max;
showmessage('接受完毕');
end;
end;
end;
end;
end;
----------------------------------------------被控制端,因为和控制端的功能一样,所以代码也是一样的
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
s,str:string;
MyBuffer: array[0..10000] of byte; {设置接收缓冲区}
len:integer;
begin
//下载
if isdown=false then
begin
str:=socket.ReceiveText;
s:=Copy(str,1,5);
if s='11111' then
begin
m.LoadFromFile(copy(str,6,length(str)));
m.Position:=0;
len:=m.Size;
socket.SendText(inttostr(len)) ;
end;
if s='22222' then
begin
m.Position:=0;
Socket.SendStream(m);
end;
end
else
begin
if msize = 0 then {MySize为服务端发送的字节数,如果为0表示为尚未开始接收}
begin
S := Socket.ReceiveText;
msize := Strtoint(S); {设置需接收的字节数}
self.ProgressBar1.Max:=msize;
Socket.SendText('22222'); {发指令通知服务端开始发送图象}
end
else
begin
len := socket.ReceiveLength; {读出包长度}
ProgressBar1.Position:=ProgressBar1.Position+len;
Socket.ReceiveBuf(MyBuffer, len); {接收数据包并读入缓冲区内}
m.Write(MyBuffer, len); {将数据写入流中}
if m.Size >= msize then {如果流长度大于需接收的字节数,则接收完毕}
begin
try
m.SaveToFile('d:/2.mp3');
finally {以下为清除工作 }
msize:=0;
m.Clear;
ProgressBar1.Position:=self.ProgressBar1.Max;
showmessage('接受完毕');
end;
end;
end;
end;
end;
上面的问题是,不管是哪一端下载文件,老是下第二个文件的时候就报错,应该是内存错误,我改过好多处地方,问题依然,请高手们帮忙解决下,谢谢