,我有一个问题,把我逼上了墙,。我试图将Windows上Indy 10客户机/服务器应用程序的服务器端移植到Linux,以节省成本。该应用程序最初是使用Delphi 2010开发的。我已经将它移植到Lazarus/FreePascal,它在Windows上运行良好。考虑到Lazarus/FreePascal是多平台和免费的,它是理想的候选人。
我已经尽力让服务器应用程序在Linux上工作,但没有成功。服务器只是不与连接的客户端通信。什么都没有!
然后我决定回到原点。我试图获得一个非常基本的例子来工作在Linux上。源代码的相关部分如下所示
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.
这是程序的项目文件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.
当我尝试使用telnet从终端连接到服务器时,我会得到以下响应
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.
如您所见,telnet连接到服务器。但是,在客户端连接“输入整数并输入”之后,服务器的第一个响应将不会出现。此外,当我向服务器发送一个数字,例如"7“被平方时,telnet表示”由外国主机关闭的连接“。因此telnet客户端也完全不接收服务器的响应。我正在使用Indy版本,所以这不是一个老的Indy版本的问题。
因此,即使这个基本示例在Linux中也不起作用!我不知道如何解决这个问题,所以我真的需要你的帮助。此外,如果您有任何关于使用Pascal在Linux上进行套接字编程的材料,我会非常感激的。
我在Linux上使用Lazarus 0.9.31/FPC 2.4.4和Indy 10.5.8。
JDaniel
发布于 2011-10-16 16:06:16
Id_IPv4
是在IdGlobal.pas中定义的,确保单元在uses
子句中。请注意,只有在定义了Bindings.Add()
时才调用Binding
,但您正在尝试访问IFDEF块之外的Binding
。你根本不需要IFDEF块。Indy默认为IPv4。
关于通信问题,如果FreePascal正确地调用了TIdIOHandler.WriteLn()
,而不是某些控制台WriteLn() I/O例程,那么您所展示的代码没有什么问题。你能给我看一下客户代码吗?
服务器端,我现在唯一能想到的可能出错的事情是Indy的TIdTextEncoding类在发送/接收字符串时可能失败,如果您已经将TIdIOHandler.DefStringEncoding
属性或全局GIdDefaultEncoding
变量设置为非默认编码的话。在非Windows系统上,TIdTextEncoding使用iconv库,而Indy的Indy支持现在被认为有点错误。另一方面,Indy的默认编码是ASCII,它根本不依赖于iconv,所以使用它不应该有任何失败。
发布于 2011-10-16 22:37:34
@雷米·莱博。我找到了答案。我一直在玩文本编码,直到得到了下面的代码:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
s: string;
i: Integer;
begin
with AContext.Connection.IOHandler do
try
{$IFDEF UNIX}
DefStringEncoding := TIdTextEncoding.Default
{$ENDIF}
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.Port := 6501;
{$ENDIF}
{IFDEF MSWINDOWS}
IdTCPServer1.DefaultPort := 6501
{$ENDIF}
IdTCPServer1.Active := True;
end;
这是telnet的响应:
telnet 127.0.0.1 6501
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Type an integer and Enter
7
7 squared is 49
Connection closed by foreign host.
但是,我仍然想知道如何设置全局GIdDefaultEncoding变量,以便消除DefStringEncoding := TIDTextEncoding.Default。谢谢你的帮助。
https://stackoverflow.com/questions/7767616
复制相似问题