这两个控制台应用程序包括一个使用 bmThreadBlocking 模式的 TTcpServer 的套接字服务器和一个使用 bmBlocking 模式的 TTcpClient 的套接字客户端。TTcpClient 旨在连接到服务器,发送一条线路,然后断开连接。TTcpServer 旨在侦听传入的连接,并回显在其 OnAccept 事件处理程序中收到的行。
这两个控制台应用程序在 Windows(XP 和 7)中运行良好。但是,当我直接使用 CrossKylix 或 Kylix 编译它时,应用程序无法在 Linux(SuSE 10.0 和 CentOs 5u7)中按预期运行。只要一个客户端连接,即在 TTcpServer 的 OnAccept 事件处理程序之内/之后,服务器应用程序就会立即获得“[1]+ Stopped ./TestSocketServer_1_Console_Native”。你能帮忙评论一下这个问题吗?任何帮助将不胜感激!
这两个控制台应用程序可以在http://www.multiupload.com/9HIIG61W93下载
为方便起见,源代码也粘贴在这里。(端口号 98765 或 1876 或其他数字没有帮助。)
TestSocketServer_1_Console_Native
TestSocketServer_1_Console_Native.dpr
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.
uServerDataModule.pas
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.
TestSocketClient_1_Console_Native
TestSocketClient_1_Console_Native.dpr
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.
uClientDataModule.pas
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.