T
takdick
Unregistered / Unconfirmed
GUEST, unregistred user!
我使用socket傳送文件,但不知何故,接收的文件只有4k,請幫我看看以下的代碼有何問題.
接收端:
fs3:=TFileStream.Create('c:/test2.exe',fmcreate);
fs3.Position:=0;
procedure TForm1.cs3Connect(Sender: TObject; Socket: TCustomWinSocket);
begin
cs3.socket.SendText('go~');
end;
procedure TForm1.cs3Read(Sender: TObject; Socket: TCustomWinSocket);
var
stt3 : string;
buf3 : pointer;
lh3:integer;
begin
lh3:=Socket.ReceiveLength;
GetMem(buf3,lh3);
Socket.ReceiveBuf(buf3^,lh3);
stt3:=StrPas(PChar(buf3));
stt3:=Copy(stt3,1,lh3);
fs3.WriteBuffer(buf3^,lh3);
cs3.Socket.SendText('go~');
FreeMem(buf3,lh3);
end;
發送端:
fs:=tfilestream.Create('c:/test.exe',fmopenread);
fs.Position:=0;
procedure TForm1.ss2ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
buf2ointer;
stt2:string;
begin
stt2:=socket.ReceiveText;
if stt2='go~' then
begin
if fs.Position< fs.Size then
begin
count:=fs.Size-fs.Position;
if count>4096 then count:=4096;
GetMem(buf2,count);
fs.Read(buf2^,count);
ss2.Socket.Connections[0].SendBuf(buf2^,count);
FreeMem(buf2,count);
end
else
begin
ss2.Socket.Connections[0].SendText('ok~');
fs.Free;
socket.Close;
end;
end;
end;
接收端:
fs3:=TFileStream.Create('c:/test2.exe',fmcreate);
fs3.Position:=0;
procedure TForm1.cs3Connect(Sender: TObject; Socket: TCustomWinSocket);
begin
cs3.socket.SendText('go~');
end;
procedure TForm1.cs3Read(Sender: TObject; Socket: TCustomWinSocket);
var
stt3 : string;
buf3 : pointer;
lh3:integer;
begin
lh3:=Socket.ReceiveLength;
GetMem(buf3,lh3);
Socket.ReceiveBuf(buf3^,lh3);
stt3:=StrPas(PChar(buf3));
stt3:=Copy(stt3,1,lh3);
fs3.WriteBuffer(buf3^,lh3);
cs3.Socket.SendText('go~');
FreeMem(buf3,lh3);
end;
發送端:
fs:=tfilestream.Create('c:/test.exe',fmopenread);
fs.Position:=0;
procedure TForm1.ss2ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
buf2ointer;
stt2:string;
begin
stt2:=socket.ReceiveText;
if stt2='go~' then
begin
if fs.Position< fs.Size then
begin
count:=fs.Size-fs.Position;
if count>4096 then count:=4096;
GetMem(buf2,count);
fs.Read(buf2^,count);
ss2.Socket.Connections[0].SendBuf(buf2^,count);
FreeMem(buf2,count);
end
else
begin
ss2.Socket.Connections[0].SendText('ok~');
fs.Free;
socket.Close;
end;
end;
end;