dsocksvr.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Dual server program. This will listen on port 4100 till
  13. a client connects if '-i' is on the command-line.
  14. Otherwise it will open a unix socket. You can connect by
  15. running the 'sockcli' or 'dsockcli' programs in another
  16. terminal.
  17. specifying -b on the command-line will disable blocking.
  18. }
  19. {$mode objfpc} {$H+}
  20. uses sysutils,ssockets,getopts;
  21. const
  22. ThePort=4100;
  23. TheSocket = 'ServerSoc';
  24. Var
  25. DoInet,NonBlocking : boolean;
  26. Type
  27. TServerApp = Class(TObject)
  28. Private
  29. FCalls : longint;
  30. FServer : TSocketServer;
  31. Public
  32. Constructor Create(Port : longint);
  33. Constructor Create(Socket : String);
  34. Destructor Destroy;override;
  35. Procedure OnConnect (Sender : TObject; Data : TSocketStream);
  36. Procedure OnIdle(Sender : TObject);
  37. Procedure Run;
  38. end;
  39. Constructor TServerApp.Create(Port : longint);
  40. begin
  41. FServer:=TINetServer.Create(Port);
  42. FServer.OnConnect:=@OnConnect;
  43. end;
  44. Constructor TServerApp.Create(Socket : String);
  45. begin
  46. FServer:=TUnixServer.Create(Socket);
  47. FServer.OnConnect:=@OnConnect;
  48. end;
  49. Destructor TServerApp.Destroy;
  50. begin
  51. FServer.Free;
  52. end;
  53. Procedure TServerApp.OnConnect (Sender : TObject; Data : TSocketStream);
  54. Var Buf : ShortString;
  55. Count : longint;
  56. begin
  57. Repeat
  58. Count:=Data.Read(Buf[1],255);
  59. SetLength(Buf,Count);
  60. Write('Server got : ',Buf);
  61. Until (Count=0) or (Pos('QUIT',Buf)<>0);
  62. Data.Free;
  63. FServer.StopAccepting;
  64. end;
  65. Procedure TServerApp.OnIdle(Sender : TObject);
  66. begin
  67. Inc(FCalls);
  68. Write('.');
  69. end;
  70. Procedure TServerApp.Run;
  71. begin
  72. Write ('Listening on ');
  73. if FServer is TUnixServer Then
  74. Writeln ('socket : ',(FServer as TUnixServer).Filename)
  75. else If FServer is TINetServer Then
  76. Writeln ('port : ',(FServer as TInetServer).port);
  77. If NonBlocking then
  78. begin
  79. FServer.SetNonBlocking;
  80. FServer.OnIdle:=@OnIdle;
  81. end;
  82. FServer.StartAccepting;
  83. end;
  84. Var
  85. Application : TServerApp;
  86. c : char;
  87. begin
  88. DoInet:=False;
  89. NonBlocking:=False;
  90. repeat
  91. c:=getopt('ib');
  92. case c of
  93. 'b' : NonBlocking:=True;
  94. 'i' : DoInet:=True;
  95. end;
  96. until c=EndOfOptions;
  97. If DoInet then
  98. Application:=TServerApp.Create(ThePort)
  99. else
  100. Application:=TServerApp.Create(TheSocket);
  101. Application.Run;
  102. Application.Free;
  103. end.