Delphi Cross Platform Socket Issue

  

First of all I am trying to make a Client/Server:
Server is an app in a PC (VCL).
Client is an android App.
When the server is online, once i open the client its connects normally,
When the server is offline, I need to auto re-connect to the server but in this case the client is stuck at this procedure and doesn’t get out of it:
FClientSocket.Connect(string.Empty, ‘192.168.1.155’+#0, string.Empty, 4851);

trying to free the socket gives an error..
trying to free the thread doesn’t get freed..
i did everything but nothing worked ..
My code so far (stripped):
uses ..System.Net.Socket..
..
var
Form1: TForm1;
FClientSocket: TSocket;
FThread, RThread: TThread;
Connected : Boolean = False;

Procedure Connect;
begin
FClientSocket := TSocket.Create(TSocketType.TCP);
FClientSocket.Connect(string.Empty, ‘192.168.1.155’+#0, string.Empty, 4851);
Connected := True;
end;

Procedure Disconnect();
Begin
Connected := False;
if FClientSocket <> nil then
FClientSocket.Free;
end;

Procedure TForm1.SocketConnect;
begin
Connected := False;
//Connect to VCL server
RThread := TThread.CreateAnonymousThread(procedure
begin
Connect;
end);
RThread.FreeOnTerminate := True;
RThread.Start;
Sleep(500);

While Connected do
begin
// Receive the message from the VCL server
// ..
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FThread := TThread.CreateAnonymousThread(procedure
begin
SocketConnect;
end);
FThread.FreeOnTerminate := True;
FThread.Start;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if FClientSocket <> nil then
Begin
FClientSocket.Free;
end;
if FThread <> nil then
Begin
FThread.OnTerminate := nil;
FThread.FreeOnTerminate := False;
FThread.Terminate;
FThread.WaitFor;
FreeAndnil(FThread);
end;
end;

Comments are closed.