From 3f347f5a77ed85faaaf04a90a5ed9194fcd58e18 Mon Sep 17 00:00:00 2001 From: mysticbbs Date: Sat, 17 Aug 2013 22:27:16 -0400 Subject: [PATCH] Initial import --- mdl/m_tcp_client.pas | 90 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 mdl/m_tcp_client.pas diff --git a/mdl/m_tcp_client.pas b/mdl/m_tcp_client.pas new file mode 100644 index 0000000..b121e08 --- /dev/null +++ b/mdl/m_tcp_client.pas @@ -0,0 +1,90 @@ +Unit m_TCP_Client; + +{$I M_OPS.PAS} + +Interface + +Uses + Classes, + m_Strings, + m_IO_Sockets; + +Type + TTCPClient = Class + Client : TIOSocket; + ResponseType : Integer; + ResponseData : TStringList; + + Constructor Create; + Destructor Destroy; Override; + Function Connect (Address: String; Port: Word) : Boolean; + Function SendCommand (Str: String) : Integer; + Function GetResponse : Integer; + End; + +Implementation + +Constructor TTCPClient.Create; +Begin + Inherited Create; + + Client := NIL; + ResponseData := TStringList.Create; +End; + +Destructor TTCPClient.Destroy; +Begin + Client.Free; + ResponseData.Free; + + Inherited Destroy; +End; + +Function TTCPClient.Connect (Address: String; Port: Word) : Boolean; +Begin + Client := TIOSocket.Create; + + Result := Client.Connect(Address, Port); +End; + +Function TTCPClient.SendCommand (Str: String) : Integer; +Begin + Result := -1; + + If Client.FSocketHandle = -1 Then Exit; + + Client.WriteLine(Str); + //WriteLn(Str); + + Result := GetResponse; +End; + +Function TTCPClient.GetResponse : Integer; +Var + Str : String; +Begin + Result := -1; + + If Client.FSocketHandle = -1 Then Exit; + + If Client.ReadLine(Str) > 0 Then Begin + ResponseType := strS2I(Copy(Str, 1, 3)); + Result := ResponseType; + + //WriteLn(Str); + + If Str[4] = '-' Then Begin + ResponseData.Clear; + + Repeat + If Client.ReadLine(Str) <= 0 Then Break; + + ResponseData.Add(Str); + + //WriteLn(Str); + Until Copy(Str, 1, 4) = strI2S(ResponseType) + ' '; + End; + End; +End; + +End.