Develop Socket applications in Delphi 7 (below)

xiaoxiao2021-03-13  181

Customer code

The client disc is shown in the code list 1.

Code list 1

Procedure TFormMain.btnConnectClick (Sender: TOBJECT);

Begin

IDTCPCLIENT.HOST: = Edthost.Text;

IDTCPCLIENT.PORT: = STRTOINT (EDTPORT.TEXT);

Lblog.Items.Add ('is connecting' edthost.text '...');

With idtcpclient do

Begin

Try

Connect (5000);

Try

Lblog.Items.Add (Readln ());

BtnConnect.enabled: = false;

Btnsend.enabled: = true;

BTNDISCONNECT.ENABED: = TRUE;

Except

Lblog.Items.Add ('Remote Host No Response!');

IDTCPCLIENT.DISCONNECT ();

End; // end Try

Except

Lblog.Items.Add ('Unable to establish a connection to' edthost.text ');

End; // end Try

End; // end with

END;

Procedure TFormMain.btnsendClick (Sender: TOBJECT);

Begin

LBLOG.Items.Add ('data' edtdata.text);

With idtcpclient do

Begin

Try

Writeln ('Data' Edtdata.text);

Lblog.Items.Add (Readln ())

Except

Lblog.Items.Add ('Send Data Failed!');

IDTCPCLIENT.DISCONNECT ();

Lblog.Items.Add ('The connection to the host' edthost.text 'is disconnected!'); ');

BtnConnect.enabled: = true;

Btnsend.enabled: = false;

BTNDISCONNECT.ENABLED: = false;

End; // end Try

End; // end with

END;

Procedure TFormMain.btndisconnectClick (Sender: TOBJECT);

VAR

Received: String;

Begin

Lblog.Items.Add ('quit');

Try

IDTCPCLIENT.WRITELN ('quit');

Finally

IDTCPCLIENT.DISCONNECT ();

Lblog.Items.Add ('The connection to the host' edthost.text 'is disconnected!'); ');

BtnConnect.enabled: = true;

Btnsend.enabled: = false;

BTNDISCONNECT.ENABLED: = false;

End; // end Try

END;

During the "Connection" button event response, first enter the host and port of the IDTCPCLIENT according to the user, and call the IDTCPCLIENT's Connect method issues a connection request. Then call the READLN method to read the server response data.

During the "Send" button event response, call the WriteLn method to write the data command to send data to the server. During the "Disable" button event response, send the quit command to the server and invoke the disconnect method to disconnect.

The program also includes code with communication information record and exception processing.

Server program code

The server program is shown in the code list 2.

Code list 2

Procedure TFormMain.btnStartClick (Sender: TOBJECT);

Begin

IDTCPSERVER.DEFAULTPORT: = STRTOINT (EDTPORT.TEXT);

IDTCPSERVER.ACTIVE: = true;

BtnStart.enabled: = false;

BtnStop.enabled: = true;

Lblog.Items.Add ('server has been successfully started!');

END;

Procedure TFormMain.btnStopClick (Sender: TOBJECT);

Begin

IDTCPSERVER.ACTIVE: = false;

BtnStart.enabled: = true;

BtnStop.enabled: = false;

Lblog.Items.Add ('server has been successfully stopped!');

END;

Procedure TFormMain.idtcpserverConnect (Athread: TidpeertHread);

Begin

Lblog.Items.Add ('from host'

Athread.Connection. Socket.Binding.Peerip

'' S connection request has been accepted! ');

Athread.Connection.writeln ('100: Welcome to the simple TCP server!');

END;

Procedure TFormMain.idtcpServeRexecute (Athread: TidpeertHread);

VAR

Scommand: String;

Begin

With Athread.Connection DO

Begin

Scommand: = readln ();

FLOGENTRY: = Scommand 'from host'

Athread.Connection.socket.binding.peerip;

Athread.Synchronize (AddLoGentry);

IF ANSISTARTSTEXT ('Data', Scommand) THEN

Begin

FReceived: = RightStr (Scommand, Length (Scommand) -5);

Writeln ('200: Data Receive Success!');

Athread.synchronize (DisplayData);

end

Else if Sametext (Scommand, 'Quit') THEN Begin

FLOGENTRY: = 'disconnects the same host'

Athread.Connection. Socket.Binding.Peerip

'Connection! "

Athread.Synchronize (AddLoGentry);

Disconnect;

end

Else Begin

Writeln ('500: Unrecognizable command!');

FLOGENTRY: = 'Unrecognizable command:' scommand;

Athread.synchronize (addlogentry); end; // endif

END;

END;

Procedure tformmain.displaydata ();

Begin

Edtdata.text: = FRECEIVED;

END;

Procedure tformmain.addlogenTry ();

Begin

Lblog.Items.Add (FLOGENTRY);

END;

The "Start" button sets the iDTCPServer's Active property to start the server, the Stop button sets the Active property to close the server.

The IDTCPSERVERCONNECT method sends a welcome information to the client as the Idtcpserver's ONCORRECT event response process. The OnCorRECT event occurs when a client connection request is accepted, and thread Athread created for the connection is passed to the IDTCPSerConnect method as a parameter.

The IDTCPServeRexecute method is an IDECUTE event response process of IDTCPServer. The Onexecute event occurs when the TidPeertHread object attempts to perform its RUN method. Onexecute event is different from the usual event, and its response process is performed in a thread context, and the parameter Athread is called it. This is important, which means that there may be multiple onexecute event response processes being executed at the same time. The Onexecute event response process will be repeated before the connection is disconnected or interrupted.

In the IDTCPServerexecute method, first read the instruction and then discriminate the instruction. If it is a DATA directive, you can solve the data and display it. If it is received, it is disconnected. It is important to point out that due to the IDTCPServeRexecute method executed in a certain thread context, the display data and add event records are done to the corresponding method to the Synchronize call.

Run the program

Run the client and server programs, follow the procedure:

1. Start the server according to the "Start" button of the server program;

2. Establish a connection with the server according to the "Connection" button of the client program;

3. Enter "Hello, IND!" In the Data Edit box of the client program, and press the "Send" button to send data;

4. Disconnect the connection with the server according to the "disconnection" button of the client program;

5. Stop the server according to the "Stop" button of the server program.

The results of the program run are shown in Figures 4 and 5.

Figure 4 Simple TCP Customer

Figure 5 Simple TCP Server

More information

To learn more about INDY, please refer to:

[1] INDY home www.nevrona.com/indy;

[2] INDY's help file;

[3] http://www.swissdelphicenter.com/en/indyarticles.php About Indy article;

[4] Chad Z. HOWER (Indy's primitive author) and Hadi Hariri, "INDY IN DEPTH", can be found from http://www.atozedsoftware.com/;

[5] MARCO CANTU, Sybex published in "Mastering Delphi 7".

转载请注明原文地址:https://www.9cbs.com/read-129153.html

New Post(0)