Home > Back-end >  TcpServer onGetThread events, how to use online is old material, not suitable for, well, less and le
TcpServer onGetThread events, how to use online is old material, not suitable for, well, less and le

Time:10-18

The TcpServer procedure TForm_main. TcpServer1GetThread (Sender: TObject;
Var ClientSocketThread: TClientSocketThread); Events should be how to do?
Checked for half a day, not to use, the Internet is one more parameter, here are two parameters, there are no corresponding example!

CodePudding user response:

Tcpserver component properties
LocalHostAddr get local machine IP;
Localport for local port;
LocalHostName local computer name;
BlockMode attributes, can be set for bmBlocking TTCPServer \ bmNonBlocking \ bmThreadBlocking this three kinds of communication mode;

BmBlocking for blocking mode, data transmission, on the basis of synchronous
BmNonBlocking non-blocking mode, data transmission in an asynchronous manner,
BmThreadBlocking thread asynchronous mode, create a child thread communication with TcpClient, synchronously in the thread data transmission;

If is bmBlocking mode, can write code OnAccept event
Procedure TForm1. TcpServer1Accept (Sender: TObject; ClientSocket: TCustomIpClient);
//receive the tcpclient data
ClientSocket. ReceiveFrom ()
ClientSocket. ReceiveBuf ()

//send data to the tcpclient
ClientSocket. SendBuf ()
ClientSocket. SendTo ()

If is bmThreadBlocking mode, can write code OnGetThread events
Procedure TForm1. TcpServer1GetThread (Sender: TObject; Var ClientSocketThread: TClientSocketThread);
The begin
ClientSocketThread. ClientSocket.
//receive the tcpclient data
ClientSocketThread. ClientSocket. ReceiveFrom ()
ClientSocketThread. ClientSocket. ReceiveBuf ()

//send data to the tcpclient
ClientSocketThread. ClientSocket. SendBuf ()
ClientSocketThread. ClientSocket. SendTo ()
Are Listening to return booleans, listen
LookupHostName IP access to the host name specified argument;

D7, & gt;
TCPserver communication method1, specify its Localport attribute value is greater than 1024 integer value, the purpose is to specify TCPsever1 listener port;
2, set its communication mode: Blockmode;
3, open TCPsever1 monitor client connection requests and TCPsever1. Active:=true;
4, in TCPserver1 OnAccept event in writing, speaking, reading and writing client code; Receivebuf (char array, length), Sendbuf client connection (char array, length), speaking, reading and writing;

Var buff: an array of char [0.. 255];
Tmpbuf: string;
The begin
Clientsocket. OnReceive:=onbuff;//custom Onbuff display receives data
While ClientSocket. Connected do
The begin
If ClientSocket. WaitForData (50) then//guarantee nonblocking read data;
ClientSocket. Receivebuf (buff [0], sizeof (buff));//must be outside the decision or send out data

If outbuf<> 'then//outbuf for global variables used to receive the Form in the Memo value;
The begin
Tmpbuf:=outbuf;
Outbuf:=';
ClientSocket. SendBuf (pchar (tmpbuf) ^, length (tmpbuf));
end;

//will receive according to the Memo
Procedure Tform1. Onbuff (Sender: TObject; Buf: pchar; Var DataLen: Integer);
Var buff: pchar;
St: string;
I: integer;
The begin
St:=';
Buff:=buf;
For I:=0 to datalen do
The begin
St: st + buff=^.
Inc (buff);
end;
Self. Memo1. Lines. The Add (st);
end;