Home > Back-end >  The Delphi problem of TCP
The Delphi problem of TCP

Time:09-20

This is the client's
 implementation 

{$R *. DFM}

Procedure Tfrm_Client. Btn_connectClick (Sender: TObject);
The begin
If idtcpclnt1=nil then exit;
If not idtcpclnt1. Connected then
The begin
Try
Idtcpclnt1. Host:=idpwtch1. LocalIP;
Idtcpclnt1. Port:=1119;
Idtcpclnt1. Connect;
Except,
ShowMessage (" connection failed ");
Idtcpclnt1. Disconnect;
The exit
end;

end;
end;

Procedure Tfrm_Client. Btn_sendClick (Sender: TObject);
Var
S: a string;
The begin
S:=edt1. Text;
Idtcpclnt1. IOHandler. Write (S);
end;


This is a server-side
 implementation 

{$R *. DFM}

Procedure TFrm_Server. Btn_OpenServerClick (Sender: TObject);
The begin
Idtcpsrvr1. Bindings. Add;
Idtcpsrvr1. Bindings. Items [0]. Port:=1119;
Idtcpsrvr1. Bindings. Items [0]. IP:=idpwtch1. LocalIP;
Idtcpsrvr1. Active:=True;
end;

Procedure TFrm_Server. Idtcpsrvr1Execute (AContext: TIdContext);
Var
S: TStringList;
The begin
S:=TStringList. Create;
AContext. Connection. IOHandler. ReadStrings (s);
Mmo1. Lines. The Add (S.T ext);
end;

End.


Can even but does not display the bar I go

CodePudding user response:

Send: idtcpclnt1. IOHandler. Write (S, IndyTextEncoding_UTF8);//specified coding gibberish,
Accept when you need to specify the number of rows, the number of rows to add before data transfer, receive 1 row: AContext. Connection. The IOHandler. ReadStrings (S, 1, IndyTextEncoding_UTF8);
Transceiver can also use WriteLn and ReadLn, need not consider the number of lines, in addition, the string list need to release, or a memory leak,

CodePudding user response:

reference 1st floor santiaodahan response:
send: idtcpclnt1. IOHandler. Write (S, IndyTextEncoding_UTF8);//specified coding gibberish,
Accept when you need to specify the number of rows, the number of rows to add before data transfer, receive 1 row: AContext. Connection. The IOHandler. ReadStrings (S, 1, IndyTextEncoding_UTF8);
Transceiver can also use WriteLn and ReadLn, need not consider the number of lines, in addition, the string list need to release, or a memory leak,

Thank you how to send multiple clients, I'm doing a double play renju

CodePudding user response:

refer to the second floor baidu_36063547 response:
Quote: refer to 1st floor santiaodahan response:

Send: idtcpclnt1. IOHandler. Write (S, IndyTextEncoding_UTF8);//specified coding gibberish,
Accept when you need to specify the number of rows, the number of rows to add before data transfer, receive 1 row: AContext. Connection. The IOHandler. ReadStrings (S, 1, IndyTextEncoding_UTF8);
Transceiver can also use WriteLn and ReadLn, need not consider the number of lines, in addition, the string list need to release, or a memory leak,

Thank you how to send multiple clients, I'm doing a double play renju

The server can connect multiple clients, through the server communication between each other, Indy10demo \ Chat in 2.0 how Chat client instance, there are examples of Chat in Indy9 slightly have the difference, also can consult,
  • Related