用多线程吧!
这是我写的程序的一部分:
-------------------
unit U_fileserver;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Psock, NMSTRM, DSIcon, NMUDP, Spin,inifiles, Menus;
type
TForm1 = class(TForm)
DSIcon1: TDSIcon;
UDPLIS: TNMUDP;
Edit1: TEdit;
Sport: TSpinEdit;
Slisport: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Button2: TButton;
Open_b: TButton;
Open_f: TOpenDialog;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
procedure UDPLISDataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Open_bClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
procedure setdefault;
public
{ Public declarations }
end;
type
Tmysocket = class(Tthread)
private
{ Private declarations }
MyStrm: TNMStrm;
Myudp: TNMUDP;
Myhost: string;
Myport,Mylisport: integer;
Myfile: string;
Myvdate: Tdatetime;
protected
procedure Execute;override;
public
{ Public declarations }
Constructor Create(vhost,vfile: string;vport,vlisport: integer;Vdate: Tdatetime);
end;
type
TFileTimeType = (fttCreation, fttLastAccess, fttLastWrite);
function GetFileDateTime(const FileName: string; FileTimeType: TFileTimeType): TDateTime;
var
Form1: TForm1;
Verdate: Tdatetime;
implementation
{$R *.DFM}
{ Tmysocket }
constructor Tmysocket.Create(vhost, vfile: string; vport,vlisport: integer;Vdate: Tdatetime);
begin
inherited Create(false);
Myhost := vhost;
Myport := vport;
Myfile := vfile;
Mylisport := vlisport;
Myvdate := vdate;
end;
procedure Tmysocket.Execute;
var
MyFStream: TFileStream;
buff: Array [1..3] of Char;
begin
FreeOnTerminate:=True;
Myudp := TNMUDP.Create(nil);
try
Myudp.RemoteHost := Myhost;
Myudp.RemotePort := Mylisport;
if Myvdate = Verdate then
begin
buff := 'not';
Myudp.SendBuffer(buff,length('not'));
Myudp.Free;
Exit;
end
else
begin
buff := 'yes';
Myudp.SendBuffer(buff,length('yes'));
Myudp.Free;
end;
except
Myudp.Free;
end;
try
MyStrm := TNMStrm.create(nil);
MyStrm.host := Myhost;
MyStrm.port := Myport;
MyStrm.FromName := datetimetostr(Verdate);
MyFStream := TFileStream.Create(Myfile, fmShareDenyNone);
try
if not Terminated then
MyStrm.PostIt(MyFStream);
finally
MyFStream.Free;
end;
finally
MyStrm.free;
end;
end;
------------------------------------------------------------
procedure TForm1.UDPLISDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
datestr: string;
begin
MyStream := TMemoryStream.Create;
try
UDPLIS.ReadStream(MyStream);
SetLength(dateStr,NumberBytes);
MyStream.Read(dateStr[1],NumberBytes);
finally
MyStream.Free;
end;
Verdate := GetFileDateTime(edit1.text,fttLastWrite);
TMysocket.Create(FromIp,edit1.text,Sport.Value,Port,strtodatetime(datestr));
end;