The simplest communication instance of Ttcpserver and Ttcpclient-delphi

Source: Internet
Author: User

Unit Tcpscdemo;
The simplest communication instance of Ttcpserver and Ttcpclient-delphi

Borland introduces Ttcpserver and Ttcpclient as the main network communication control, which means that the two controls have great advantages,
But the information about the two controls is very rare, and many people are asking the same question: How does tcpserver send data to TcpClient?
Online answers are all sorts of strange, summed up there are three kinds:
1. Refer to the Delphi Netchat. Example TCPServer only receive, answer the question score 0.
2. Use Indy or Serversocket/clientsocket to say things. Irrelevant, answer 0.
3. Suggest the use of Indy or serversocket/clientsocket, reasonable, also works, but the answer is not to the point, answer 59.
Today I would like to give an example is to use Ttcpserver and ttcpclient for two-way communication, I believe that after watching will, the answer score 99.
Why not 100? Because it's too easy!

Interface

Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, Stdctrls;

Type
Work Flow:
After the program runs, click [Connect] to connect TcpClient and TCPServer
Click [Send] to send the text in the Edit1 to the Tcpserver,tcpserver received later to add to Mserver, and reply to a message.
TcpClient1 received tcpserver reply message and added to TCPServer
All the controls are talking about it? Let ' s go!
Tfrmmain = Class (Tform)
Edit1:tedit;
Button1:tbutton; Connection
Button2:tbutton; Send
Mserver:tmemo;
Mclient:tmemo;
Tcpserver1:ttcpserver; active:=true;host:=127.0.0.1;port:=2011; Other default values
Tcpclient1:ttcpclient; remotehost:=127.0.0.1;remoteport:=2011; Other default values
Procedure Tcpserver1accept (Sender:tobject;
Clientsocket:tcustomipclient);
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End

This class implements the message that Ttcpclient receives ttcpserver reply
Tclientreceivethread = Class (TThread)
Procedure Execute; Override
End

Var
Frmmain:tfrmmain;
Clientreceivethread:tclientreceivethread;

Implementation

{$R *.DFM}

Procedure Tclientreceivethread.execute;
Begin
With Frmmain do
Begin
While tcpclient1.connected do//dead loop! Necessary!!!
MCLIENT.LINES.ADD (Tcpclient1.receiveln);
End
End

TcpServer1 's OnAccept Event
Procedure Tfrmmain.tcpserver1accept (Sender:tobject;
Clientsocket:tcustomipclient);
Var
s:string;
Begin
While clientsocket.connected do//dead loop! Necessary!!!
Begin
s: = Clientsocket.receiveln; Inbox
MSERVER.LINES.ADD (s);
S: = Datetimetostr (now);
CLIENTSOCKET.SENDLN (s); Hair
End
End

Procedure Tfrmmain.button1click (Sender:tobject);
Begin
Connect to the server if the client receive thread is created successfully
If Tcpclient1.connect then clientreceivethread: = Tclientreceivethread.create (false);
End

Procedure Tfrmmain.button2click (Sender:tobject);
Begin
TCPCLIENT1.SENDLN (Edit1.text);
End

It's really simple, the last word:
End.

Simplest ttcpserver and ttcpclient communication instance-delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.