Einzelnen Beitrag anzeigen

Benutzerbild von Mavarik
Mavarik

Registriert seit: 9. Feb 2006
Ort: Stolberg (Rhld)
4.143 Beiträge
 
Delphi 10.3 Rio
 
#4

Re: To Block or not to Block. Frei nach WS

  Alt 23. Sep 2006, 00:16
Ja eben...bzw. nein

Blocking

Der Server startet für jede Verbindung einen eigenen Thread der in einer Endlosschleife wartet...

Delphi-Quellcode:
while NewSock.Connected do
    try
      Sleep(100);
      Command:=NewSock.ReceiveLine();
      //ReceiveLine returns when:
      // BlockingTimeout expires (return value=''), or
      // data has been received, but no full line (return value=''), or
      // data has been received, full line (return value=full line)
      if Command<>'then
      begin
        LogBox.Items.Insert(0, 'Receive: '+Command);
        if (UpperCase(Command)='DATE') then
          NewSock.SendLine(DateTimeToStr(Now))
        else if (UpperCase(Command)='QUIT') then
          NewSock.Close
        else
          NewSock.SendLine('Unknown or no command received. Valid commands are: DATE , QUIT');
      end;
    except
      on E:ESockException do LogBox.Items.Insert(0, 'Exception: '+E.Message);
    end;
Nonblocking

Der Server kann sich mit sich selber beschäftigen, bis der entsprechende event diese Procedure aufruft.

Delphi-Quellcode:
procedure TMainForm.MasterSockRead(Sender: TObject; Count: Integer);
var
  Command: String;
  NewSock: TSock;
begin
  NewSock:=(Sender as TSock);
  try
    Command:=NewSock.ReceiveLine;
    if (Length(Command)>0) then
    begin
      LogBox.Items.Insert(0, 'Receive: '+Command);
      if (UpperCase(Command)='DATE') then
        NewSock.SendLine(DateTimeToStr(Now))
      else if (UpperCase(Command)='QUIT') then
        NewSock.Close
      else
        NewSock.SendLine('Unknown command. Valid commands are: DATE , QUIT');
    end;
  except
    on E:ESockException do LogBox.Items.Insert(0, 'Exception: '+E.Message);
  end;
end;

Frank

(Aus den Beispielprogrammen)
  Mit Zitat antworten Zitat