T
takdick
Unregistered / Unconfirmed
GUEST, unregistred user!
我想用socket接收文件,服務端已得到要接收文件的大小和儲存路徑,但接收文件內容的代碼不知該如何寫?我知道使用記錄類型會較方便,但想知道存不使用的情況下又該如何處理?謝謝.
客戶端
filesize:longint;
filename,shortname:string;
m,ms1:tmemorystream;
procedure TForm1.uploadClick(Sender: TObject);
begin
if opendialog1.Execute then
begin
filename:=opendialog1.FileName;
shortname:=ExtractFileName(filename);
ms1:=tmemorystream.Create;
ms1.Position:=0;
ms1.LoadFromFile(filename);
filesize:=ms1.Size;
if filesize >8388608 then
begin
application.MessageBox('文件>8M,無法上傳','提示');
ms1.Free;
exit;
end;
ss.Socket.Connections[0].SendText('send');
end;
end;
procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
str:string;
begin
str:=socket.ReceiveText;
if str='size' then
Socket.SendText('size'+inttostr(filesize));
if str='name' then
Socket.SendText('name'+filename+'.bak');
if str='data' then
Socket.SendStream(ms1);
if str='okok' then
begin
ss.Close;
showmessage('file send success.');
end;
end;
服務端
procedure TForm1.cs2Read(Sender: TObject; Socket: TCustomWinSocket);
var
ms:tmemorystream;
len,filesize:longint;
str,filename,filepath,head,tail:string;
buffer:array [0..10000] of byte;
begin
str:=Socket.ReceiveText;
head:=copy(str,1,5);
tail:=copy(str,6,length(str)-5);
if head='send' then
begin
Socket.SendText('size');
end;
if head='size' then
begin
filesize:=strtoint(tail);
Socket.SendText('name');
end;
if head='name' then //已收到要接收文件的大小和儲存路徑
begin
filename:=tail;
Socket.SendText('data');
end;
len:=socket.ReceiveLength; //請問以下接收文件的代碼該如何寫?還要加些什麼才能正常接收文件?
socket.ReceiveBuf(buffer,len);
ms:=tmemorystream.Create;
......
......
......
ms.SaveToFile(filename);
Socket.SendText('okok');
cs.Close;
end;
end;
客戶端
filesize:longint;
filename,shortname:string;
m,ms1:tmemorystream;
procedure TForm1.uploadClick(Sender: TObject);
begin
if opendialog1.Execute then
begin
filename:=opendialog1.FileName;
shortname:=ExtractFileName(filename);
ms1:=tmemorystream.Create;
ms1.Position:=0;
ms1.LoadFromFile(filename);
filesize:=ms1.Size;
if filesize >8388608 then
begin
application.MessageBox('文件>8M,無法上傳','提示');
ms1.Free;
exit;
end;
ss.Socket.Connections[0].SendText('send');
end;
end;
procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
str:string;
begin
str:=socket.ReceiveText;
if str='size' then
Socket.SendText('size'+inttostr(filesize));
if str='name' then
Socket.SendText('name'+filename+'.bak');
if str='data' then
Socket.SendStream(ms1);
if str='okok' then
begin
ss.Close;
showmessage('file send success.');
end;
end;
服務端
procedure TForm1.cs2Read(Sender: TObject; Socket: TCustomWinSocket);
var
ms:tmemorystream;
len,filesize:longint;
str,filename,filepath,head,tail:string;
buffer:array [0..10000] of byte;
begin
str:=Socket.ReceiveText;
head:=copy(str,1,5);
tail:=copy(str,6,length(str)-5);
if head='send' then
begin
Socket.SendText('size');
end;
if head='size' then
begin
filesize:=strtoint(tail);
Socket.SendText('name');
end;
if head='name' then //已收到要接收文件的大小和儲存路徑
begin
filename:=tail;
Socket.SendText('data');
end;
len:=socket.ReceiveLength; //請問以下接收文件的代碼該如何寫?還要加些什麼才能正常接收文件?
socket.ReceiveBuf(buffer,len);
ms:=tmemorystream.Create;
......
......
......
ms.SaveToFile(filename);
Socket.SendText('okok');
cs.Close;
end;
end;