-
Notifications
You must be signed in to change notification settings - Fork 5
/
Unit4.pas
171 lines (137 loc) · 4.19 KB
/
Unit4.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
unit Unit4;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB,
Datasnap.DBClient, Datasnap.Win.MConnect, Datasnap.Win.SConnect,
IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer,
System.Win.ScktComp,IdContext,IdGlobal,IDSync, IdTCPConnection, IdTCPClient,
IdHTTP, System.Net.URLClient, System.Net.HttpClient,
System.Net.HttpClientComponent;
type
TForm4 = class(TForm)
Memo1: TMemo;
IdTCPServer1: TIdTCPServer;
Edit2: TEdit;
IdTCPClient1: TIdTCPClient;
NetHTTPClient1: TNetHTTPClient;
Label1: TLabel;
Memo2: TMemo;
procedure FormActivate(Sender: TObject);
procedure ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
Stream: TMemoryStream;
FSize: Integer;
writing: Boolean;
public
{ Public declarations }
Terminal : TStringList;
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
uses uCRC, uGPS;
procedure onLogin(imei: string; Serial: word;ip: string);
begin
Form4.Memo1.Lines.Add(imei + ' logged in: '+ip+' '+ IntToStr(Serial));
end;
procedure TForm4.Button1Click(Sender: TObject);
begin
IdTCPClient1.Host := '41.111.44.206';
IdTCPClient1.Port:=strtoint(Edit2.Text);
IdTCPClient1.Connect;
//IdTCPClient1.Socket.WriteLn(Edit1.Text);
end;
procedure TForm4.Button2Click(Sender: TObject);
begin
//
IdTcpServer1.DefaultPort := strtoint(Edit2.Text);
//IdTcpServer1.Active := True;
{IdTCPClient1.Host := '41.105.58.201';
IdTCPClient1.Port:=9090;
IdTCPClient1.Connect;}
//Memo1.Lines.Add('ready.....');
//ClientSocket1.Active := True;
end;
procedure TForm4.Button3Click(Sender: TObject);
begin
IdTcpServer1.Active := False;
Memo1.Lines.Clear;
end;
procedure TForm4.FormActivate(Sender: TObject);
var
LHTTP: TNetHTTPClient;
Params: TStringList;
begin
Terminal := TStringList.Create;
IdTcpServer1.DefaultPort := strtoint(Edit2.Text);
IdTcpServer1.Active := true;
LHTTP := TNetHTTPClient.Create(nil);
try
Params := TStringList.Create;
try
Params.Add('test=hello');
finally
Params.Free;
end;
finally
LHTTP.Free;
end;
Label1.Caption := 'Ready...................';
end;
procedure TForm4.FormCreate(Sender: TObject);
begin
uGPS.onLogin(onLogin);
end;
function BytesToString(const Value: TBytes): WideString;
begin
SetLength(Result, Length(Value) div SizeOf(WideChar));
if Length(Result) > 0 then
Move(Value[0], Result[1], Length(Value));
end;
procedure TForm4.IdTCPServer1Execute(AContext: TIdContext);
var
s: String;
aByte: Byte;
start, information, serial, error, stop, data: TIdBytes;
length, protocol: byte;
crc: word;
i: Integer;
begin
{s := AContext.Connection.IOHandler.ReadLn();//AContext.Connection.IOHandler.ReadLn(IndyUTF16BigEndianEncoding);
Memo1.Lines.Add(s);}
Memo1.Lines.Add(AContext.Connection.Socket.Binding.PeerIP);
uGPS.onRecieve(AContext.Connection.IOHandler, s, Terminal,AContext.Connection.Socket.Binding.PeerIP);
Memo1.Lines.Add(s);
end;
procedure TForm4.ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('RemoteAddress : '+Socket.RemoteAddress);
Memo1.Lines.Add('RemoteHost : '+Socket.RemoteHost);
Memo1.Lines.Add('LocalAddress : '+Socket.LocalAddress);
Memo1.Lines.Add('LocalHost : '+Socket.LocalHost);
end;
procedure TForm4.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
RecvStr : String;
Buffer:array[0..999] of Ansichar;
begin
RecvStr := Socket.ReceiveText;
//Socket.ReceiveBuf(Buffer,Socket.ReceiveLength);
Memo1.Lines.Add('message : '+RecvStr);
Memo1.Lines.Add('AnsiString : '+AnsiString(RecvStr));
Memo1.Lines.Add(' : '+RecvStr);
//Memo1.Lines.Add('message : '+Socket.ReceiveText());
//ClientSocket1.Active := false;
end;
end.