Home > Back-end >  Using standard TCP/IP protocol to short synchronous communication connecting the stable connection o
Using standard TCP/IP protocol to short synchronous communication connecting the stable connection o

Time:09-25

Delphi xe5 development
Communication packet format
Head logo (0 x02) 1 BYTE
Business packet length 4 BYTE
Business packet n BYTE
The tail sign (0 x03) 1 BYTE
Check code 1 BYTE

Procedure TForm1. QueryBankClick (Sender: TObject);
Var
Senddata publishes the event, STR, get string: a string;
Makecode, I: Integer;
The begin
STR:=Format (' % 3 s % % % % 4 s 4 s 4 s - '20 s, [' 111', '0991', '1100', '0010', '13609994211']);
STR:=Format (' % s' %. 4 d, [Length (STR), STR]);

Makecode:=word (# 02);
For I:=1 to Length (STR) do
The begin
Makecode:=makecode xor word (STR [I]);
end;
Makecode:=makecode xor word (# 3);
Senddata publishes the event:=# 02 # + STR + 3 + CRH (makecode);

Then if SendToServer (senddata publishes the event, get string)=0
The begin
End


The SendToServer content is as follows:
The function SendToServer (const senddata publishes the event: the string; Var Retstring: string) : Integer;
Var
TCPCLNT: TIdTCPClient;
Readchar: Char;
Readbyte readbyte2: Byte;
Sendstr: TIdBytes;
StartTime: Cardinal;
I, makecode: Integer;//effect words pay
The begin
Result:=1;
TCPCLNT:=TIdTCPClient. Create (nil);
Retstring:=';
Makecode:=word (# 02);
Try
TCPCLNT. Host:='136.204.9.11';
TCPCLNT. Port:=1111;
Try
TCPCLNT. Connect;
If not TCPCLNT. Connected then
The begin
Result:=2;
exit;
end;
Except,
Result:=2;
exit;
end;

SetLength (sendstr, Length (senddata publishes the event));
Sendstr move (senddata publishes the event [1], [0], Length (senddata publishes the event));
TCPCLNT. IOHandler. Write (sendstr, Length (sendstr) + 1);

//30 seconds overtime
StartTime: GetTickCount +=30000;
Repeat
If the startTime & lt;=GetTickCount then
The begin
Result:=3;
exit;
end;
readchar:=TCPCLNT. IOHandler. Readchar ();
Until readchar=# 02;
The finally
FreeAndNil (TCPCLNT);
end;

end;


The question is:
As long as the program runs to the readchar:=TCPCLNT IOHandler. Readchar (); There will appear the connection closed gracefully?

CodePudding user response:

That is a server error,
  • Related