两个控制台应用程序包括一个使用bmThreadBlocking模式的TTcpServer的套接字服务器,以及一个使用bmBlocking模式的TTcpClient的套接字客户端。 TTcpClient旨在连接到服务器,发送一行,然后断开连接。 TTcpServer旨在侦听传入的连接,并回显其OnAccept事件处理程序中收到的行。
这两个控制台应用程序在Windows(XP和7)中运行良好。但是,当我直接使用CrossKylix或Kylix编译它时,应用程序无法在Linux(SuSE 10.0和CentOs 5u7)中按预期运行。一个客户端连接时,即在TTcpServer的OnAccept事件处理程序内/之后,服务器应用程序获得“[1] +已停止./TestSocketServer_1_Console_Native”。你能帮忙评论一下这个问题吗?任何帮助将不胜感激!
可以在http://www.multiupload.com/9HIIG61W93
下载两个控制台应用程序为方便起见,也会粘贴源代码。 (端口号,98765或1876或其他一些号码,没有帮助。)
program TestSocketServer_1_Console_Native;
{$APPTYPE CONSOLE}
uses
uServerDataModule in 'uServerDataModule.pas' {ServerDataModule: TServerDataModule},
SysUtils;
begin
{ TODO -oUser -cConsole Main : Insert code here }
ServerDataModule := TServerDataModule.Create(nil);
try
ServerDataModule.tcpServerCCL.Active := True;
while True do
begin
Sleep(500);
end;
finally
ServerDataModule.Free;
end;
end.
unit uServerDataModule;
interface
uses
SysUtils, Classes, Sockets;
type
TServerDataModule = class(TDataModule)
tcpServerCCL: TTcpServer;
private
{ Private declarations }
procedure tcpServerCCLAccept(Sender: TObject; ClientSocket: TCustomIpClient);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
end;
var
ServerDataModule: TServerDataModule;
implementation
{$R *.dfm}
{ TServerDataModule }
constructor TServerDataModule.Create(AOwner: TComponent);
begin
inherited;
tcpServerCCL.Active := False;
tcpServerCCL.LocalPort := '98765';
tcpServerCCL.OnAccept := tcpServerCCLAccept;
end;
procedure TServerDataModule.tcpServerCCLAccept(Sender: TObject; ClientSocket:
TCustomIpClient);
var
l_InputStr: string;
begin
WriteLn('Accepted connection from ' + ClientSocket.LocalHost);
l_InputStr := ClientSocket.Receiveln();
Writeln(PChar(l_InputStr));
end;
end.
program TestSocketClient_1_Console_Native;
{$APPTYPE CONSOLE}
uses
uClientDataModule in 'uClientDataModule.pas' {ClientDataModule: TClientDataModule},
SysUtils;
begin
{ TODO -oUser -cConsole Main : Insert code here }
ClientDataModule := TClientDataModule.Create(nil);
try
if ClientDataModule.tcpClientCCL.Connect then
begin
ClientDataModule.tcpClientCCL.Sendln('hello from client');
end;
finally
end;
end.
unit uClientDataModule;
interface
uses
SysUtils, Classes, Sockets;
type
TClientDataModule = class(TDataModule)
tcpClientCCL: TTcpClient;
private
{ Private declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
end;
var
ClientDataModule: TClientDataModule;
implementation
{$R *.dfm}
{ TClientDataModule }
constructor TClientDataModule.Create(AOwner: TComponent);
begin
inherited;
tcpClientCCL.Active := False;
tcpClientCCL.RemoteHost := '127.0.0.1';
tcpClientCCL.RemotePort := '98765';
end;
end.