socksvr.pp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. Program server;
  2. {
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. TUnixServerApp server program. This will listen on a socket till
  13. a client connects. You can connect by running the 'dsockcli' or
  14. 'sockcli' programs in another terminal.
  15. }
  16. {$ifndef unix}
  17. {$fatal This test is only for Unix platforms}
  18. {$endif}
  19. {$mode objfpc}{$h+}
  20. uses ssockets;
  21. const
  22. SPath='ServerSoc';
  23. Type
  24. TUnixServerApp = Class(TObject)
  25. Private
  26. FServer : TUnixServer;
  27. Public
  28. Constructor Create(SockName : String);
  29. Destructor Destroy;override;
  30. Procedure OnConnect (Sender : TObject; Data : TSocketStream);
  31. Procedure Run;
  32. end;
  33. Constructor TUnixServerApp.Create(SockName : String);
  34. begin
  35. FServer:=TUnixServer.Create(SockName);
  36. FServer.OnConnect:=@OnConnect;
  37. end;
  38. Destructor TUNixServerApp.Destroy;
  39. begin
  40. FServer.Free;
  41. end;
  42. Procedure TUnixServerApp.OnConnect (Sender : TObject; Data : TSocketStream);
  43. Var Buf : ShortString;
  44. Count : longint;
  45. begin
  46. Repeat
  47. Count:=Data.Read(Buf[1],255);
  48. SetLength(Buf,Count);
  49. Write('Server got : ',Buf);
  50. Until (Count=0) or (Pos('QUIT',Buf)<>0);
  51. Data.Free;
  52. FServer.StopAccepting;
  53. end;
  54. Procedure TUnixServerApp.Run;
  55. begin
  56. FServer.StartAccepting;
  57. end;
  58. Var
  59. Application : TUnixServerApp;
  60. begin
  61. Application:=TUnixServerApp.Create(SPath);
  62. Application.Run;
  63. Application.Free;
  64. end.