Q&A

  • 스레드(thread)에서 서버소켓의 이벤트를 연결했는데 이벤트가 안타는데요.
이곳의 자료를 찾아 짜맞추기를 해보았는데
이벤트를 타지 않는데요.
왜 그런지요?

<!--CodeS-->
//메인 화면
implementation
var I : Integer;
var FSocThread : TSocThread;

{$R *.dfm}
procedure TFormServer.FormCreate(Sender: TObject);
begin
  I := 0;
  ServerSocket1.Open;
end;

procedure TFormServer.FormDestroy(Sender: TObject);
begin
  if (FSocThread <> nil) then  begin
    FSocThread.Terminate;
    FSocThread.WaitFor;  // wait for it to terminate
  end;
  if ServerSocket1.Active then ServerSocket1.Close;
end;

procedure TFormServer.Timer1Timer(Sender: TObject);
begin
  Inc(I);
  ListBoxMAIN.Items.Add(IntToStr(I));
end;

procedure TFormServer.ButtonSTARTClick(Sender: TObject);
begin
  TSocThread.Create(ServerSocket1, Handle);
end;

end.

//스레드
unit SocThreadClass;

interface

uses
  Classes,ScktComp,Messages;
const
  wm_RefreshClients = wm_User;

type
  TSocThread = class(TThread)
  private
    { Private declarations }
    SvrSocket : TServerSocket;
    procedure SvrSocketClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure SvrSocketClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure SvrSocketClientRead(Sender: TObject; Socket: TCustomWinSocket);
  protected
    procedure Execute; override;
  public
    constructor Create(Conn:TServerSocket; FHandle:Integer);
    procedure RefreshClients(var Msg:TMessage);
       message wm_RefreshClients;
  end;

implementation

{ SocketThread }
uses SocketServer;

constructor TSocThread.Create(Conn:TServerSocket; FHandle:Integer);
begin
  { Place thread code here }
  inherited Create(True);
  SvrSocket := Conn;
  SvrSocket.OnClientConnect    := SvrSocketClientConnect;
  SvrSocket.OnClientDisconnect := SvrSocketClientDisconnect;
  SvrSocket.OnClientRead       := SvrSocketClientRead;
  Resume;
end;

procedure TSocThread.Execute;
begin
  try
     while SvrSocket.Active do begin
       //
     end;
  except
     if SvrSocket.Active then SvrSocket.Active := False;
     if not self.Terminated then Self.Terminate;
  end;

end;

procedure TSocThread.SvrSocketClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  FormServer.ListBoxLOG.Items.Add ('Connected: ' + Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')' );
end;

procedure TSocThread.SvrSocketClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  FormServer.ListBoxLOG.Items.Add ('Disconnected: '+Socket.RemoteHost + ' (' + Socket.RemoteAddress + ')' );
end;

procedure TSocThread.SvrSocketClientRead(Sender: TObject; Socket: TCustomWinSocket);
var       strCommand, strFeedback: string;
begin
  // read from the client
  strCommand := Socket.ReceiveText;
  FormSERVER.ListBoxLOG.Items.Add ('Client: ' + Socket.RemoteAddress + ': ' +strCommand);
  // log result
  FormSERVER.ListBoxLOG.Items.Add (strFeedback);
end;

procedure TSocThread.RefreshClients;
var          I : Integer;
begin
  FormSERVER.ListBoxCLIENTS.Clear;
  for I := 0 to SvrSocket.Socket.ActiveConnections - 1 do
    with SvrSocket.Socket.Connections [I] do
      FormServer.ListBoxCLIENTS.Items.Add (RemoteAddress + ' (' + RemoteHost + ')');
end;

<!--CodeE-->
0  COMMENTS