I have a problem that is driving me up the wall. I am trying to port the server side of an Indy 10 client/server application on Windows to Linux to save costs. The application was originally developed using Delphi 2010. I have since ported it to Lazarus/FreePascal and it works fine on Windows. Given that Lazarus/FreePascal is multiplatform and free, it is the ideal candidate for the job.
I've tried all I can to get the server application to work on Linux without success. The server just does not communicate with the connected clients. Nothing at all!
I then decided to go back to square one. I tried to get a very basic example to work on Linux. The relevant parts of the source code is as shown below
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
s: string;
i: Integer;
begin
with AContext.Connection.IOHandler do
try
WriteLn('Type an integer and Enter');
s := ReadLn;
try
i := StrToInt(s);
WriteLn(s + ' squared is ' + IntToStr(i*i));
except
WriteLn(s + ' is not an integer');
end;
finally
Free;
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
var
Binding: TIdSocketHandle;
begin
{$IFDEF UNIX}
Binding := IdTCPServer1.Bindings.Add;
//Binding.IPVersion := Id_IPv4; <----- Gives compilation error Error: Identifier not found "Id_IPv4"
{$ENDIF}
Binding.IP := '127.0.0.1';
Binding.Port := 6501;
IdTCPServer1.Active := True;
end;
end.
This is the program's project file squares.lpr
program squares;
{$mode objfpc}{$H+}
// The following line is is necessary for Linux thread support
{$IFDEF UNIX}{$DEFINE UseCThreads}{$ENDIF}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, uSquares
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource := True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
When I try to connect to the server from the terminal using telnet, I get the following response
telnet 127.0.0.1 6501
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
7
Connection closed by foreign host.
As you can see, telnet connects to the server. But the server's first response after the client connects "Type an integer and Enter" does not show up. In addition, when I send a number to the server e.g "7" to be squared, telnet says "Connection closed by foreign host". So the telnet client also does not receive the server's responses at all. I'm using Indy svn version so it is not a question of an old Indy version.
So even this basic example does not work in Linux! I don't know how to solve this problem so I really need your help. In addition, if you have any material I can read on socket programming on Linux using Pascal, I'll really appreciate it.
I'm using Lazarus 0.9.31/FPC 2.4.4 and Indy 10.5.8 on Linux Mint.
JDaniel