TTcpServer "stopped" in Linux

255 views Asked by At

The two console applications include one socket server using TTcpServer of bmThreadBlocking mode, and one socket client using TTcpClient of bmBlocking mode. The TTcpClient is intended to connect to the server, send a line, and then disconnect. The TTcpServer is intended to listen to the incoming connections, and echo the line received in its OnAccept event handler.

These two console applications run fine in Windows (XP and 7). However, when I compile it using CrossKylix or Kylix directly, the applications cannot run as expected in Linux (SuSE 10.0 and CentOs 5u7). The server application gets "[1]+ Stopped ./TestSocketServer_1_Console_Native" as soon as one client connects, i.e., within/after TTcpServer's OnAccept event handler. Could you help to comment on this problem? Any help will be appreciated!

The two console applications can be downloaded at http://www.multiupload.com/9HIIG61W93

The source code is also pasted here for your convenience. (The port number, 98765 or 1876 or some other number, does not help.)

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.
0

There are 0 answers