我已經寫出來了﹐是線程Socket的﹐給你哦﹗
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
private
{ Private declarations }
public
{ Public declarations }
end;
TClientStread=class(TThread)
protected
procedure GetFileFromStream;
procedure Execute;override;
end;
TServerStread=class(TServerClientThread)
public
procedure ClientExecute;override;
end;
var
Form1: TForm1;
FF: String;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Active:=True;
ClientSocket1.Active:=False;
end;
{ TClientStread }
procedure TClientStread.Execute;
begin
inherited;
FreeOnTerminate:= True;
Synchronize(GetFileFromStream);
end;
procedure TClientStread.GetFileFromStream;
var
Data: Array[0..1023] Of char;
S: TWinSocketStream;
F: TFileStream;
FN,A: String;
Buf: Pointer;
t: Integer;
begin
with Form1.OpenDialog1 do
begin
Execute;
if FileName<>'' then
begin
FN:=FileName;
FF:=ExtractFileName(FN);
Form1.ClientSocket1.Host:='172.17.8.205';
Form1.ClientSocket1.Active:=True;
S:=TWinSocketStream.Create(Form1.ClientSocket1.Socket,60000);
F:=TFileStream.Create(FN,fmShareDenyWrite);
try
{ if Form1.ClientSocket1.Socket.Data<>nil then
begin
A:= Form1.ClientSocket1.Socket.ReceiveText;
Showmessage(A);
end; }
T:=F.Size;
GetMem(Buf,T);
F.ReadBuffer(Buf^,T);
S.WriteBuffer(Buf^,F.Size);
// S.CopyFrom(F,0);
finally
S.Free;
F.Free;
Form1.ClientSocket1.Active:=False;
FreeMem(Buf,T);
end; //End Try
end; //End if FileName<>'' then
end; //End with Form1.OpenDialog1 do
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TClientStread.Create(False);
end;
{ TServerStread }
procedure TServerStread.ClientExecute;
var
RBuf: Array [0..4095] of char;
S: TWinSocketStream;
F: TFileStream;
BytesRead:Integer;
begin
//inherited;
While (not Terminated) and (ClientSocket.Connected) do
begin
try
FreeOnTerminate:=True;
// ClientSocket.SendText('I got');
S:= TWinSocketStream.Create(ClientSocket,60000);
F:= TFileStream.Create('C:/'+FF,fmCreate);
try
FillChar(RBuf,SizeOf(RBuf),0);
if S.WaitForData(5000) then
Repeat
BytesRead:=S.Read(RBuf,SizeOf(RBuf));
if BytesRead=0 then
ClientSocket.Close
else F.WriteBuffer(RBuf,BytesRead);
Until Not S.WaitForData(2000);
finally
F.Free;
S.Free;
ClientSocket.Close;
end; //End try finally
except
HandleException;
end; //End try except
end;
end;
procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread:= TServerStread.Create(False,ClientSocket);
end;
end.