The previous period of time is required to write a small program for a local area network file. Later, I saw a post in the 9CBS. So I finished it, published. I hope to help the friends needed. I am also a beginner. It is also possible that the masters seem to have a prior. Ha ha. Since this program is very simple, I just made a package. The purpose here only wants to publish the code. So don't explain the basic conceptual knowledge. I used four controls in this program. TidTcpserver, TidtcpClient, Tidantifreeze, Tidicmpclien. These four controls have been packaged in the TgetFileClass class. Below is the basic functions: 1. ping function. Second, read the contents of the file. Third, read the entire file. (It is also a copy file). The following TgetFileClass files. {***********************Instructions for use************************ ************************} // Server Description {1. Create an instance of the TgetFileClass object 2. Setting Serverport 3. Call ServerStart Method 4. Set ReadFilePath and ReadFileName 5. Stop calls ServerStop Method}
// Client Description {1. Create an instance of TgetFileClass object 2. Set Host 3. Set the ClientPort 4. Call the ClientConnect method 5. Call the pingforusesbefor method before sending a request to the server to spell the server before calling this method For the pingbuffersize and pingRecEightimeout properties. It is determined according to the pingserver attribute. 6. Set the type of readfilety attribute 7. If you are reading the content, call ClientGetFileInfo 8. If it is a pass file, please set the SaveFilePath property before calling ClientGetfile 10. Disconnect CLIENTDIONNECT method} {**** *********************************************************** *********************************
Unit ugetfileclass;
interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdTCPServer, StrUtils, IdIcmpClient, DateUtils; Type // define read the file Method (file, file content) Treadfiletype = (RTFILE, RTFILEINFO);
TgetfileClass = Class Private FIDTCPSERVER: TIDTCPSERVER; // Server FIDTCPCLIENT: TIDTCPCLIENT; // Client Fidantifreeze: Tidantifreeze; / / Prevent Frozen FidicMpClient: TidicMpClient; // PING server
FPINGBUFFERSIZE: INTEGER; / / PING Packet size fpingRecEightiMeout: integer; // ping
FHOST: STRING; / / Host FclientPort: Integer; // Client port fserverport: integer; // service port fsenddata: string; // Send data fscommand: string; // Command FREADFILENAME: STRING; // To read the file name FREADFILEPATH: STRING; // To read file path fsavefilename: string; // To save the file name (store) fsavefilepath: string; // To save the path to the file
FclientConnect: boolean; // Client status FServerstart: boolean; // server status
FclientResultInfo: string; // Client status information FServerResultinfo: String; // server status information
FFileInfo: TStrings; // file contents FReadFileType: TReadFileType; // read files way public procedure SetHost (const AHost: string); // Set the port procedure SetClientPort (const APort: Integer); // Set client port procedure SetServerPort ( Const APORT: Integer; // Set the service port // to read the procedure setReadFileName (const areadfilename: string); // Set the file name of the file to read Procedure SetReadFilePath (const areadfilepath: string); // Setting To read File path // To save the procedure setsavefilename (const asavefilename: string); // Set the file name Procedure SetSaveFilePath (const asavefilepath: string); // Set the file path you want to save
Procedure SetReadFileType (const areadfilety: TreadFileType); // Set the way to read the file.
Procedure setPingbuffersize (APINGBUFFERSIZE: INTEGER); / / Set the size of the ping packet. Procedure setPingReceTimeout (APINGRECEIVETIMEOUT: Integer); // Setting up PING.
Function getClientResultInfo: string; // Return to client status information Function GetServerResultInfo: String; // Return to server status information
function ClientConnect: Integer; // client connection function ClientDisConnect: Integer; // client disconnects function SendData (const AData: string): Integer; // send data function ClientGetFileInfo: Integer; // request to the server file Function ServerStart : Integer; // server side, start Function ServerStop: Integer; // server stop procedure IdTCPServerConnect (AThread: TIdPeerThread); // server connection procedure IdTCPServerExecute (AThread: TIdPeerThread); // // execution with the server PING control test Whether the server can communicate. Function PingforusesBefor: boolean
// Announced attribute. property Host: string read FHost write SetHost; property ClientPort: Integer read FClientPort write SetClientPort; property ServerPort: Integer read FServerPort write SetServerPort; property ClientState: string read GetClientResultInfo; // current client information property ServerState: string read GetServerResultInfo; // Services end current information property FileInfo: TStrings read FFileInfo; // // need to read the file contents of the file path and file name specified by the user property ReadFilePath:. String read FReadFilePath write SetReadFilePath; property ReadFileName:. String read FReadFileName write SetReadFileName; // save the file path and file name specified by the user property SaveFilePath: String read FSaveFilePath write SetSaveFilePath; property SaveFileName:. String read FSaveFileName write SetSaveFileName; // read the file type of property ReadFileType: TReadFileType read FReadFileType write FReadFileType; // PING Label True True is a packet size of PROELTY PINGERVER: Boolean ReadforusesBefor (// Fighting) Packet size Property PingBuffersize: Integer R EAD FPINGBUFFERSIZE WRITE SETPINGBUFFERSIZE; // Fighting Time Property PingRecEtimeout: Integer Read FPINGRECEIVETIMEOUT WRITE SETPINGRECEIVETIMEOUT
Constructor Create (Owner: Tcomponent); Destructor Destroy; Override; End; Implementation
{Tgetfileclass}
function TGetFileClass.ClientConnect: Integer; begin Result: = 0; if FIdTCPClient.Connected then FIdTCPClient.Disconnect; FIdTCPClient.Host: = Host; FIdTCPClient.Port: = ClientPort; with FIdTCPClient do begin Try Try Connect (50000); FClientResultInfo: = Readln (); fclientConnect: = true; Except FclientResultinfo: = 'Remote hosts are unresponsive! '; FIDTCPCLIENT.DISCONNECT; FCLIENTCONNECT: = false; End; Except fclientResultinfo: =' Unable to establish a connection to ' host '; result: = -1; end; end; end;
function TGetFileClass.ClientDisConnect: Integer; begin Result: = 0; if FIdTCPClient.Connected then begin Try FIdTCPClient.WriteLn ( 'QUIT'); Finally FIdTCPClient.Disconnect; FClientConnect: = False; FClientResultInfo: = 'with host' Host 'of The line is disconnected. End; end;
function TGetFileClass.ClientGetFileInfo: Integer; var aFileSize, FileSize: Integer; Buff: array [0..1023] of Byte; ReadCount: Integer; FileInfoStream: TMemoryStream; FileStream: TFileStream; aFileName: string; begin Result: = 0; if FClientConnect THEN BEGIN / / Send command FIDTCPCLIENT.WRITELN ('getfile'); // Read file name AFILENAME: = FIDTCPCLIENT.READLN (# 13 # 10, 100); if AfileName = '' Then EXIT; if FIDTCPCLIENT.CONNECTED THEN BEGIN // Return to confirm file transfer flag FIDTCPCLIENT.WRITELN;
// Start reading the file length, create files FIDTCPCLIENT.Readbuffer (AfileSize, 4); FileSize: = AFILESize;
// If the read type of the file content if ReadFileType = rtFileInfo then begin FileInfoStream: = TMemoryStream.Create; try // read the file stream repeat if FileSize - FileInfoStream.Size> SizeOf (Buff) then ReadCount: = SizeOf (Buff) else ReadCount: = FileSize - FileInfoStream.Size; FIdTCPClient.ReadBuffer (Buff, ReadCount); FileInfoStream.WriteBuffer (Buff, ReadCount); Application.ProcessMessages; until FileInfoStream.Size> = FileSize;
FileInfostream.position: = 0; // The pointer position of the positioning stream begins to read data from 0. FFileInfo.clear; ffileinfo.loadFromstream (fileInfostream); // store the contents of the file into the FFileinfo variable. finally FileInfoStream.Free; FileInfoStream: = nil; end; end else begin // if the type of the file is read SaveFileName: = aFileName; FileStream: = TFileStream.Create (SaveFilePath SaveFileName, fmCreate); try // read the file stream Repeat if FileSize - FileStream.size> Sizeof (BUFF) THEN READ: = SIZEOF (BUFF) Else Readcount: = FILESIZE - FILESTREAM.SIZE
FIdTCPClient.ReadBuffer (Buff, ReadCount); FileStream.WriteBuffer (Buff, ReadCount); Application.ProcessMessages; until FileStream.Size> = FileSize; finally FileStream.Free; FileStream: = nil; end; end; end; end; end;
constructor TGetFileClass.Create (Owner: TComponent); begin // create a building structure function class server and client components FIdTCPServer: = TIdTCPServer.Create (OWner); FIdTCPServer.OnConnect:. = IdTCPServerConnect; FIdTCPServer.OnExecute: = IDTCPSERVEREXECUTE;
FIDTCPCLIENT: = TIDTCPCLIENT.CREATE (OWNER); FIDANTIFREEZE: = Tidantifreeze.create (Owner); FidicMpClient: = TidicMpClient.create (Owner);
// Create a Strings variable that stores the file content. You can read the contents of the file by attribute FileInfo. FFileInfo: = TStringList.create;
DESTRUCTOR TGETFILASS.DESTROY; begin // Release the generated object in the parser function. FIDTCPSERVER.FREE; FIDTCPCLIENT.FREE; Fidantifreeze.Free; fidicmpclient.free; inherited;
Function TgetFileClass.getClientResultInfo: string; begin // Returns the current realistic state of the client. You can read it via the clientState property. RESULT: = fclientResultinfo;
Function TgetFileClass.getServerResultInfo: string; begin // Returns the current realistic status of the server side. You can read it through the ServerState property. RESULT: = fserverResultinfo;
procedure TGetFileClass.IdTCPServerConnect (AThread: TIdPeerThread); begin FServerStart: = True; AThread.Connection.WriteLn ( 'Welcome to connect to the server!'); FServerResultInfo: = 'from host' AThread.Connection.Socket.Binding.PeerIP 'of The request has been accepted. END;
procedure TGetFileClass.IdTCPServerExecute (AThread: TIdPeerThread); var Buf: array [0..1023] of Byte; ReadCount: Integer; aStream: TFileStream; aSize: Integer; begin with AThread.Connection do begin FsCommand: = ReadLn (); if Sametext (fscommand, 'quit') Then Begin fserverResultinfo: = 'disconnects the connection with the host' attcture.connection.socket.binding.peerip '! '; DisConnect; FServerStart: = False; end else if AnsiStartsText (' Data ', FsCommand) then begin FServerResultInfo: = ReadLn (); WriteLn (' 200:! Data reception success '); end else if SameText (FsCommand,' GetFile ') then begin aStream: = TFileStream.Create (ReadFilePath ReadFilename, fmOpenRead or fmShareDenyWrite); try // send the file name AThread.Connection.WriteLn (ReadFilename); // wait for an acknowledgment received AThread.Connection.ReadLn (# 13 # 10 , 1000; // Write file length and file stream asize: = astream.size; athread.connection.writebuffler (asize, 4); while school, 4); while astream.position
function TGetFileClass.SendData (const AData: string): Integer; begin Result: = 0; with FIdTCPClient do begin Try WriteLn ( 'Data' AData); FClientResultInfo: = ReadLn (); Except DisConnect; FClientConnect: = False; FClientResultInfo: = 'Disconnected with the host' host '. '; Result: = -1; End; end; end; function TGetFileClass.ServerStart: Integer; begin Result: = 0; Try FIdTCPServer.DefaultPort: = ServerPort; FIdTCPServer.Active: = True; FServerStart: = True; FServerResultInfo: = 'The server has been successfully started! '; EXCEPT RESULT: = -1; fserverstart: = false; end;
Function tgetfileclass.serverstop: integer; begin result: = 0; try fidtcpserver.active: = false; fserverStart: = false; fserverResultinfo: = 'server is closed! '; EXCEPT RESULT: = -1; end;
Procedure tgetfileclass.setClientPort (const APORT: Integer); Begin if Aport <> fclientport the fclientport: = APORT;
Procedure tgetfileclass.setReadFileName: String; begin if aadfilename <> freadFilename Then FreadFileName: = areadfilename;
Procedure tgetfileclass.setReadFilePath: string; begin if areadfilepath <> freadFilepath ThenfreadFilePath: = aadfilepath;
Procedure tgetfileclass.sethost (const ahost: string); begin if ahost <> fHOST THEN FHOST: = ahost;
Procedure tgetfileclass.setsavefilename: string; begin if asavefilename <> fsavefilename damavefilename: = asavefilename;
procedure TGetFileClass.SetSaveFilePath (const ASaveFilePath: string); begin if ASaveFilePath <> FSaveFilePath then FSaveFilePath: = ASaveFilePath; end; procedure TGetFileClass.SetServerPort (const APort: Integer); begin if APort <> FServerPort then FServerPort: = APort; end;
Procedure tgetfileclass.setReadFileType: Treadfiletype); Begin if aadfiletype <> freadfiletype then freadfilety: = areadfilety;
Function TGetFileClass.PingForUsesBefor: Boolean; var StartDate, EndDate, UseDate: Ttime; i: integer; temp, hm: word; begin if PingBufferSize = 0 then PingBufferSize: = 10000; if PingReceiveTimeout = 0 then PingreceiveTimeout: = 5000; FIdIcmpClient.BufferSize : = Pingbuffersize; fidicmpclient.receivetimeout: = pingRecEthImeout; fidicmpclient.host: = Host;
Startdate: = now;
For i: = 0 to 1 do beg; fidicmpclient.ping;
= Now; usedate: = startdate - enddate;
// Remove the number of seconds decodedatetime (Usedate, Temp, Temp, Temp, Temp, Temp, HM, TEMP); if HM> = 5 THEN Result: = false else result: = true;
Procedure tgetfileclass.SetPingBuffersize (APINGBUFFERSIZE: Integer); begin if APINGBUFFERSIZE <> FPINGBUFFERSIZE THEN FPINGBUFFERSIZE: = APINGBUFFERSIZE; END;
Procedure tgetfileclass.SetPingReceiveTimeout (APINGRECEIVETIMEOUT: Integer); begin if ApingRecEtimeout <> fpingReceiveTimeout dam FPINGRECEIVETIMEOUT: = APINGRECEIVETIMEOUT; END;
END. Server-side calling method: *********** Var getFileClass: TgetFileClass; launch: getfileclass.serverport: = 6060; // Please change your own. GetFileClass.ServerStart; // Start the server BtnStart.Enabled: = false; BtnStop.Enabled: = True; Btnclose.Enabled: = false; LbLog.Items.Add (GetFileClass.ServerState); // LbLog is a TListBox ~ ServerState service Current status information. GetFileClass.ReadFilePath: = extractFilePath (Application.exename); // Set the path to read file getFileClass.ReadFileName: = '1.txt'; // Set the name to read the file ********* ******************* Stop: getfileclass.serverstop; btnStop.enabled: = false; btnStart.enabled: = true; btnclose.enabled: = true; lblog.Items .Add (getfileclass.serverstate; *************************************************** *************************************** Clients Call method: connection method: getfileclass.host: = edthost.text; getfileclass.clientport: = 6060; // This port is going to be like the server.
GetFileClass.ClientConnect; // server connected LbLog.Items.Add (GetFileClass.ClientState); BtnConnect.Enabled: = False; BtnDisConnect.Enabled: = True; BtnExit.Enabled: = False; send a message to the server: if GetFileClass.SendData ( 'aaa') = -1 The begin Application.MessageBox ('Send Failure!', 'Error', MB_OK); End; lblog.items.add (getfileclass.clientState); ping server: function pingserver: boolean; begin getfileclass. Pingbuffersize: = 10000; getFileClass.PingRecETITIMEOUT: = 5000; Result: = getfileclass.pingforusesbefor; // ping server .end; Read the contents of the specified file on the server or the entire file: (mainly based on incoming parameters ) procedure GetFile (AReadFileType: TReadFileType); begin if (not PingServer) then exit; // read type provided if AReadFileType = rtFileInfo then begin GetFileClass.ReadFileType: = AReadFileType; // call ClientGetFileInfo method GetFileclass class, send instructions to the server . If getfileclass.clientgetfileinfo = -1 THEN EXIT; memo1.clear; // The file information obtained can be read by the FileInfo attribute of the GetFileClass class. Memo1.Lines.Addstrings (getfileclass.fileinfo); // Clear. GetfileClass.Filei nfo.Clear; end else begin GetFileClass.ReadFileType: = AReadFileType; GetFileClass.SaveFilePath: = 'F: /'; // ExtractFilePath (Application.ExeName); if GetFileClass.ClientGetFileInfo = -1 then exit; end; end; OFF Server: getfileclass.clientdisconnect; lblog.items.add (getfileclass.clientState); self.btnconnect.enabled: = true; self.btndisconnect.enabled: = false; btnexit.enabled: = true; ******** *********************************************************** ******************************** The Delphi7 Windows2000 Server runs successfully. hope that it can help us. This program is a bit early. Please don't laugh.